#include<bits/stdc++.h>//I love Kagamine Rin forever!
#define ll long long
using namespace std;//() is so cute!
struct lmt
{
ll v[600005];
int lc[600005],rc[600005],dist[600005];
lmt(){dist[0]=-1;}
int merge(int x,int y)
{
if(!x||!y)return x+y;
if(v[y]>v[x])swap(x,y);
rc[x]=merge(rc[x],y);
if(dist[lc[x]]<dist[rc[x]])swap(lc[x],rc[x]);
dist[x]=dist[rc[x]]+1;
return x;
}
int pop(int x)
{
return merge(lc[x],rc[x]);
}
}akari;
int sz;
int rt[600005];
int n,m;
int fa[600005],c[600005];
int deg[600005];
ll ans;
int main()
{
scanf("%d%d",&n,&m);
for(int i=2;i<=n+m;i++)
{
scanf("%d%d",&fa[i],&c[i]);
deg[fa[i]]++;
ans+=c[i];
}
for(int i=n+m;i>=2;i--)
{
ll l=0,r=0;
if(i<=n)
{
while(--deg[i])rt[i]=akari.pop(rt[i]);
r=akari.v[rt[i]],rt[i]=akari.pop(rt[i]);
l=akari.v[rt[i]],rt[i]=akari.pop(rt[i]);
}
akari.v[++sz]=l+c[i],akari.v[++sz]=r+c[i];
rt[i]=akari.merge(rt[i],akari.merge(sz,sz-1));
rt[fa[i]]=akari.merge(rt[fa[i]],rt[i]);
}
while(deg[1]--)rt[1]=akari.pop(rt[1]);
while(rt[1])ans-=akari.v[rt[1]],rt[1]=akari.pop(rt[1]);
printf("%lld\n",ans);
return 0;
}
Compilation message
fireworks.cpp: In function 'int main()':
fireworks.cpp:32:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
32 | scanf("%d%d",&n,&m);
| ~~~~~^~~~~~~~~~~~~~
fireworks.cpp:35:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
35 | scanf("%d%d",&fa[i],&c[i]);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
456 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
456 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
604 KB |
Output is correct |
33 |
Correct |
2 ms |
600 KB |
Output is correct |
34 |
Correct |
1 ms |
604 KB |
Output is correct |
35 |
Correct |
1 ms |
604 KB |
Output is correct |
36 |
Correct |
1 ms |
724 KB |
Output is correct |
37 |
Correct |
1 ms |
600 KB |
Output is correct |
38 |
Correct |
1 ms |
604 KB |
Output is correct |
39 |
Correct |
1 ms |
604 KB |
Output is correct |
40 |
Correct |
1 ms |
604 KB |
Output is correct |
41 |
Correct |
1 ms |
604 KB |
Output is correct |
42 |
Correct |
1 ms |
724 KB |
Output is correct |
43 |
Correct |
2 ms |
600 KB |
Output is correct |
44 |
Correct |
2 ms |
600 KB |
Output is correct |
45 |
Correct |
2 ms |
604 KB |
Output is correct |
46 |
Correct |
2 ms |
604 KB |
Output is correct |
47 |
Correct |
2 ms |
604 KB |
Output is correct |
48 |
Correct |
2 ms |
776 KB |
Output is correct |
49 |
Correct |
2 ms |
604 KB |
Output is correct |
50 |
Correct |
2 ms |
604 KB |
Output is correct |
51 |
Correct |
2 ms |
604 KB |
Output is correct |
52 |
Correct |
2 ms |
600 KB |
Output is correct |
53 |
Correct |
2 ms |
856 KB |
Output is correct |
54 |
Correct |
2 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
456 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
604 KB |
Output is correct |
33 |
Correct |
2 ms |
600 KB |
Output is correct |
34 |
Correct |
1 ms |
604 KB |
Output is correct |
35 |
Correct |
1 ms |
604 KB |
Output is correct |
36 |
Correct |
1 ms |
724 KB |
Output is correct |
37 |
Correct |
1 ms |
600 KB |
Output is correct |
38 |
Correct |
1 ms |
604 KB |
Output is correct |
39 |
Correct |
1 ms |
604 KB |
Output is correct |
40 |
Correct |
1 ms |
604 KB |
Output is correct |
41 |
Correct |
1 ms |
604 KB |
Output is correct |
42 |
Correct |
1 ms |
724 KB |
Output is correct |
43 |
Correct |
2 ms |
600 KB |
Output is correct |
44 |
Correct |
2 ms |
600 KB |
Output is correct |
45 |
Correct |
2 ms |
604 KB |
Output is correct |
46 |
Correct |
2 ms |
604 KB |
Output is correct |
47 |
Correct |
2 ms |
604 KB |
Output is correct |
48 |
Correct |
2 ms |
776 KB |
Output is correct |
49 |
Correct |
2 ms |
604 KB |
Output is correct |
50 |
Correct |
2 ms |
604 KB |
Output is correct |
51 |
Correct |
2 ms |
604 KB |
Output is correct |
52 |
Correct |
2 ms |
600 KB |
Output is correct |
53 |
Correct |
2 ms |
856 KB |
Output is correct |
54 |
Correct |
2 ms |
604 KB |
Output is correct |
55 |
Correct |
4 ms |
1372 KB |
Output is correct |
56 |
Correct |
13 ms |
3676 KB |
Output is correct |
57 |
Correct |
22 ms |
6236 KB |
Output is correct |
58 |
Correct |
30 ms |
7760 KB |
Output is correct |
59 |
Correct |
42 ms |
10320 KB |
Output is correct |
60 |
Correct |
49 ms |
12632 KB |
Output is correct |
61 |
Correct |
57 ms |
14352 KB |
Output is correct |
62 |
Correct |
63 ms |
15696 KB |
Output is correct |
63 |
Correct |
86 ms |
18768 KB |
Output is correct |
64 |
Correct |
83 ms |
19796 KB |
Output is correct |
65 |
Correct |
54 ms |
21612 KB |
Output is correct |
66 |
Correct |
51 ms |
21588 KB |
Output is correct |
67 |
Correct |
47 ms |
21588 KB |
Output is correct |
68 |
Correct |
90 ms |
20900 KB |
Output is correct |
69 |
Correct |
88 ms |
20816 KB |
Output is correct |
70 |
Correct |
96 ms |
20844 KB |
Output is correct |
71 |
Correct |
163 ms |
19968 KB |
Output is correct |
72 |
Correct |
143 ms |
19752 KB |
Output is correct |
73 |
Correct |
151 ms |
19536 KB |
Output is correct |
74 |
Correct |
178 ms |
19672 KB |
Output is correct |
75 |
Correct |
160 ms |
19280 KB |
Output is correct |
76 |
Correct |
153 ms |
19280 KB |
Output is correct |
77 |
Correct |
201 ms |
19280 KB |
Output is correct |
78 |
Correct |
193 ms |
19280 KB |
Output is correct |
79 |
Correct |
224 ms |
19152 KB |
Output is correct |