#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
using ll=long long;
struct A{
multiset<ll> L,R;
ll mn;
A():mn(0){}
void insert(ll x){
if(L.empty()) L.emplace(x), R.emplace(x);
else if(x<*L.rbegin()){
mn+=*L.rbegin()-x;
L.emplace(x), L.emplace(x);
R.emplace(*L.rbegin()), L.erase(prev(L.end()));
}
else if(x>*R.begin()){
mn+=x-*R.begin();
R.emplace(x), R.emplace(x);
L.emplace(*R.begin()), R.erase(R.begin());
}
else L.emplace(x), R.emplace(x);
}
A& operator+=(A o){
if(L.size()<o.L.size()) swap(*this,o);
for(auto &e:o.L){
if(e<*R.begin()) L.emplace(e);
else{
mn+=e-*R.begin();
R.emplace(e), L.emplace(*R.begin()), R.erase(R.begin());
}
}
for(auto &e:o.R){
if(e>*L.rbegin()) R.emplace(e);
else{
mn+=*L.rbegin()-e;
L.emplace(e), R.emplace(*L.rbegin()), L.erase(prev(L.end()));
}
}
mn+=o.mn;
return *this;
}
void clear(){L.clear(),R.clear();}
int sz(){return L.size()+R.size();}
}t[300005];
vector<int> adj[300005];
int p[300005],big[300005];
ll c[300005];
int main(){
ios::sync_with_stdio(false); cin.tie(0);
int n,m; cin>>n>>m;
for(int i=2;i<=n+m;++i) cin>>p[i]>>c[i], adj[p[i]].emplace_back(i);
for(int i=n+m;i>n;--i) t[i].insert(0);
for(int i=n+m;i>=1;--i){
if(i<=n) swap(t[i],t[big[i]]);
for(auto &v:adj[i]){
if(v==big[i]) continue;
t[i]+=t[v];
t[v].clear();
}
if(i==1) break;
while(t[i].R.size()>1){
auto itr=prev(t[i].R.end());
t[i].R.erase(itr);
}
auto itr1=prev(t[i].L.end()), itr2=t[i].R.begin();
t[i].L.emplace(*itr1+c[i]), t[i].L.erase(itr1);
t[i].R.emplace(*itr2+c[i]), t[i].R.erase(itr2);
if(!big[p[i]] || t[big[p[i]]].sz()<t[i].sz()) big[p[i]]=i;
}
cout<<t[1].mn;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
40284 KB |
Output is correct |
2 |
Correct |
9 ms |
40372 KB |
Output is correct |
3 |
Correct |
10 ms |
40588 KB |
Output is correct |
4 |
Correct |
9 ms |
40284 KB |
Output is correct |
5 |
Correct |
9 ms |
40284 KB |
Output is correct |
6 |
Correct |
9 ms |
40284 KB |
Output is correct |
7 |
Correct |
10 ms |
40284 KB |
Output is correct |
8 |
Correct |
8 ms |
40284 KB |
Output is correct |
9 |
Correct |
8 ms |
40372 KB |
Output is correct |
10 |
Correct |
9 ms |
40380 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
40284 KB |
Output is correct |
2 |
Correct |
9 ms |
40500 KB |
Output is correct |
3 |
Correct |
9 ms |
40284 KB |
Output is correct |
4 |
Correct |
9 ms |
40284 KB |
Output is correct |
5 |
Correct |
8 ms |
40284 KB |
Output is correct |
6 |
Correct |
9 ms |
40280 KB |
Output is correct |
7 |
Correct |
8 ms |
40284 KB |
Output is correct |
8 |
Correct |
8 ms |
40404 KB |
Output is correct |
9 |
Correct |
9 ms |
40284 KB |
Output is correct |
10 |
Correct |
8 ms |
40396 KB |
Output is correct |
11 |
Correct |
9 ms |
40284 KB |
Output is correct |
12 |
Correct |
8 ms |
40420 KB |
Output is correct |
13 |
Correct |
8 ms |
40284 KB |
Output is correct |
14 |
Correct |
9 ms |
40284 KB |
Output is correct |
15 |
Correct |
9 ms |
40284 KB |
Output is correct |
16 |
Correct |
9 ms |
40292 KB |
Output is correct |
17 |
Correct |
10 ms |
40292 KB |
Output is correct |
18 |
Correct |
9 ms |
40296 KB |
Output is correct |
19 |
Correct |
10 ms |
40520 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
40284 KB |
Output is correct |
2 |
Correct |
9 ms |
40372 KB |
Output is correct |
3 |
Correct |
10 ms |
40588 KB |
Output is correct |
4 |
Correct |
9 ms |
40284 KB |
Output is correct |
5 |
Correct |
9 ms |
40284 KB |
Output is correct |
6 |
Correct |
9 ms |
40284 KB |
Output is correct |
7 |
Correct |
10 ms |
40284 KB |
Output is correct |
8 |
Correct |
8 ms |
40284 KB |
Output is correct |
9 |
Correct |
8 ms |
40372 KB |
Output is correct |
10 |
Correct |
9 ms |
40380 KB |
Output is correct |
11 |
Correct |
10 ms |
40284 KB |
Output is correct |
12 |
Correct |
9 ms |
40500 KB |
Output is correct |
13 |
Correct |
9 ms |
40284 KB |
Output is correct |
14 |
Correct |
9 ms |
40284 KB |
Output is correct |
15 |
Correct |
8 ms |
40284 KB |
Output is correct |
16 |
Correct |
9 ms |
40280 KB |
Output is correct |
17 |
Correct |
8 ms |
40284 KB |
Output is correct |
18 |
Correct |
8 ms |
40404 KB |
Output is correct |
19 |
Correct |
9 ms |
40284 KB |
Output is correct |
20 |
Correct |
8 ms |
40396 KB |
Output is correct |
21 |
Correct |
9 ms |
40284 KB |
Output is correct |
22 |
Correct |
8 ms |
40420 KB |
Output is correct |
23 |
Correct |
8 ms |
40284 KB |
Output is correct |
24 |
Correct |
9 ms |
40284 KB |
Output is correct |
25 |
Correct |
9 ms |
40284 KB |
Output is correct |
26 |
Correct |
9 ms |
40292 KB |
Output is correct |
27 |
Correct |
10 ms |
40292 KB |
Output is correct |
28 |
Correct |
9 ms |
40296 KB |
Output is correct |
29 |
Correct |
10 ms |
40520 KB |
Output is correct |
30 |
Correct |
9 ms |
40288 KB |
Output is correct |
31 |
Correct |
9 ms |
40380 KB |
Output is correct |
32 |
Correct |
9 ms |
40612 KB |
Output is correct |
33 |
Correct |
9 ms |
40548 KB |
Output is correct |
34 |
Correct |
9 ms |
40540 KB |
Output is correct |
35 |
Correct |
10 ms |
40540 KB |
Output is correct |
36 |
Correct |
11 ms |
40536 KB |
Output is correct |
37 |
Correct |
11 ms |
40796 KB |
Output is correct |
38 |
Correct |
11 ms |
40796 KB |
Output is correct |
39 |
Correct |
12 ms |
40796 KB |
Output is correct |
40 |
Correct |
10 ms |
40668 KB |
Output is correct |
41 |
Correct |
9 ms |
40540 KB |
Output is correct |
42 |
Correct |
10 ms |
40796 KB |
Output is correct |
43 |
Correct |
12 ms |
40796 KB |
Output is correct |
44 |
Correct |
11 ms |
40796 KB |
Output is correct |
45 |
Correct |
11 ms |
40788 KB |
Output is correct |
46 |
Correct |
12 ms |
40796 KB |
Output is correct |
47 |
Correct |
12 ms |
41012 KB |
Output is correct |
48 |
Correct |
12 ms |
40792 KB |
Output is correct |
49 |
Correct |
11 ms |
40796 KB |
Output is correct |
50 |
Correct |
13 ms |
40796 KB |
Output is correct |
51 |
Correct |
13 ms |
40796 KB |
Output is correct |
52 |
Correct |
12 ms |
40796 KB |
Output is correct |
53 |
Correct |
12 ms |
40796 KB |
Output is correct |
54 |
Correct |
11 ms |
40900 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
40284 KB |
Output is correct |
2 |
Correct |
9 ms |
40372 KB |
Output is correct |
3 |
Correct |
10 ms |
40588 KB |
Output is correct |
4 |
Correct |
9 ms |
40284 KB |
Output is correct |
5 |
Correct |
9 ms |
40284 KB |
Output is correct |
6 |
Correct |
9 ms |
40284 KB |
Output is correct |
7 |
Correct |
10 ms |
40284 KB |
Output is correct |
8 |
Correct |
8 ms |
40284 KB |
Output is correct |
9 |
Correct |
8 ms |
40372 KB |
Output is correct |
10 |
Correct |
9 ms |
40380 KB |
Output is correct |
11 |
Correct |
10 ms |
40284 KB |
Output is correct |
12 |
Correct |
9 ms |
40500 KB |
Output is correct |
13 |
Correct |
9 ms |
40284 KB |
Output is correct |
14 |
Correct |
9 ms |
40284 KB |
Output is correct |
15 |
Correct |
8 ms |
40284 KB |
Output is correct |
16 |
Correct |
9 ms |
40280 KB |
Output is correct |
17 |
Correct |
8 ms |
40284 KB |
Output is correct |
18 |
Correct |
8 ms |
40404 KB |
Output is correct |
19 |
Correct |
9 ms |
40284 KB |
Output is correct |
20 |
Correct |
8 ms |
40396 KB |
Output is correct |
21 |
Correct |
9 ms |
40284 KB |
Output is correct |
22 |
Correct |
8 ms |
40420 KB |
Output is correct |
23 |
Correct |
8 ms |
40284 KB |
Output is correct |
24 |
Correct |
9 ms |
40284 KB |
Output is correct |
25 |
Correct |
9 ms |
40284 KB |
Output is correct |
26 |
Correct |
9 ms |
40292 KB |
Output is correct |
27 |
Correct |
10 ms |
40292 KB |
Output is correct |
28 |
Correct |
9 ms |
40296 KB |
Output is correct |
29 |
Correct |
10 ms |
40520 KB |
Output is correct |
30 |
Correct |
9 ms |
40288 KB |
Output is correct |
31 |
Correct |
9 ms |
40380 KB |
Output is correct |
32 |
Correct |
9 ms |
40612 KB |
Output is correct |
33 |
Correct |
9 ms |
40548 KB |
Output is correct |
34 |
Correct |
9 ms |
40540 KB |
Output is correct |
35 |
Correct |
10 ms |
40540 KB |
Output is correct |
36 |
Correct |
11 ms |
40536 KB |
Output is correct |
37 |
Correct |
11 ms |
40796 KB |
Output is correct |
38 |
Correct |
11 ms |
40796 KB |
Output is correct |
39 |
Correct |
12 ms |
40796 KB |
Output is correct |
40 |
Correct |
10 ms |
40668 KB |
Output is correct |
41 |
Correct |
9 ms |
40540 KB |
Output is correct |
42 |
Correct |
10 ms |
40796 KB |
Output is correct |
43 |
Correct |
12 ms |
40796 KB |
Output is correct |
44 |
Correct |
11 ms |
40796 KB |
Output is correct |
45 |
Correct |
11 ms |
40788 KB |
Output is correct |
46 |
Correct |
12 ms |
40796 KB |
Output is correct |
47 |
Correct |
12 ms |
41012 KB |
Output is correct |
48 |
Correct |
12 ms |
40792 KB |
Output is correct |
49 |
Correct |
11 ms |
40796 KB |
Output is correct |
50 |
Correct |
13 ms |
40796 KB |
Output is correct |
51 |
Correct |
13 ms |
40796 KB |
Output is correct |
52 |
Correct |
12 ms |
40796 KB |
Output is correct |
53 |
Correct |
12 ms |
40796 KB |
Output is correct |
54 |
Correct |
11 ms |
40900 KB |
Output is correct |
55 |
Correct |
15 ms |
41312 KB |
Output is correct |
56 |
Correct |
42 ms |
45148 KB |
Output is correct |
57 |
Correct |
67 ms |
47292 KB |
Output is correct |
58 |
Correct |
83 ms |
48532 KB |
Output is correct |
59 |
Correct |
119 ms |
50492 KB |
Output is correct |
60 |
Correct |
148 ms |
52396 KB |
Output is correct |
61 |
Correct |
172 ms |
53552 KB |
Output is correct |
62 |
Correct |
191 ms |
54868 KB |
Output is correct |
63 |
Correct |
236 ms |
57188 KB |
Output is correct |
64 |
Correct |
255 ms |
58020 KB |
Output is correct |
65 |
Correct |
93 ms |
51992 KB |
Output is correct |
66 |
Correct |
85 ms |
51940 KB |
Output is correct |
67 |
Correct |
94 ms |
52028 KB |
Output is correct |
68 |
Correct |
152 ms |
61300 KB |
Output is correct |
69 |
Correct |
199 ms |
61636 KB |
Output is correct |
70 |
Correct |
202 ms |
61504 KB |
Output is correct |
71 |
Correct |
343 ms |
72356 KB |
Output is correct |
72 |
Correct |
336 ms |
72208 KB |
Output is correct |
73 |
Correct |
287 ms |
72388 KB |
Output is correct |
74 |
Correct |
285 ms |
72412 KB |
Output is correct |
75 |
Correct |
280 ms |
72232 KB |
Output is correct |
76 |
Correct |
316 ms |
72272 KB |
Output is correct |
77 |
Correct |
312 ms |
71376 KB |
Output is correct |
78 |
Correct |
362 ms |
71340 KB |
Output is correct |
79 |
Correct |
402 ms |
71932 KB |
Output is correct |