#include<bits/stdc++.h>
using namespace std;
#define N 5<<16
#define pop(X) rt[X]=merge(ch[rt[X]][0],ch[rt[X]][1])
#define int long long
int deg[N],P[N],C[N],rt[N],ch[N][2],v[N],CC;
mt19937 rng(9234623);
int merge(int a,int b){
if(!a+!b)
return a+b;
if(v[a]<v[b])
swap(a,b);
int x=rng()%2;
ch[a][x]=merge(ch[a][x],b);
return a;
}
signed main(){
int n,m,ans=0;
cin>>n>>m;
for(int i=2;i<=n+m;i++)
cin>>P[i]>>C[i],deg[P[i]]++,ans+=C[i];
for(int i=n+m;i>1;i--){
int l=0,r=0;
if(deg[i]){
while(--deg[i])
pop(i);
r=v[rt[i]];
pop(i);
l=v[rt[i]];
pop(i);
}
v[++CC]=l+C[i],v[++CC]=r+C[i];
rt[i]=merge(rt[i],merge(CC,CC-1));
rt[P[i]]=merge(rt[P[i]],rt[i]);
}
while(deg[1]--)
pop(1);
while(rt[1])
ans-=v[rt[1]],pop(1);
cout<<ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
2 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
2 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6488 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
2 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6572 KB |
Output is correct |
7 |
Correct |
1 ms |
6488 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
2 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
11 |
Correct |
1 ms |
6492 KB |
Output is correct |
12 |
Correct |
1 ms |
6492 KB |
Output is correct |
13 |
Correct |
2 ms |
6492 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
6744 KB |
Output is correct |
17 |
Correct |
1 ms |
6492 KB |
Output is correct |
18 |
Correct |
1 ms |
6488 KB |
Output is correct |
19 |
Correct |
2 ms |
6488 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
2 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
2 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6488 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
11 |
Correct |
1 ms |
6492 KB |
Output is correct |
12 |
Correct |
1 ms |
6492 KB |
Output is correct |
13 |
Correct |
2 ms |
6492 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
6572 KB |
Output is correct |
17 |
Correct |
1 ms |
6488 KB |
Output is correct |
18 |
Correct |
1 ms |
6492 KB |
Output is correct |
19 |
Correct |
2 ms |
6492 KB |
Output is correct |
20 |
Correct |
1 ms |
6492 KB |
Output is correct |
21 |
Correct |
1 ms |
6492 KB |
Output is correct |
22 |
Correct |
1 ms |
6492 KB |
Output is correct |
23 |
Correct |
2 ms |
6492 KB |
Output is correct |
24 |
Correct |
1 ms |
6492 KB |
Output is correct |
25 |
Correct |
1 ms |
6492 KB |
Output is correct |
26 |
Correct |
1 ms |
6744 KB |
Output is correct |
27 |
Correct |
1 ms |
6492 KB |
Output is correct |
28 |
Correct |
1 ms |
6488 KB |
Output is correct |
29 |
Correct |
2 ms |
6488 KB |
Output is correct |
30 |
Correct |
1 ms |
6492 KB |
Output is correct |
31 |
Correct |
2 ms |
6508 KB |
Output is correct |
32 |
Correct |
2 ms |
6492 KB |
Output is correct |
33 |
Correct |
3 ms |
7004 KB |
Output is correct |
34 |
Correct |
3 ms |
6748 KB |
Output is correct |
35 |
Correct |
4 ms |
7000 KB |
Output is correct |
36 |
Correct |
3 ms |
6748 KB |
Output is correct |
37 |
Correct |
4 ms |
6748 KB |
Output is correct |
38 |
Correct |
4 ms |
6748 KB |
Output is correct |
39 |
Correct |
5 ms |
6748 KB |
Output is correct |
40 |
Correct |
4 ms |
6928 KB |
Output is correct |
41 |
Correct |
3 ms |
6748 KB |
Output is correct |
42 |
Correct |
3 ms |
6748 KB |
Output is correct |
43 |
Correct |
4 ms |
6748 KB |
Output is correct |
44 |
Correct |
4 ms |
6748 KB |
Output is correct |
45 |
Correct |
4 ms |
6748 KB |
Output is correct |
46 |
Correct |
6 ms |
7512 KB |
Output is correct |
47 |
Correct |
5 ms |
6748 KB |
Output is correct |
48 |
Correct |
5 ms |
6748 KB |
Output is correct |
49 |
Correct |
5 ms |
6748 KB |
Output is correct |
50 |
Correct |
5 ms |
6748 KB |
Output is correct |
51 |
Correct |
5 ms |
6748 KB |
Output is correct |
52 |
Correct |
5 ms |
6748 KB |
Output is correct |
53 |
Correct |
5 ms |
6744 KB |
Output is correct |
54 |
Correct |
5 ms |
6916 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6492 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
2 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
2 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6488 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
11 |
Correct |
1 ms |
6492 KB |
Output is correct |
12 |
Correct |
1 ms |
6492 KB |
Output is correct |
13 |
Correct |
2 ms |
6492 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
6492 KB |
Output is correct |
16 |
Correct |
1 ms |
6572 KB |
Output is correct |
17 |
Correct |
1 ms |
6488 KB |
Output is correct |
18 |
Correct |
1 ms |
6492 KB |
Output is correct |
19 |
Correct |
2 ms |
6492 KB |
Output is correct |
20 |
Correct |
1 ms |
6492 KB |
Output is correct |
21 |
Correct |
1 ms |
6492 KB |
Output is correct |
22 |
Correct |
1 ms |
6492 KB |
Output is correct |
23 |
Correct |
2 ms |
6492 KB |
Output is correct |
24 |
Correct |
1 ms |
6492 KB |
Output is correct |
25 |
Correct |
1 ms |
6492 KB |
Output is correct |
26 |
Correct |
1 ms |
6744 KB |
Output is correct |
27 |
Correct |
1 ms |
6492 KB |
Output is correct |
28 |
Correct |
1 ms |
6488 KB |
Output is correct |
29 |
Correct |
2 ms |
6488 KB |
Output is correct |
30 |
Correct |
1 ms |
6492 KB |
Output is correct |
31 |
Correct |
2 ms |
6508 KB |
Output is correct |
32 |
Correct |
2 ms |
6492 KB |
Output is correct |
33 |
Correct |
3 ms |
7004 KB |
Output is correct |
34 |
Correct |
3 ms |
6748 KB |
Output is correct |
35 |
Correct |
4 ms |
7000 KB |
Output is correct |
36 |
Correct |
3 ms |
6748 KB |
Output is correct |
37 |
Correct |
4 ms |
6748 KB |
Output is correct |
38 |
Correct |
4 ms |
6748 KB |
Output is correct |
39 |
Correct |
5 ms |
6748 KB |
Output is correct |
40 |
Correct |
4 ms |
6928 KB |
Output is correct |
41 |
Correct |
3 ms |
6748 KB |
Output is correct |
42 |
Correct |
3 ms |
6748 KB |
Output is correct |
43 |
Correct |
4 ms |
6748 KB |
Output is correct |
44 |
Correct |
4 ms |
6748 KB |
Output is correct |
45 |
Correct |
4 ms |
6748 KB |
Output is correct |
46 |
Correct |
6 ms |
7512 KB |
Output is correct |
47 |
Correct |
5 ms |
6748 KB |
Output is correct |
48 |
Correct |
5 ms |
6748 KB |
Output is correct |
49 |
Correct |
5 ms |
6748 KB |
Output is correct |
50 |
Correct |
5 ms |
6748 KB |
Output is correct |
51 |
Correct |
5 ms |
6748 KB |
Output is correct |
52 |
Correct |
5 ms |
6748 KB |
Output is correct |
53 |
Correct |
5 ms |
6744 KB |
Output is correct |
54 |
Correct |
5 ms |
6916 KB |
Output is correct |
55 |
Correct |
9 ms |
9308 KB |
Output is correct |
56 |
Correct |
32 ms |
13500 KB |
Output is correct |
57 |
Correct |
56 ms |
15444 KB |
Output is correct |
58 |
Correct |
71 ms |
16464 KB |
Output is correct |
59 |
Correct |
97 ms |
18260 KB |
Output is correct |
60 |
Runtime error |
110 ms |
36904 KB |
Execution killed with signal 11 |
61 |
Halted |
0 ms |
0 KB |
- |