#include <stdio.h>
#define N 300000
int tt[(1 + N * 2) * 2], hh[(1 + N * 2) * 2]; long long kk[1 + N * 2];
int node(long long k) {
static int _ = 1;
kk[_] = k;
return _++;
}
void pul(int u) {
int tmp;
if (hh[tt[u << 1 | 0]] < hh[tt[u << 1 | 1]])
tmp = tt[u << 1 | 0], tt[u << 1 | 0] = tt[u << 1 | 1], tt[u << 1 | 1] = tmp;
hh[u] = hh[tt[u << 1 | 1]] + 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 | 1] = merge(tt[u << 1 | 1], 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;
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:48:2: warning: ignoring return value of 'scanf', declared with attribute warn_unused_result [-Wunused-result]
48 | scanf("%d%d", &n, &m), n_ = n + m;
| ^~~~~~~~~~~~~~~~~~~~~
fireworks.c:50:3: warning: ignoring return value of 'scanf', declared with attribute warn_unused_result [-Wunused-result]
50 | 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 |
1 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 |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
0 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
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 |
0 ms |
364 KB |
Output is correct |
4 |
Correct |
0 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 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 |
1 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 |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
0 ms |
364 KB |
Output is correct |
9 |
Correct |
1 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 |
0 ms |
364 KB |
Output is correct |
13 |
Correct |
0 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 |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
384 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
1 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 |
364 KB |
Output is correct |
31 |
Correct |
1 ms |
364 KB |
Output is correct |
32 |
Correct |
1 ms |
492 KB |
Output is correct |
33 |
Correct |
1 ms |
492 KB |
Output is correct |
34 |
Correct |
2 ms |
492 KB |
Output is correct |
35 |
Correct |
2 ms |
492 KB |
Output is correct |
36 |
Correct |
2 ms |
620 KB |
Output is correct |
37 |
Correct |
2 ms |
620 KB |
Output is correct |
38 |
Correct |
2 ms |
620 KB |
Output is correct |
39 |
Correct |
2 ms |
620 KB |
Output is correct |
40 |
Correct |
2 ms |
512 KB |
Output is correct |
41 |
Correct |
2 ms |
504 KB |
Output is correct |
42 |
Correct |
2 ms |
492 KB |
Output is correct |
43 |
Correct |
2 ms |
620 KB |
Output is correct |
44 |
Correct |
2 ms |
620 KB |
Output is correct |
45 |
Correct |
2 ms |
620 KB |
Output is correct |
46 |
Correct |
3 ms |
748 KB |
Output is correct |
47 |
Correct |
3 ms |
748 KB |
Output is correct |
48 |
Correct |
3 ms |
620 KB |
Output is correct |
49 |
Correct |
4 ms |
748 KB |
Output is correct |
50 |
Correct |
3 ms |
620 KB |
Output is correct |
51 |
Correct |
3 ms |
620 KB |
Output is correct |
52 |
Correct |
3 ms |
620 KB |
Output is correct |
53 |
Correct |
4 ms |
620 KB |
Output is correct |
54 |
Correct |
4 ms |
748 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 |
1 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 |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
0 ms |
364 KB |
Output is correct |
9 |
Correct |
1 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 |
0 ms |
364 KB |
Output is correct |
13 |
Correct |
0 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 |
20 |
Correct |
1 ms |
364 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
384 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
1 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 |
364 KB |
Output is correct |
31 |
Correct |
1 ms |
364 KB |
Output is correct |
32 |
Correct |
1 ms |
492 KB |
Output is correct |
33 |
Correct |
1 ms |
492 KB |
Output is correct |
34 |
Correct |
2 ms |
492 KB |
Output is correct |
35 |
Correct |
2 ms |
492 KB |
Output is correct |
36 |
Correct |
2 ms |
620 KB |
Output is correct |
37 |
Correct |
2 ms |
620 KB |
Output is correct |
38 |
Correct |
2 ms |
620 KB |
Output is correct |
39 |
Correct |
2 ms |
620 KB |
Output is correct |
40 |
Correct |
2 ms |
512 KB |
Output is correct |
41 |
Correct |
2 ms |
504 KB |
Output is correct |
42 |
Correct |
2 ms |
492 KB |
Output is correct |
43 |
Correct |
2 ms |
620 KB |
Output is correct |
44 |
Correct |
2 ms |
620 KB |
Output is correct |
45 |
Correct |
2 ms |
620 KB |
Output is correct |
46 |
Correct |
3 ms |
748 KB |
Output is correct |
47 |
Correct |
3 ms |
748 KB |
Output is correct |
48 |
Correct |
3 ms |
620 KB |
Output is correct |
49 |
Correct |
4 ms |
748 KB |
Output is correct |
50 |
Correct |
3 ms |
620 KB |
Output is correct |
51 |
Correct |
3 ms |
620 KB |
Output is correct |
52 |
Correct |
3 ms |
620 KB |
Output is correct |
53 |
Correct |
4 ms |
620 KB |
Output is correct |
54 |
Correct |
4 ms |
748 KB |
Output is correct |
55 |
Correct |
5 ms |
1004 KB |
Output is correct |
56 |
Correct |
21 ms |
2796 KB |
Output is correct |
57 |
Correct |
37 ms |
4076 KB |
Output is correct |
58 |
Correct |
49 ms |
4864 KB |
Output is correct |
59 |
Correct |
65 ms |
6252 KB |
Output is correct |
60 |
Correct |
84 ms |
7532 KB |
Output is correct |
61 |
Correct |
100 ms |
8300 KB |
Output is correct |
62 |
Correct |
110 ms |
9068 KB |
Output is correct |
63 |
Correct |
143 ms |
10732 KB |
Output is correct |
64 |
Correct |
148 ms |
11244 KB |
Output is correct |
65 |
Correct |
80 ms |
8044 KB |
Output is correct |
66 |
Correct |
81 ms |
7916 KB |
Output is correct |
67 |
Correct |
80 ms |
8044 KB |
Output is correct |
68 |
Correct |
159 ms |
13292 KB |
Output is correct |
69 |
Correct |
177 ms |
13548 KB |
Output is correct |
70 |
Correct |
174 ms |
13424 KB |
Output is correct |
71 |
Correct |
303 ms |
18632 KB |
Output is correct |
72 |
Correct |
302 ms |
18540 KB |
Output is correct |
73 |
Correct |
326 ms |
18540 KB |
Output is correct |
74 |
Correct |
320 ms |
18540 KB |
Output is correct |
75 |
Correct |
337 ms |
18500 KB |
Output is correct |
76 |
Correct |
345 ms |
18496 KB |
Output is correct |
77 |
Correct |
352 ms |
18180 KB |
Output is correct |
78 |
Correct |
381 ms |
18176 KB |
Output is correct |
79 |
Correct |
515 ms |
18680 KB |
Output is correct |