#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 |
2 ms |
12632 KB |
Output is correct |
2 |
Correct |
3 ms |
12636 KB |
Output is correct |
3 |
Correct |
2 ms |
12636 KB |
Output is correct |
4 |
Correct |
2 ms |
12636 KB |
Output is correct |
5 |
Correct |
2 ms |
12724 KB |
Output is correct |
6 |
Correct |
2 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 |
2 ms |
12636 KB |
Output is correct |
10 |
Correct |
3 ms |
12636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
12636 KB |
Output is correct |
2 |
Correct |
2 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 |
12724 KB |
Output is correct |
6 |
Correct |
2 ms |
12636 KB |
Output is correct |
7 |
Correct |
2 ms |
12636 KB |
Output is correct |
8 |
Correct |
2 ms |
12636 KB |
Output is correct |
9 |
Correct |
2 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 |
2 ms |
12636 KB |
Output is correct |
14 |
Correct |
2 ms |
12636 KB |
Output is correct |
15 |
Correct |
2 ms |
12640 KB |
Output is correct |
16 |
Correct |
2 ms |
12644 KB |
Output is correct |
17 |
Correct |
2 ms |
12640 KB |
Output is correct |
18 |
Correct |
2 ms |
12636 KB |
Output is correct |
19 |
Correct |
3 ms |
12636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
12632 KB |
Output is correct |
2 |
Correct |
3 ms |
12636 KB |
Output is correct |
3 |
Correct |
2 ms |
12636 KB |
Output is correct |
4 |
Correct |
2 ms |
12636 KB |
Output is correct |
5 |
Correct |
2 ms |
12724 KB |
Output is correct |
6 |
Correct |
2 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 |
2 ms |
12636 KB |
Output is correct |
10 |
Correct |
3 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 |
2 ms |
12636 KB |
Output is correct |
14 |
Correct |
3 ms |
12636 KB |
Output is correct |
15 |
Correct |
2 ms |
12724 KB |
Output is correct |
16 |
Correct |
2 ms |
12636 KB |
Output is correct |
17 |
Correct |
2 ms |
12636 KB |
Output is correct |
18 |
Correct |
2 ms |
12636 KB |
Output is correct |
19 |
Correct |
2 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 |
2 ms |
12636 KB |
Output is correct |
24 |
Correct |
2 ms |
12636 KB |
Output is correct |
25 |
Correct |
2 ms |
12640 KB |
Output is correct |
26 |
Correct |
2 ms |
12644 KB |
Output is correct |
27 |
Correct |
2 ms |
12640 KB |
Output is correct |
28 |
Correct |
2 ms |
12636 KB |
Output is correct |
29 |
Correct |
3 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 |
4 ms |
12632 KB |
Output is correct |
34 |
Correct |
3 ms |
12832 KB |
Output is correct |
35 |
Correct |
4 ms |
12892 KB |
Output is correct |
36 |
Correct |
4 ms |
12888 KB |
Output is correct |
37 |
Correct |
4 ms |
12892 KB |
Output is correct |
38 |
Correct |
4 ms |
12892 KB |
Output is correct |
39 |
Correct |
4 ms |
12892 KB |
Output is correct |
40 |
Correct |
4 ms |
13404 KB |
Output is correct |
41 |
Correct |
4 ms |
13404 KB |
Output is correct |
42 |
Correct |
3 ms |
12636 KB |
Output is correct |
43 |
Correct |
5 ms |
13148 KB |
Output is correct |
44 |
Correct |
4 ms |
13148 KB |
Output is correct |
45 |
Correct |
5 ms |
13148 KB |
Output is correct |
46 |
Correct |
4 ms |
13148 KB |
Output is correct |
47 |
Correct |
5 ms |
13152 KB |
Output is correct |
48 |
Correct |
4 ms |
12892 KB |
Output is correct |
49 |
Correct |
4 ms |
13148 KB |
Output is correct |
50 |
Correct |
4 ms |
12892 KB |
Output is correct |
51 |
Correct |
4 ms |
12892 KB |
Output is correct |
52 |
Correct |
4 ms |
12892 KB |
Output is correct |
53 |
Correct |
5 ms |
12892 KB |
Output is correct |
54 |
Correct |
4 ms |
13148 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
12632 KB |
Output is correct |
2 |
Correct |
3 ms |
12636 KB |
Output is correct |
3 |
Correct |
2 ms |
12636 KB |
Output is correct |
4 |
Correct |
2 ms |
12636 KB |
Output is correct |
5 |
Correct |
2 ms |
12724 KB |
Output is correct |
6 |
Correct |
2 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 |
2 ms |
12636 KB |
Output is correct |
10 |
Correct |
3 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 |
2 ms |
12636 KB |
Output is correct |
14 |
Correct |
3 ms |
12636 KB |
Output is correct |
15 |
Correct |
2 ms |
12724 KB |
Output is correct |
16 |
Correct |
2 ms |
12636 KB |
Output is correct |
17 |
Correct |
2 ms |
12636 KB |
Output is correct |
18 |
Correct |
2 ms |
12636 KB |
Output is correct |
19 |
Correct |
2 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 |
2 ms |
12636 KB |
Output is correct |
24 |
Correct |
2 ms |
12636 KB |
Output is correct |
25 |
Correct |
2 ms |
12640 KB |
Output is correct |
26 |
Correct |
2 ms |
12644 KB |
Output is correct |
27 |
Correct |
2 ms |
12640 KB |
Output is correct |
28 |
Correct |
2 ms |
12636 KB |
Output is correct |
29 |
Correct |
3 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 |
4 ms |
12632 KB |
Output is correct |
34 |
Correct |
3 ms |
12832 KB |
Output is correct |
35 |
Correct |
4 ms |
12892 KB |
Output is correct |
36 |
Correct |
4 ms |
12888 KB |
Output is correct |
37 |
Correct |
4 ms |
12892 KB |
Output is correct |
38 |
Correct |
4 ms |
12892 KB |
Output is correct |
39 |
Correct |
4 ms |
12892 KB |
Output is correct |
40 |
Correct |
4 ms |
13404 KB |
Output is correct |
41 |
Correct |
4 ms |
13404 KB |
Output is correct |
42 |
Correct |
3 ms |
12636 KB |
Output is correct |
43 |
Correct |
5 ms |
13148 KB |
Output is correct |
44 |
Correct |
4 ms |
13148 KB |
Output is correct |
45 |
Correct |
5 ms |
13148 KB |
Output is correct |
46 |
Correct |
4 ms |
13148 KB |
Output is correct |
47 |
Correct |
5 ms |
13152 KB |
Output is correct |
48 |
Correct |
4 ms |
12892 KB |
Output is correct |
49 |
Correct |
4 ms |
13148 KB |
Output is correct |
50 |
Correct |
4 ms |
12892 KB |
Output is correct |
51 |
Correct |
4 ms |
12892 KB |
Output is correct |
52 |
Correct |
4 ms |
12892 KB |
Output is correct |
53 |
Correct |
5 ms |
12892 KB |
Output is correct |
54 |
Correct |
4 ms |
13148 KB |
Output is correct |
55 |
Correct |
7 ms |
13404 KB |
Output is correct |
56 |
Correct |
22 ms |
17244 KB |
Output is correct |
57 |
Correct |
35 ms |
18476 KB |
Output is correct |
58 |
Correct |
44 ms |
19652 KB |
Output is correct |
59 |
Correct |
59 ms |
22636 KB |
Output is correct |
60 |
Correct |
81 ms |
23504 KB |
Output is correct |
61 |
Correct |
87 ms |
24780 KB |
Output is correct |
62 |
Correct |
98 ms |
25432 KB |
Output is correct |
63 |
Correct |
128 ms |
26956 KB |
Output is correct |
64 |
Correct |
135 ms |
26960 KB |
Output is correct |
65 |
Correct |
100 ms |
66136 KB |
Output is correct |
66 |
Correct |
82 ms |
66128 KB |
Output is correct |
67 |
Correct |
72 ms |
19308 KB |
Output is correct |
68 |
Correct |
116 ms |
48768 KB |
Output is correct |
69 |
Correct |
120 ms |
45544 KB |
Output is correct |
70 |
Correct |
121 ms |
45516 KB |
Output is correct |
71 |
Correct |
138 ms |
40384 KB |
Output is correct |
72 |
Correct |
136 ms |
40344 KB |
Output is correct |
73 |
Correct |
115 ms |
37052 KB |
Output is correct |
74 |
Correct |
122 ms |
40128 KB |
Output is correct |
75 |
Correct |
121 ms |
35264 KB |
Output is correct |
76 |
Correct |
125 ms |
36456 KB |
Output is correct |
77 |
Correct |
128 ms |
35908 KB |
Output is correct |
78 |
Correct |
130 ms |
36804 KB |
Output is correct |
79 |
Correct |
114 ms |
35980 KB |
Output is correct |