#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 |
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 |
6492 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 |
6492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6552 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
2 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 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 |
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 |
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 |
# |
결과 |
실행 시간 |
메모리 |
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 |
6492 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 |
6492 KB |
Output is correct |
11 |
Correct |
1 ms |
6552 KB |
Output is correct |
12 |
Correct |
1 ms |
6492 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
2 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 |
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 |
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 |
6492 KB |
Output is correct |
27 |
Correct |
1 ms |
6492 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 |
6492 KB |
Output is correct |
33 |
Correct |
1 ms |
6748 KB |
Output is correct |
34 |
Correct |
2 ms |
6748 KB |
Output is correct |
35 |
Correct |
2 ms |
6748 KB |
Output is correct |
36 |
Correct |
2 ms |
6612 KB |
Output is correct |
37 |
Correct |
3 ms |
6744 KB |
Output is correct |
38 |
Correct |
2 ms |
6748 KB |
Output is correct |
39 |
Correct |
2 ms |
6748 KB |
Output is correct |
40 |
Correct |
2 ms |
6748 KB |
Output is correct |
41 |
Correct |
3 ms |
6748 KB |
Output is correct |
42 |
Correct |
2 ms |
6748 KB |
Output is correct |
43 |
Correct |
3 ms |
6748 KB |
Output is correct |
44 |
Correct |
3 ms |
6748 KB |
Output is correct |
45 |
Correct |
3 ms |
6748 KB |
Output is correct |
46 |
Correct |
4 ms |
6748 KB |
Output is correct |
47 |
Correct |
3 ms |
6748 KB |
Output is correct |
48 |
Correct |
3 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 |
3 ms |
6880 KB |
Output is correct |
53 |
Correct |
4 ms |
6748 KB |
Output is correct |
54 |
Correct |
4 ms |
6748 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 |
6492 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 |
6492 KB |
Output is correct |
11 |
Correct |
1 ms |
6552 KB |
Output is correct |
12 |
Correct |
1 ms |
6492 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
2 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 |
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 |
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 |
6492 KB |
Output is correct |
27 |
Correct |
1 ms |
6492 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 |
6492 KB |
Output is correct |
33 |
Correct |
1 ms |
6748 KB |
Output is correct |
34 |
Correct |
2 ms |
6748 KB |
Output is correct |
35 |
Correct |
2 ms |
6748 KB |
Output is correct |
36 |
Correct |
2 ms |
6612 KB |
Output is correct |
37 |
Correct |
3 ms |
6744 KB |
Output is correct |
38 |
Correct |
2 ms |
6748 KB |
Output is correct |
39 |
Correct |
2 ms |
6748 KB |
Output is correct |
40 |
Correct |
2 ms |
6748 KB |
Output is correct |
41 |
Correct |
3 ms |
6748 KB |
Output is correct |
42 |
Correct |
2 ms |
6748 KB |
Output is correct |
43 |
Correct |
3 ms |
6748 KB |
Output is correct |
44 |
Correct |
3 ms |
6748 KB |
Output is correct |
45 |
Correct |
3 ms |
6748 KB |
Output is correct |
46 |
Correct |
4 ms |
6748 KB |
Output is correct |
47 |
Correct |
3 ms |
6748 KB |
Output is correct |
48 |
Correct |
3 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 |
3 ms |
6880 KB |
Output is correct |
53 |
Correct |
4 ms |
6748 KB |
Output is correct |
54 |
Correct |
4 ms |
6748 KB |
Output is correct |
55 |
Correct |
5 ms |
9304 KB |
Output is correct |
56 |
Correct |
17 ms |
14684 KB |
Output is correct |
57 |
Correct |
29 ms |
16240 KB |
Output is correct |
58 |
Correct |
36 ms |
16980 KB |
Output is correct |
59 |
Correct |
50 ms |
20608 KB |
Output is correct |
60 |
Correct |
62 ms |
23892 KB |
Output is correct |
61 |
Correct |
76 ms |
27984 KB |
Output is correct |
62 |
Correct |
79 ms |
29116 KB |
Output is correct |
63 |
Correct |
98 ms |
31568 KB |
Output is correct |
64 |
Correct |
102 ms |
32584 KB |
Output is correct |
65 |
Correct |
64 ms |
37960 KB |
Output is correct |
66 |
Correct |
53 ms |
38088 KB |
Output is correct |
67 |
Correct |
51 ms |
37972 KB |
Output is correct |
68 |
Correct |
123 ms |
35996 KB |
Output is correct |
69 |
Correct |
127 ms |
35744 KB |
Output is correct |
70 |
Correct |
129 ms |
35920 KB |
Output is correct |
71 |
Correct |
214 ms |
33360 KB |
Output is correct |
72 |
Correct |
213 ms |
33224 KB |
Output is correct |
73 |
Correct |
226 ms |
33216 KB |
Output is correct |
74 |
Correct |
227 ms |
33104 KB |
Output is correct |
75 |
Correct |
272 ms |
32928 KB |
Output is correct |
76 |
Correct |
286 ms |
32852 KB |
Output is correct |
77 |
Correct |
270 ms |
32704 KB |
Output is correct |
78 |
Correct |
308 ms |
32768 KB |
Output is correct |
79 |
Correct |
379 ms |
32696 KB |
Output is correct |