#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, pul(t);
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 |
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 |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
0 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
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 |
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 |
372 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 |
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 |
1 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 |
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 |
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 |
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 |
372 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 |
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 |
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 |
3 ms |
748 KB |
Output is correct |
54 |
Correct |
4 ms |
748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
1 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 |
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 |
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 |
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 |
372 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 |
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 |
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 |
3 ms |
748 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 |
2412 KB |
Output is correct |
57 |
Correct |
39 ms |
3692 KB |
Output is correct |
58 |
Correct |
47 ms |
4460 KB |
Output is correct |
59 |
Correct |
69 ms |
5868 KB |
Output is correct |
60 |
Correct |
84 ms |
7148 KB |
Output is correct |
61 |
Correct |
97 ms |
7916 KB |
Output is correct |
62 |
Correct |
110 ms |
8812 KB |
Output is correct |
63 |
Correct |
135 ms |
10348 KB |
Output is correct |
64 |
Correct |
143 ms |
10860 KB |
Output is correct |
65 |
Correct |
84 ms |
7648 KB |
Output is correct |
66 |
Correct |
87 ms |
7660 KB |
Output is correct |
67 |
Correct |
82 ms |
7660 KB |
Output is correct |
68 |
Correct |
162 ms |
12908 KB |
Output is correct |
69 |
Correct |
183 ms |
13036 KB |
Output is correct |
70 |
Correct |
185 ms |
13036 KB |
Output is correct |
71 |
Correct |
320 ms |
18028 KB |
Output is correct |
72 |
Correct |
310 ms |
18316 KB |
Output is correct |
73 |
Correct |
319 ms |
18156 KB |
Output is correct |
74 |
Correct |
332 ms |
18284 KB |
Output is correct |
75 |
Correct |
344 ms |
18156 KB |
Output is correct |
76 |
Correct |
350 ms |
18156 KB |
Output is correct |
77 |
Correct |
364 ms |
17772 KB |
Output is correct |
78 |
Correct |
383 ms |
17900 KB |
Output is correct |
79 |
Correct |
539 ms |
18356 KB |
Output is correct |