#include<bits/stdc++.h>
#define N 300001
int n,M;
int e[N][3],eo,h[N];
std::priority_queue<long long> pq[N];
long long m[N],c[N];
void link(int u,int v,int w) { e[++eo][0]=v,e[eo][1]=w,e[eo][2]=h[u],h[u]=eo; }
void dfs(int u,int p,int ci)
{
m[u]=c[u]=0;
if(u > n)
{
pq[u].push(ci);
pq[u].push(ci);
m[u]=1;
c[u]=-ci;
return;
}
for(int v,j=h[u];j;j=e[j][2])if((v=e[j][0])-p)
{
dfs(v,u,e[j][1]);
if(pq[v].size()>pq[u].size()) { std::swap(pq[v],pq[u]); std::swap(m[u],m[v]), std::swap(c[u],c[v]); }
while(pq[v].size()) pq[u].push(pq[v].top()), pq[v].pop();
m[u]+=m[v];
c[u]+=c[v];
}
if(u!=1)
{
while(m[u]>1)
{
--m[u];
c[u]+=pq[u].top();
pq[u].pop();
}
long long x=pq[u].top();
pq[u].pop();
long long x2=pq[u].top();
pq[u].pop();
pq[u].push(x+ci);
pq[u].push(x2+ci);
c[u]-=ci;
}
else
{
while(m[1]>0)
{
--m[1];
c[1]+=pq[1].top();
pq[1].pop();
}
printf("%lld",c[1]);
}
}
int main()
{
scanf("%d%d",&n,&M);
for(int p,c,i=2;i<=n+M;++i) scanf("%d%d",&p,&c),link(p,i,c);
dfs(1,1,-1);
}
Compilation message
fireworks.cpp: In function 'int main()':
fireworks.cpp:70:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
70 | scanf("%d%d",&n,&M);
| ~~~~~^~~~~~~~~~~~~~
fireworks.cpp:71:38: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
71 | for(int p,c,i=2;i<=n+M;++i) scanf("%d%d",&p,&c),link(p,i,c);
| ~~~~~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
12636 KB |
Output is correct |
2 |
Correct |
5 ms |
12892 KB |
Output is correct |
3 |
Correct |
2 ms |
12636 KB |
Output is correct |
4 |
Correct |
3 ms |
12636 KB |
Output is correct |
5 |
Correct |
2 ms |
12636 KB |
Output is correct |
6 |
Correct |
3 ms |
12636 KB |
Output is correct |
7 |
Correct |
3 ms |
12636 KB |
Output is correct |
8 |
Correct |
2 ms |
12636 KB |
Output is correct |
9 |
Correct |
3 ms |
12632 KB |
Output is correct |
10 |
Correct |
2 ms |
12636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
12636 KB |
Output is correct |
2 |
Correct |
3 ms |
12636 KB |
Output is correct |
3 |
Correct |
2 ms |
12636 KB |
Output is correct |
4 |
Correct |
3 ms |
12636 KB |
Output is correct |
5 |
Correct |
2 ms |
12636 KB |
Output is correct |
6 |
Correct |
3 ms |
12636 KB |
Output is correct |
7 |
Correct |
3 ms |
12636 KB |
Output is correct |
8 |
Correct |
3 ms |
12636 KB |
Output is correct |
9 |
Correct |
3 ms |
12636 KB |
Output is correct |
10 |
Correct |
2 ms |
12636 KB |
Output is correct |
11 |
Correct |
3 ms |
12636 KB |
Output is correct |
12 |
Correct |
2 ms |
12636 KB |
Output is correct |
13 |
Correct |
4 ms |
12748 KB |
Output is correct |
14 |
Correct |
3 ms |
12636 KB |
Output is correct |
15 |
Correct |
3 ms |
12760 KB |
Output is correct |
16 |
Correct |
3 ms |
12632 KB |
Output is correct |
17 |
Correct |
3 ms |
12636 KB |
Output is correct |
18 |
Correct |
2 ms |
12636 KB |
Output is correct |
19 |
Correct |
4 ms |
12636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
12636 KB |
Output is correct |
2 |
Correct |
5 ms |
12892 KB |
Output is correct |
3 |
Correct |
2 ms |
12636 KB |
Output is correct |
4 |
Correct |
3 ms |
12636 KB |
Output is correct |
5 |
Correct |
2 ms |
12636 KB |
Output is correct |
6 |
Correct |
3 ms |
12636 KB |
Output is correct |
7 |
Correct |
3 ms |
12636 KB |
Output is correct |
8 |
Correct |
2 ms |
12636 KB |
Output is correct |
9 |
Correct |
3 ms |
12632 KB |
Output is correct |
10 |
Correct |
2 ms |
12636 KB |
Output is correct |
11 |
Correct |
3 ms |
12636 KB |
Output is correct |
12 |
Correct |
3 ms |
12636 KB |
Output is correct |
13 |
Correct |
2 ms |
12636 KB |
Output is correct |
14 |
Correct |
3 ms |
12636 KB |
Output is correct |
15 |
Correct |
2 ms |
12636 KB |
Output is correct |
16 |
Correct |
3 ms |
12636 KB |
Output is correct |
17 |
Correct |
3 ms |
12636 KB |
Output is correct |
18 |
Correct |
3 ms |
12636 KB |
Output is correct |
19 |
Correct |
3 ms |
12636 KB |
Output is correct |
20 |
Correct |
2 ms |
12636 KB |
Output is correct |
21 |
Correct |
3 ms |
12636 KB |
Output is correct |
22 |
Correct |
2 ms |
12636 KB |
Output is correct |
23 |
Correct |
4 ms |
12748 KB |
Output is correct |
24 |
Correct |
3 ms |
12636 KB |
Output is correct |
25 |
Correct |
3 ms |
12760 KB |
Output is correct |
26 |
Correct |
3 ms |
12632 KB |
Output is correct |
27 |
Correct |
3 ms |
12636 KB |
Output is correct |
28 |
Correct |
2 ms |
12636 KB |
Output is correct |
29 |
Correct |
4 ms |
12636 KB |
Output is correct |
30 |
Correct |
3 ms |
12636 KB |
Output is correct |
31 |
Correct |
3 ms |
12636 KB |
Output is correct |
32 |
Correct |
3 ms |
12636 KB |
Output is correct |
33 |
Correct |
3 ms |
12892 KB |
Output is correct |
34 |
Correct |
3 ms |
12892 KB |
Output is correct |
35 |
Correct |
6 ms |
12764 KB |
Output is correct |
36 |
Correct |
5 ms |
12892 KB |
Output is correct |
37 |
Correct |
4 ms |
13024 KB |
Output is correct |
38 |
Correct |
4 ms |
12892 KB |
Output is correct |
39 |
Correct |
5 ms |
12988 KB |
Output is correct |
40 |
Correct |
4 ms |
13660 KB |
Output is correct |
41 |
Correct |
4 ms |
13640 KB |
Output is correct |
42 |
Correct |
4 ms |
12892 KB |
Output is correct |
43 |
Correct |
4 ms |
13404 KB |
Output is correct |
44 |
Correct |
4 ms |
13148 KB |
Output is correct |
45 |
Correct |
5 ms |
13148 KB |
Output is correct |
46 |
Correct |
5 ms |
13148 KB |
Output is correct |
47 |
Correct |
5 ms |
13148 KB |
Output is correct |
48 |
Correct |
5 ms |
13216 KB |
Output is correct |
49 |
Correct |
4 ms |
13148 KB |
Output is correct |
50 |
Correct |
5 ms |
12892 KB |
Output is correct |
51 |
Correct |
4 ms |
12892 KB |
Output is correct |
52 |
Correct |
5 ms |
13088 KB |
Output is correct |
53 |
Correct |
4 ms |
13148 KB |
Output is correct |
54 |
Correct |
4 ms |
13140 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
12636 KB |
Output is correct |
2 |
Correct |
5 ms |
12892 KB |
Output is correct |
3 |
Correct |
2 ms |
12636 KB |
Output is correct |
4 |
Correct |
3 ms |
12636 KB |
Output is correct |
5 |
Correct |
2 ms |
12636 KB |
Output is correct |
6 |
Correct |
3 ms |
12636 KB |
Output is correct |
7 |
Correct |
3 ms |
12636 KB |
Output is correct |
8 |
Correct |
2 ms |
12636 KB |
Output is correct |
9 |
Correct |
3 ms |
12632 KB |
Output is correct |
10 |
Correct |
2 ms |
12636 KB |
Output is correct |
11 |
Correct |
3 ms |
12636 KB |
Output is correct |
12 |
Correct |
3 ms |
12636 KB |
Output is correct |
13 |
Correct |
2 ms |
12636 KB |
Output is correct |
14 |
Correct |
3 ms |
12636 KB |
Output is correct |
15 |
Correct |
2 ms |
12636 KB |
Output is correct |
16 |
Correct |
3 ms |
12636 KB |
Output is correct |
17 |
Correct |
3 ms |
12636 KB |
Output is correct |
18 |
Correct |
3 ms |
12636 KB |
Output is correct |
19 |
Correct |
3 ms |
12636 KB |
Output is correct |
20 |
Correct |
2 ms |
12636 KB |
Output is correct |
21 |
Correct |
3 ms |
12636 KB |
Output is correct |
22 |
Correct |
2 ms |
12636 KB |
Output is correct |
23 |
Correct |
4 ms |
12748 KB |
Output is correct |
24 |
Correct |
3 ms |
12636 KB |
Output is correct |
25 |
Correct |
3 ms |
12760 KB |
Output is correct |
26 |
Correct |
3 ms |
12632 KB |
Output is correct |
27 |
Correct |
3 ms |
12636 KB |
Output is correct |
28 |
Correct |
2 ms |
12636 KB |
Output is correct |
29 |
Correct |
4 ms |
12636 KB |
Output is correct |
30 |
Correct |
3 ms |
12636 KB |
Output is correct |
31 |
Correct |
3 ms |
12636 KB |
Output is correct |
32 |
Correct |
3 ms |
12636 KB |
Output is correct |
33 |
Correct |
3 ms |
12892 KB |
Output is correct |
34 |
Correct |
3 ms |
12892 KB |
Output is correct |
35 |
Correct |
6 ms |
12764 KB |
Output is correct |
36 |
Correct |
5 ms |
12892 KB |
Output is correct |
37 |
Correct |
4 ms |
13024 KB |
Output is correct |
38 |
Correct |
4 ms |
12892 KB |
Output is correct |
39 |
Correct |
5 ms |
12988 KB |
Output is correct |
40 |
Correct |
4 ms |
13660 KB |
Output is correct |
41 |
Correct |
4 ms |
13640 KB |
Output is correct |
42 |
Correct |
4 ms |
12892 KB |
Output is correct |
43 |
Correct |
4 ms |
13404 KB |
Output is correct |
44 |
Correct |
4 ms |
13148 KB |
Output is correct |
45 |
Correct |
5 ms |
13148 KB |
Output is correct |
46 |
Correct |
5 ms |
13148 KB |
Output is correct |
47 |
Correct |
5 ms |
13148 KB |
Output is correct |
48 |
Correct |
5 ms |
13216 KB |
Output is correct |
49 |
Correct |
4 ms |
13148 KB |
Output is correct |
50 |
Correct |
5 ms |
12892 KB |
Output is correct |
51 |
Correct |
4 ms |
12892 KB |
Output is correct |
52 |
Correct |
5 ms |
13088 KB |
Output is correct |
53 |
Correct |
4 ms |
13148 KB |
Output is correct |
54 |
Correct |
4 ms |
13140 KB |
Output is correct |
55 |
Correct |
7 ms |
13536 KB |
Output is correct |
56 |
Correct |
23 ms |
17992 KB |
Output is correct |
57 |
Correct |
39 ms |
19780 KB |
Output is correct |
58 |
Correct |
49 ms |
21196 KB |
Output is correct |
59 |
Correct |
81 ms |
24944 KB |
Output is correct |
60 |
Correct |
89 ms |
26316 KB |
Output is correct |
61 |
Correct |
93 ms |
27852 KB |
Output is correct |
62 |
Correct |
102 ms |
29032 KB |
Output is correct |
63 |
Correct |
152 ms |
31048 KB |
Output is correct |
64 |
Correct |
147 ms |
31564 KB |
Output is correct |
65 |
Correct |
84 ms |
70940 KB |
Output is correct |
66 |
Correct |
83 ms |
70988 KB |
Output is correct |
67 |
Correct |
74 ms |
24228 KB |
Output is correct |
68 |
Correct |
119 ms |
53968 KB |
Output is correct |
69 |
Correct |
122 ms |
50276 KB |
Output is correct |
70 |
Correct |
123 ms |
50124 KB |
Output is correct |
71 |
Correct |
139 ms |
43972 KB |
Output is correct |
72 |
Correct |
141 ms |
44672 KB |
Output is correct |
73 |
Correct |
124 ms |
40788 KB |
Output is correct |
74 |
Correct |
125 ms |
44124 KB |
Output is correct |
75 |
Correct |
123 ms |
40032 KB |
Output is correct |
76 |
Correct |
127 ms |
39828 KB |
Output is correct |
77 |
Correct |
128 ms |
39472 KB |
Output is correct |
78 |
Correct |
128 ms |
40388 KB |
Output is correct |
79 |
Correct |
112 ms |
39504 KB |
Output is correct |