#include<bits/stdc++.h>
using namespace std;
#define N 5<<17
#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(){
cin.tie(0)->sync_with_stdio(0);
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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6488 KB |
Output is correct |
2 |
Correct |
2 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 |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
2 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6488 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 |
1 ms |
6612 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6488 KB |
Output is correct |
11 |
Correct |
2 ms |
6492 KB |
Output is correct |
12 |
Correct |
1 ms |
6492 KB |
Output is correct |
13 |
Correct |
1 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 |
6492 KB |
Output is correct |
17 |
Correct |
1 ms |
6488 KB |
Output is correct |
18 |
Correct |
1 ms |
6492 KB |
Output is correct |
19 |
Correct |
1 ms |
6492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6488 KB |
Output is correct |
2 |
Correct |
2 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 |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
2 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
11 |
Correct |
1 ms |
6488 KB |
Output is correct |
12 |
Correct |
1 ms |
6492 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
6612 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
1 ms |
6492 KB |
Output is correct |
18 |
Correct |
1 ms |
6492 KB |
Output is correct |
19 |
Correct |
1 ms |
6492 KB |
Output is correct |
20 |
Correct |
1 ms |
6488 KB |
Output is correct |
21 |
Correct |
2 ms |
6492 KB |
Output is correct |
22 |
Correct |
1 ms |
6492 KB |
Output is correct |
23 |
Correct |
1 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 |
6492 KB |
Output is correct |
27 |
Correct |
1 ms |
6488 KB |
Output is correct |
28 |
Correct |
1 ms |
6492 KB |
Output is correct |
29 |
Correct |
1 ms |
6492 KB |
Output is correct |
30 |
Correct |
1 ms |
6492 KB |
Output is correct |
31 |
Correct |
1 ms |
6492 KB |
Output is correct |
32 |
Correct |
2 ms |
6748 KB |
Output is correct |
33 |
Correct |
2 ms |
6744 KB |
Output is correct |
34 |
Correct |
2 ms |
6748 KB |
Output is correct |
35 |
Correct |
2 ms |
6892 KB |
Output is correct |
36 |
Correct |
3 ms |
6748 KB |
Output is correct |
37 |
Correct |
3 ms |
6744 KB |
Output is correct |
38 |
Correct |
3 ms |
6744 KB |
Output is correct |
39 |
Correct |
3 ms |
6748 KB |
Output is correct |
40 |
Correct |
2 ms |
6748 KB |
Output is correct |
41 |
Correct |
2 ms |
6748 KB |
Output is correct |
42 |
Correct |
2 ms |
6748 KB |
Output is correct |
43 |
Correct |
2 ms |
6748 KB |
Output is correct |
44 |
Correct |
3 ms |
6764 KB |
Output is correct |
45 |
Correct |
3 ms |
6748 KB |
Output is correct |
46 |
Correct |
3 ms |
6748 KB |
Output is correct |
47 |
Correct |
4 ms |
6748 KB |
Output is correct |
48 |
Correct |
4 ms |
6748 KB |
Output is correct |
49 |
Correct |
4 ms |
6748 KB |
Output is correct |
50 |
Correct |
3 ms |
6748 KB |
Output is correct |
51 |
Correct |
3 ms |
6748 KB |
Output is correct |
52 |
Correct |
4 ms |
6888 KB |
Output is correct |
53 |
Correct |
3 ms |
6956 KB |
Output is correct |
54 |
Correct |
4 ms |
6744 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
6488 KB |
Output is correct |
2 |
Correct |
2 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 |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
2 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6492 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
11 |
Correct |
1 ms |
6488 KB |
Output is correct |
12 |
Correct |
1 ms |
6492 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
1 ms |
6612 KB |
Output is correct |
16 |
Correct |
1 ms |
6492 KB |
Output is correct |
17 |
Correct |
1 ms |
6492 KB |
Output is correct |
18 |
Correct |
1 ms |
6492 KB |
Output is correct |
19 |
Correct |
1 ms |
6492 KB |
Output is correct |
20 |
Correct |
1 ms |
6488 KB |
Output is correct |
21 |
Correct |
2 ms |
6492 KB |
Output is correct |
22 |
Correct |
1 ms |
6492 KB |
Output is correct |
23 |
Correct |
1 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 |
6492 KB |
Output is correct |
27 |
Correct |
1 ms |
6488 KB |
Output is correct |
28 |
Correct |
1 ms |
6492 KB |
Output is correct |
29 |
Correct |
1 ms |
6492 KB |
Output is correct |
30 |
Correct |
1 ms |
6492 KB |
Output is correct |
31 |
Correct |
1 ms |
6492 KB |
Output is correct |
32 |
Correct |
2 ms |
6748 KB |
Output is correct |
33 |
Correct |
2 ms |
6744 KB |
Output is correct |
34 |
Correct |
2 ms |
6748 KB |
Output is correct |
35 |
Correct |
2 ms |
6892 KB |
Output is correct |
36 |
Correct |
3 ms |
6748 KB |
Output is correct |
37 |
Correct |
3 ms |
6744 KB |
Output is correct |
38 |
Correct |
3 ms |
6744 KB |
Output is correct |
39 |
Correct |
3 ms |
6748 KB |
Output is correct |
40 |
Correct |
2 ms |
6748 KB |
Output is correct |
41 |
Correct |
2 ms |
6748 KB |
Output is correct |
42 |
Correct |
2 ms |
6748 KB |
Output is correct |
43 |
Correct |
2 ms |
6748 KB |
Output is correct |
44 |
Correct |
3 ms |
6764 KB |
Output is correct |
45 |
Correct |
3 ms |
6748 KB |
Output is correct |
46 |
Correct |
3 ms |
6748 KB |
Output is correct |
47 |
Correct |
4 ms |
6748 KB |
Output is correct |
48 |
Correct |
4 ms |
6748 KB |
Output is correct |
49 |
Correct |
4 ms |
6748 KB |
Output is correct |
50 |
Correct |
3 ms |
6748 KB |
Output is correct |
51 |
Correct |
3 ms |
6748 KB |
Output is correct |
52 |
Correct |
4 ms |
6888 KB |
Output is correct |
53 |
Correct |
3 ms |
6956 KB |
Output is correct |
54 |
Correct |
4 ms |
6744 KB |
Output is correct |
55 |
Correct |
5 ms |
9564 KB |
Output is correct |
56 |
Correct |
17 ms |
15496 KB |
Output is correct |
57 |
Correct |
36 ms |
17752 KB |
Output is correct |
58 |
Correct |
38 ms |
18768 KB |
Output is correct |
59 |
Correct |
51 ms |
22800 KB |
Output is correct |
60 |
Correct |
65 ms |
26704 KB |
Output is correct |
61 |
Correct |
75 ms |
28108 KB |
Output is correct |
62 |
Correct |
84 ms |
29192 KB |
Output is correct |
63 |
Correct |
114 ms |
31688 KB |
Output is correct |
64 |
Correct |
119 ms |
32788 KB |
Output is correct |
65 |
Correct |
52 ms |
37968 KB |
Output is correct |
66 |
Correct |
52 ms |
37972 KB |
Output is correct |
67 |
Correct |
51 ms |
37972 KB |
Output is correct |
68 |
Correct |
122 ms |
35852 KB |
Output is correct |
69 |
Correct |
128 ms |
35776 KB |
Output is correct |
70 |
Correct |
124 ms |
35744 KB |
Output is correct |
71 |
Correct |
213 ms |
33364 KB |
Output is correct |
72 |
Correct |
212 ms |
33700 KB |
Output is correct |
73 |
Correct |
227 ms |
33212 KB |
Output is correct |
74 |
Correct |
236 ms |
33104 KB |
Output is correct |
75 |
Correct |
238 ms |
32852 KB |
Output is correct |
76 |
Correct |
248 ms |
32852 KB |
Output is correct |
77 |
Correct |
275 ms |
33104 KB |
Output is correct |
78 |
Correct |
280 ms |
32572 KB |
Output is correct |
79 |
Correct |
397 ms |
32700 KB |
Output is correct |