#include <stdio.h>
#define N 300000
int tt[(1 + N * 2) * 2], hh[(1 + N * 2) * 2]; char side[1 + N * 2]; long long kk[1 + N * 2];
int node(long long k) {
static int _ = 1;
kk[_] = k, side[_] = 1;
return _++;
}
void pul(int u) {
side[u] = hh[tt[u << 1 | 0]] > hh[tt[u << 1 | 1]], hh[u] = hh[tt[u << 1 | side[u]]] + 1;
}
int merge(int u, int v) {
int tmp;
if (u == 0)
return v;
if (v == 0)
return u;
if (kk[u] < kk[v])
tmp = u, u = v, v = tmp;
tt[u << 1 | side[u]] = merge(tt[u << 1 | side[u]], v), pul(u);
return u;
}
int remove_first(int u) {
int t = merge(tt[u << 1 | 0], tt[u << 1 | 1]);
tt[u << 1 | 0] = tt[u << 1 | 1] = 0, pul(u);
return t;
}
int main() {
static int pp[N], cc[N], tt[N], cnt[N];
static long long dp[N];
int n, m, n_, i, t;
long long ans;
scanf("%d%d", &n, &m), n_ = n + m;
for (i = 1; i < n_; i++)
scanf("%d%d", &pp[i], &cc[i]), pp[i]--;
for (i = n_ - 1; i > 0; i--) {
dp[i] += cc[i];
if (i >= n)
tt[i] = merge(node(cc[i]), node(cc[i]));
else {
int u;
t = tt[i];
while (--cnt[i])
t = remove_first(t);
u = t, t = remove_first(t);
kk[u] += cc[i], kk[t] += cc[i];
tt[i] = merge(t, u);
}
dp[pp[i]] += dp[i], tt[pp[i]] = merge(tt[pp[i]], tt[i]), cnt[pp[i]]++;
}
t = tt[0];
while (cnt[0]--)
t = remove_first(t);
ans = dp[0];
while (t)
ans -= kk[t], t = remove_first(t);
printf("%lld\n", ans);
return 0;
}
Compilation message
fireworks.c: In function 'main':
fireworks.c:44:2: warning: ignoring return value of 'scanf', declared with attribute warn_unused_result [-Wunused-result]
44 | scanf("%d%d", &n, &m), n_ = n + m;
| ^~~~~~~~~~~~~~~~~~~~~
fireworks.c:46:3: warning: ignoring return value of 'scanf', declared with attribute warn_unused_result [-Wunused-result]
46 | scanf("%d%d", &pp[i], &cc[i]), pp[i]--;
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
0 ms |
364 KB |
Output is correct |
5 |
Correct |
0 ms |
364 KB |
Output is correct |
6 |
Correct |
0 ms |
364 KB |
Output is correct |
7 |
Correct |
0 ms |
364 KB |
Output is correct |
8 |
Correct |
0 ms |
364 KB |
Output is correct |
9 |
Correct |
0 ms |
364 KB |
Output is correct |
10 |
Correct |
0 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
0 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
0 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
0 ms |
364 KB |
Output is correct |
8 |
Correct |
0 ms |
364 KB |
Output is correct |
9 |
Correct |
0 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
0 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
0 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
0 ms |
364 KB |
Output is correct |
5 |
Correct |
0 ms |
364 KB |
Output is correct |
6 |
Correct |
0 ms |
364 KB |
Output is correct |
7 |
Correct |
0 ms |
364 KB |
Output is correct |
8 |
Correct |
0 ms |
364 KB |
Output is correct |
9 |
Correct |
0 ms |
364 KB |
Output is correct |
10 |
Correct |
0 ms |
364 KB |
Output is correct |
11 |
Correct |
0 ms |
364 KB |
Output is correct |
12 |
Correct |
0 ms |
364 KB |
Output is correct |
13 |
Correct |
0 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
0 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
0 ms |
364 KB |
Output is correct |
18 |
Correct |
0 ms |
364 KB |
Output is correct |
19 |
Correct |
0 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
0 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
0 ms |
364 KB |
Output is correct |
25 |
Correct |
1 ms |
364 KB |
Output is correct |
26 |
Correct |
1 ms |
364 KB |
Output is correct |
27 |
Correct |
1 ms |
364 KB |
Output is correct |
28 |
Correct |
1 ms |
364 KB |
Output is correct |
29 |
Correct |
1 ms |
364 KB |
Output is correct |
30 |
Correct |
1 ms |
396 KB |
Output is correct |
31 |
Correct |
1 ms |
364 KB |
Output is correct |
32 |
Correct |
1 ms |
364 KB |
Output is correct |
33 |
Correct |
1 ms |
492 KB |
Output is correct |
34 |
Correct |
1 ms |
492 KB |
Output is correct |
35 |
Correct |
2 ms |
492 KB |
Output is correct |
36 |
Correct |
2 ms |
492 KB |
Output is correct |
37 |
Correct |
2 ms |
492 KB |
Output is correct |
38 |
Correct |
2 ms |
492 KB |
Output is correct |
39 |
Correct |
2 ms |
492 KB |
Output is correct |
40 |
Correct |
2 ms |
492 KB |
Output is correct |
41 |
Correct |
2 ms |
492 KB |
Output is correct |
42 |
Correct |
2 ms |
492 KB |
Output is correct |
43 |
Correct |
3 ms |
620 KB |
Output is correct |
44 |
Correct |
3 ms |
620 KB |
Output is correct |
45 |
Correct |
3 ms |
620 KB |
Output is correct |
46 |
Correct |
4 ms |
620 KB |
Output is correct |
47 |
Correct |
4 ms |
620 KB |
Output is correct |
48 |
Correct |
4 ms |
620 KB |
Output is correct |
49 |
Correct |
4 ms |
620 KB |
Output is correct |
50 |
Correct |
4 ms |
620 KB |
Output is correct |
51 |
Correct |
4 ms |
620 KB |
Output is correct |
52 |
Correct |
4 ms |
620 KB |
Output is correct |
53 |
Correct |
4 ms |
620 KB |
Output is correct |
54 |
Correct |
5 ms |
620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
364 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
0 ms |
364 KB |
Output is correct |
5 |
Correct |
0 ms |
364 KB |
Output is correct |
6 |
Correct |
0 ms |
364 KB |
Output is correct |
7 |
Correct |
0 ms |
364 KB |
Output is correct |
8 |
Correct |
0 ms |
364 KB |
Output is correct |
9 |
Correct |
0 ms |
364 KB |
Output is correct |
10 |
Correct |
0 ms |
364 KB |
Output is correct |
11 |
Correct |
0 ms |
364 KB |
Output is correct |
12 |
Correct |
0 ms |
364 KB |
Output is correct |
13 |
Correct |
0 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
0 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
0 ms |
364 KB |
Output is correct |
18 |
Correct |
0 ms |
364 KB |
Output is correct |
19 |
Correct |
0 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
0 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
0 ms |
364 KB |
Output is correct |
25 |
Correct |
1 ms |
364 KB |
Output is correct |
26 |
Correct |
1 ms |
364 KB |
Output is correct |
27 |
Correct |
1 ms |
364 KB |
Output is correct |
28 |
Correct |
1 ms |
364 KB |
Output is correct |
29 |
Correct |
1 ms |
364 KB |
Output is correct |
30 |
Correct |
1 ms |
396 KB |
Output is correct |
31 |
Correct |
1 ms |
364 KB |
Output is correct |
32 |
Correct |
1 ms |
364 KB |
Output is correct |
33 |
Correct |
1 ms |
492 KB |
Output is correct |
34 |
Correct |
1 ms |
492 KB |
Output is correct |
35 |
Correct |
2 ms |
492 KB |
Output is correct |
36 |
Correct |
2 ms |
492 KB |
Output is correct |
37 |
Correct |
2 ms |
492 KB |
Output is correct |
38 |
Correct |
2 ms |
492 KB |
Output is correct |
39 |
Correct |
2 ms |
492 KB |
Output is correct |
40 |
Correct |
2 ms |
492 KB |
Output is correct |
41 |
Correct |
2 ms |
492 KB |
Output is correct |
42 |
Correct |
2 ms |
492 KB |
Output is correct |
43 |
Correct |
3 ms |
620 KB |
Output is correct |
44 |
Correct |
3 ms |
620 KB |
Output is correct |
45 |
Correct |
3 ms |
620 KB |
Output is correct |
46 |
Correct |
4 ms |
620 KB |
Output is correct |
47 |
Correct |
4 ms |
620 KB |
Output is correct |
48 |
Correct |
4 ms |
620 KB |
Output is correct |
49 |
Correct |
4 ms |
620 KB |
Output is correct |
50 |
Correct |
4 ms |
620 KB |
Output is correct |
51 |
Correct |
4 ms |
620 KB |
Output is correct |
52 |
Correct |
4 ms |
620 KB |
Output is correct |
53 |
Correct |
4 ms |
620 KB |
Output is correct |
54 |
Correct |
5 ms |
620 KB |
Output is correct |
55 |
Correct |
6 ms |
876 KB |
Output is correct |
56 |
Correct |
23 ms |
2236 KB |
Output is correct |
57 |
Correct |
41 ms |
3584 KB |
Output is correct |
58 |
Correct |
55 ms |
4460 KB |
Output is correct |
59 |
Correct |
72 ms |
5740 KB |
Output is correct |
60 |
Correct |
94 ms |
7128 KB |
Output is correct |
61 |
Correct |
109 ms |
8044 KB |
Output is correct |
62 |
Correct |
132 ms |
8812 KB |
Output is correct |
63 |
Correct |
152 ms |
10348 KB |
Output is correct |
64 |
Correct |
174 ms |
10988 KB |
Output is correct |
65 |
Correct |
84 ms |
7532 KB |
Output is correct |
66 |
Correct |
82 ms |
7532 KB |
Output is correct |
67 |
Correct |
86 ms |
7660 KB |
Output is correct |
68 |
Correct |
190 ms |
13164 KB |
Output is correct |
69 |
Correct |
203 ms |
13164 KB |
Output is correct |
70 |
Correct |
198 ms |
13164 KB |
Output is correct |
71 |
Correct |
393 ms |
18668 KB |
Output is correct |
72 |
Correct |
386 ms |
18668 KB |
Output is correct |
73 |
Correct |
417 ms |
18668 KB |
Output is correct |
74 |
Correct |
419 ms |
18796 KB |
Output is correct |
75 |
Correct |
432 ms |
18540 KB |
Output is correct |
76 |
Correct |
438 ms |
18636 KB |
Output is correct |
77 |
Correct |
463 ms |
18284 KB |
Output is correct |
78 |
Correct |
481 ms |
18156 KB |
Output is correct |
79 |
Correct |
713 ms |
18668 KB |
Output is correct |