#include <stdio.h>
#define N 300000
int ll[1 + N * 2], rr[1 + N * 2], hh[1 + N * 2]; long long kk[1 + N * 2];
int node(long long k) {
static int _ = 1;
kk[_] = k, ll[_] = rr[_] = 0;
return _++;
}
void pul(int u) {
int tmp;
if (hh[ll[u]] < hh[rr[u]])
tmp = ll[u], ll[u] = rr[u], rr[u] = tmp;
hh[u] = hh[rr[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;
rr[u] = merge(rr[u], v), pul(u);
return u;
}
int remove_first(int t) {
int t_ = merge(ll[t], rr[t]);
ll[t] = rr[t] = 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]--;
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
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 |
0 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
376 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 |
364 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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
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 |
0 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
0 ms |
364 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 |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
376 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 |
364 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 |
388 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 |
3 ms |
748 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 |
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 |
2 ms |
620 KB |
Output is correct |
44 |
Correct |
3 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 |
3 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 |
3 ms |
768 KB |
Output is correct |
54 |
Correct |
4 ms |
748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
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 |
0 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
0 ms |
364 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 |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
376 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 |
364 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 |
388 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 |
3 ms |
748 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 |
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 |
2 ms |
620 KB |
Output is correct |
44 |
Correct |
3 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 |
3 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 |
3 ms |
768 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 |
3112 KB |
Output is correct |
57 |
Correct |
37 ms |
4716 KB |
Output is correct |
58 |
Correct |
48 ms |
5996 KB |
Output is correct |
59 |
Correct |
69 ms |
7788 KB |
Output is correct |
60 |
Correct |
101 ms |
9580 KB |
Output is correct |
61 |
Correct |
112 ms |
10988 KB |
Output is correct |
62 |
Correct |
109 ms |
12012 KB |
Output is correct |
63 |
Correct |
132 ms |
14316 KB |
Output is correct |
64 |
Correct |
144 ms |
15084 KB |
Output is correct |
65 |
Correct |
85 ms |
12268 KB |
Output is correct |
66 |
Correct |
81 ms |
12268 KB |
Output is correct |
67 |
Correct |
85 ms |
12288 KB |
Output is correct |
68 |
Correct |
159 ms |
17388 KB |
Output is correct |
69 |
Correct |
180 ms |
17644 KB |
Output is correct |
70 |
Correct |
168 ms |
17516 KB |
Output is correct |
71 |
Correct |
306 ms |
22252 KB |
Output is correct |
72 |
Correct |
302 ms |
22252 KB |
Output is correct |
73 |
Correct |
322 ms |
22124 KB |
Output is correct |
74 |
Correct |
314 ms |
21996 KB |
Output is correct |
75 |
Correct |
327 ms |
21612 KB |
Output is correct |
76 |
Correct |
332 ms |
21784 KB |
Output is correct |
77 |
Correct |
351 ms |
21740 KB |
Output is correct |
78 |
Correct |
377 ms |
21484 KB |
Output is correct |
79 |
Correct |
507 ms |
21484 KB |
Output is correct |