#include<bits/stdc++.h>
#define MAXN 300010
#define int long long
using namespace std;
int n,m,ans;
int fa[MAXN],w[MAXN],deg[MAXN];
int val[MAXN<<1],d[MAXN<<1],rt[MAXN],tot;
int lc[MAXN<<1],rc[MAXN<<1];
int create(int v){
val[++tot]=v;d[tot]=0;
return tot;
}
int merge(int p,int q){
if(!p||!q)return p+q;
if(val[p]<val[q])swap(p,q);
rc[p]=merge(rc[p],q);
if(d[lc[p]]<d[rc[p]])swap(lc[p],rc[p]);
d[p]=d[rc[p]]+1;return p;
}
void pop(int &p){p=merge(lc[p],rc[p]);}
signed main(){
scanf("%lld%lld",&n,&m);
for(int i=2;i<=n+m;i++){
scanf("%lld%lld",&fa[i],&w[i]);
ans+=w[i];deg[fa[i]]++;
}
for(int u=n+m;u>=2;u--){
if(u<=n)while(deg[u]-->1)pop(rt[u]);
int R=val[rt[u]];pop(rt[u]);
int L=val[rt[u]];pop(rt[u]);
rt[u]=merge(rt[u],merge(create(L+w[u]),create(R+w[u])));
rt[fa[u]]=merge(rt[fa[u]],rt[u]);
}
while(deg[1]--)pop(rt[1]);
while(rt[1])ans-=val[rt[1]],pop(rt[1]);
printf("%lld",ans);
return 0;
}
Compilation message
fireworks.cpp: In function 'int main()':
fireworks.cpp:22:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
22 | scanf("%lld%lld",&n,&m);
| ~~~~~^~~~~~~~~~~~~~~~~~
fireworks.cpp:24:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
24 | scanf("%lld%lld",&fa[i],&w[i]);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
312 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
312 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
308 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
308 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
312 KB |
Output is correct |
6 |
Correct |
1 ms |
312 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
304 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
316 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
312 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
312 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
312 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
308 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
308 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
312 KB |
Output is correct |
16 |
Correct |
1 ms |
312 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
304 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
316 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
0 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
312 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
2 ms |
468 KB |
Output is correct |
32 |
Correct |
1 ms |
468 KB |
Output is correct |
33 |
Correct |
2 ms |
596 KB |
Output is correct |
34 |
Correct |
2 ms |
608 KB |
Output is correct |
35 |
Correct |
2 ms |
724 KB |
Output is correct |
36 |
Correct |
3 ms |
724 KB |
Output is correct |
37 |
Correct |
2 ms |
836 KB |
Output is correct |
38 |
Correct |
2 ms |
852 KB |
Output is correct |
39 |
Correct |
2 ms |
852 KB |
Output is correct |
40 |
Correct |
2 ms |
708 KB |
Output is correct |
41 |
Correct |
3 ms |
724 KB |
Output is correct |
42 |
Correct |
3 ms |
852 KB |
Output is correct |
43 |
Correct |
2 ms |
852 KB |
Output is correct |
44 |
Correct |
3 ms |
852 KB |
Output is correct |
45 |
Correct |
2 ms |
852 KB |
Output is correct |
46 |
Correct |
5 ms |
808 KB |
Output is correct |
47 |
Correct |
4 ms |
852 KB |
Output is correct |
48 |
Correct |
3 ms |
724 KB |
Output is correct |
49 |
Correct |
3 ms |
852 KB |
Output is correct |
50 |
Correct |
3 ms |
724 KB |
Output is correct |
51 |
Correct |
3 ms |
780 KB |
Output is correct |
52 |
Correct |
3 ms |
724 KB |
Output is correct |
53 |
Correct |
3 ms |
836 KB |
Output is correct |
54 |
Correct |
4 ms |
832 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
312 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
312 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
308 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
308 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
312 KB |
Output is correct |
16 |
Correct |
1 ms |
312 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
304 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
316 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
0 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
312 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
2 ms |
468 KB |
Output is correct |
32 |
Correct |
1 ms |
468 KB |
Output is correct |
33 |
Correct |
2 ms |
596 KB |
Output is correct |
34 |
Correct |
2 ms |
608 KB |
Output is correct |
35 |
Correct |
2 ms |
724 KB |
Output is correct |
36 |
Correct |
3 ms |
724 KB |
Output is correct |
37 |
Correct |
2 ms |
836 KB |
Output is correct |
38 |
Correct |
2 ms |
852 KB |
Output is correct |
39 |
Correct |
2 ms |
852 KB |
Output is correct |
40 |
Correct |
2 ms |
708 KB |
Output is correct |
41 |
Correct |
3 ms |
724 KB |
Output is correct |
42 |
Correct |
3 ms |
852 KB |
Output is correct |
43 |
Correct |
2 ms |
852 KB |
Output is correct |
44 |
Correct |
3 ms |
852 KB |
Output is correct |
45 |
Correct |
2 ms |
852 KB |
Output is correct |
46 |
Correct |
5 ms |
808 KB |
Output is correct |
47 |
Correct |
4 ms |
852 KB |
Output is correct |
48 |
Correct |
3 ms |
724 KB |
Output is correct |
49 |
Correct |
3 ms |
852 KB |
Output is correct |
50 |
Correct |
3 ms |
724 KB |
Output is correct |
51 |
Correct |
3 ms |
780 KB |
Output is correct |
52 |
Correct |
3 ms |
724 KB |
Output is correct |
53 |
Correct |
3 ms |
836 KB |
Output is correct |
54 |
Correct |
4 ms |
832 KB |
Output is correct |
55 |
Correct |
5 ms |
1748 KB |
Output is correct |
56 |
Correct |
19 ms |
5484 KB |
Output is correct |
57 |
Correct |
50 ms |
9416 KB |
Output is correct |
58 |
Correct |
47 ms |
11740 KB |
Output is correct |
59 |
Correct |
65 ms |
15464 KB |
Output is correct |
60 |
Correct |
85 ms |
19052 KB |
Output is correct |
61 |
Correct |
108 ms |
21408 KB |
Output is correct |
62 |
Correct |
125 ms |
23248 KB |
Output is correct |
63 |
Correct |
138 ms |
27152 KB |
Output is correct |
64 |
Correct |
157 ms |
28612 KB |
Output is correct |
65 |
Correct |
77 ms |
26292 KB |
Output is correct |
66 |
Correct |
79 ms |
26256 KB |
Output is correct |
67 |
Correct |
86 ms |
28912 KB |
Output is correct |
68 |
Correct |
152 ms |
29868 KB |
Output is correct |
69 |
Correct |
175 ms |
29808 KB |
Output is correct |
70 |
Correct |
163 ms |
29788 KB |
Output is correct |
71 |
Correct |
244 ms |
28704 KB |
Output is correct |
72 |
Correct |
261 ms |
28640 KB |
Output is correct |
73 |
Correct |
282 ms |
28696 KB |
Output is correct |
74 |
Correct |
267 ms |
28800 KB |
Output is correct |
75 |
Correct |
269 ms |
28516 KB |
Output is correct |
76 |
Correct |
289 ms |
28708 KB |
Output is correct |
77 |
Correct |
313 ms |
28096 KB |
Output is correct |
78 |
Correct |
336 ms |
28128 KB |
Output is correct |
79 |
Correct |
479 ms |
28680 KB |
Output is correct |