#include <stdio.h>
#include <string.h>
#define N 5000
#define INF 0x3f3f3f3f3f3f3f3fLL
long long min(long long a, long long b) { return a < b ? a : b; }
int merge(int *dd1, long long *ll1, int k1, int *dd2, long long *ll2, int k2) {
static int dd[N];
static long long ll[N];
int k, h1, h2;
k = 0, h1 = h2 = 0;
while (h1 < k1 && h2 < k2) {
long long l = min(ll1[h1], ll2[h2]);
dd[k] = dd1[h1] + dd2[h2], ll[k] = l, k++;
if ((ll1[h1] -= l) == 0)
h1++;
if ((ll2[h2] -= l) == 0)
h2++;
}
memcpy(dd1, dd, k * sizeof *dd);
memcpy(ll1, ll, k * sizeof *ll);
return k;
}
int main() {
static int pp[N], cc[N], dd[N][N], kk[N];
static long long dp[N], ll[N][N];
int n, m, n_, h, i;
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--) {
int p;
dp[i] += cc[i];
for (h = kk[i]; h > 0 && dd[i][h - 1] >= -1; h--)
dd[i][h] = dd[i][h - 1], ll[i][h] = ll[i][h - 1];
dd[i][h] = -1, ll[i][h] = cc[i], kk[i]++;
while (kk[i] && dd[i][kk[i] - 1] >= 1)
kk[i]--;
dd[i][kk[i]] = 1, ll[i][kk[i]] = INF, kk[i]++;
p = pp[i];
dp[p] += dp[i];
if (kk[p] == 0)
memcpy(dd[p], dd[i], kk[i] * sizeof *dd[i]), memcpy(ll[p], ll[i], kk[i] * sizeof *ll[i]), kk[p] = kk[i];
else
kk[p] = merge(dd[p], ll[p], kk[p], dd[i], ll[i], kk[i]);
}
ans = dp[0];
for (h = 0; h < kk[0] && dd[i][h] < 0; h++)
ans += dd[i][h] * ll[i][h];
printf("%lld\n", ans);
return 0;
}
Compilation message
fireworks.c: In function 'main':
fireworks.c:35:2: warning: ignoring return value of 'scanf', declared with attribute warn_unused_result [-Wunused-result]
35 | scanf("%d%d", &n, &m), n_ = n + m;
| ^~~~~~~~~~~~~~~~~~~~~
fireworks.c:37:3: warning: ignoring return value of 'scanf', declared with attribute warn_unused_result [-Wunused-result]
37 | 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 |
1 ms |
380 KB |
Output is correct |
3 |
Correct |
1 ms |
1132 KB |
Output is correct |
4 |
Correct |
1 ms |
1004 KB |
Output is correct |
5 |
Correct |
1 ms |
1004 KB |
Output is correct |
6 |
Correct |
1 ms |
1132 KB |
Output is correct |
7 |
Correct |
1 ms |
1132 KB |
Output is correct |
8 |
Correct |
1 ms |
1132 KB |
Output is correct |
9 |
Correct |
1 ms |
1004 KB |
Output is correct |
10 |
Correct |
1 ms |
1132 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
492 KB |
Output is correct |
2 |
Correct |
1 ms |
620 KB |
Output is correct |
3 |
Correct |
1 ms |
748 KB |
Output is correct |
4 |
Correct |
1 ms |
1132 KB |
Output is correct |
5 |
Correct |
1 ms |
1260 KB |
Output is correct |
6 |
Correct |
1 ms |
1388 KB |
Output is correct |
7 |
Correct |
1 ms |
1644 KB |
Output is correct |
8 |
Correct |
1 ms |
1772 KB |
Output is correct |
9 |
Correct |
1 ms |
1900 KB |
Output is correct |
10 |
Correct |
2 ms |
2028 KB |
Output is correct |
11 |
Correct |
2 ms |
2212 KB |
Output is correct |
12 |
Correct |
2 ms |
2412 KB |
Output is correct |
13 |
Correct |
2 ms |
2540 KB |
Output is correct |
14 |
Correct |
2 ms |
3180 KB |
Output is correct |
15 |
Correct |
2 ms |
2796 KB |
Output is correct |
16 |
Correct |
2 ms |
2796 KB |
Output is correct |
17 |
Correct |
2 ms |
2796 KB |
Output is correct |
18 |
Correct |
2 ms |
2796 KB |
Output is correct |
19 |
Correct |
2 ms |
2796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
380 KB |
Output is correct |
3 |
Correct |
1 ms |
1132 KB |
Output is correct |
4 |
Correct |
1 ms |
1004 KB |
Output is correct |
5 |
Correct |
1 ms |
1004 KB |
Output is correct |
6 |
Correct |
1 ms |
1132 KB |
Output is correct |
7 |
Correct |
1 ms |
1132 KB |
Output is correct |
8 |
Correct |
1 ms |
1132 KB |
Output is correct |
9 |
Correct |
1 ms |
1004 KB |
Output is correct |
10 |
Correct |
1 ms |
1132 KB |
Output is correct |
11 |
Correct |
0 ms |
492 KB |
Output is correct |
12 |
Correct |
1 ms |
620 KB |
Output is correct |
13 |
Correct |
1 ms |
748 KB |
Output is correct |
14 |
Correct |
1 ms |
1132 KB |
Output is correct |
15 |
Correct |
1 ms |
1260 KB |
Output is correct |
16 |
Correct |
1 ms |
1388 KB |
Output is correct |
17 |
Correct |
1 ms |
1644 KB |
Output is correct |
18 |
Correct |
1 ms |
1772 KB |
Output is correct |
19 |
Correct |
1 ms |
1900 KB |
Output is correct |
20 |
Correct |
2 ms |
2028 KB |
Output is correct |
21 |
Correct |
2 ms |
2212 KB |
Output is correct |
22 |
Correct |
2 ms |
2412 KB |
Output is correct |
23 |
Correct |
2 ms |
2540 KB |
Output is correct |
24 |
Correct |
2 ms |
3180 KB |
Output is correct |
25 |
Correct |
2 ms |
2796 KB |
Output is correct |
26 |
Correct |
2 ms |
2796 KB |
Output is correct |
27 |
Correct |
2 ms |
2796 KB |
Output is correct |
28 |
Correct |
2 ms |
2796 KB |
Output is correct |
29 |
Correct |
2 ms |
2796 KB |
Output is correct |
30 |
Correct |
3 ms |
4204 KB |
Output is correct |
31 |
Correct |
6 ms |
9964 KB |
Output is correct |
32 |
Correct |
8 ms |
13676 KB |
Output is correct |
33 |
Correct |
11 ms |
19200 KB |
Output is correct |
34 |
Correct |
14 ms |
24940 KB |
Output is correct |
35 |
Correct |
17 ms |
31084 KB |
Output is correct |
36 |
Correct |
21 ms |
34700 KB |
Output is correct |
37 |
Correct |
22 ms |
40044 KB |
Output is correct |
38 |
Correct |
25 ms |
44140 KB |
Output is correct |
39 |
Correct |
24 ms |
42604 KB |
Output is correct |
40 |
Correct |
112 ms |
184300 KB |
Output is correct |
41 |
Correct |
111 ms |
184256 KB |
Output is correct |
42 |
Correct |
25 ms |
42860 KB |
Output is correct |
43 |
Correct |
44 ms |
59520 KB |
Output is correct |
44 |
Correct |
43 ms |
58476 KB |
Output is correct |
45 |
Correct |
43 ms |
58476 KB |
Output is correct |
46 |
Correct |
27 ms |
41196 KB |
Output is correct |
47 |
Correct |
25 ms |
40812 KB |
Output is correct |
48 |
Correct |
34 ms |
37100 KB |
Output is correct |
49 |
Correct |
39 ms |
41196 KB |
Output is correct |
50 |
Correct |
50 ms |
35308 KB |
Output is correct |
51 |
Correct |
47 ms |
34284 KB |
Output is correct |
52 |
Correct |
42 ms |
37484 KB |
Output is correct |
53 |
Correct |
42 ms |
38508 KB |
Output is correct |
54 |
Correct |
106 ms |
41068 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
380 KB |
Output is correct |
3 |
Correct |
1 ms |
1132 KB |
Output is correct |
4 |
Correct |
1 ms |
1004 KB |
Output is correct |
5 |
Correct |
1 ms |
1004 KB |
Output is correct |
6 |
Correct |
1 ms |
1132 KB |
Output is correct |
7 |
Correct |
1 ms |
1132 KB |
Output is correct |
8 |
Correct |
1 ms |
1132 KB |
Output is correct |
9 |
Correct |
1 ms |
1004 KB |
Output is correct |
10 |
Correct |
1 ms |
1132 KB |
Output is correct |
11 |
Correct |
0 ms |
492 KB |
Output is correct |
12 |
Correct |
1 ms |
620 KB |
Output is correct |
13 |
Correct |
1 ms |
748 KB |
Output is correct |
14 |
Correct |
1 ms |
1132 KB |
Output is correct |
15 |
Correct |
1 ms |
1260 KB |
Output is correct |
16 |
Correct |
1 ms |
1388 KB |
Output is correct |
17 |
Correct |
1 ms |
1644 KB |
Output is correct |
18 |
Correct |
1 ms |
1772 KB |
Output is correct |
19 |
Correct |
1 ms |
1900 KB |
Output is correct |
20 |
Correct |
2 ms |
2028 KB |
Output is correct |
21 |
Correct |
2 ms |
2212 KB |
Output is correct |
22 |
Correct |
2 ms |
2412 KB |
Output is correct |
23 |
Correct |
2 ms |
2540 KB |
Output is correct |
24 |
Correct |
2 ms |
3180 KB |
Output is correct |
25 |
Correct |
2 ms |
2796 KB |
Output is correct |
26 |
Correct |
2 ms |
2796 KB |
Output is correct |
27 |
Correct |
2 ms |
2796 KB |
Output is correct |
28 |
Correct |
2 ms |
2796 KB |
Output is correct |
29 |
Correct |
2 ms |
2796 KB |
Output is correct |
30 |
Correct |
3 ms |
4204 KB |
Output is correct |
31 |
Correct |
6 ms |
9964 KB |
Output is correct |
32 |
Correct |
8 ms |
13676 KB |
Output is correct |
33 |
Correct |
11 ms |
19200 KB |
Output is correct |
34 |
Correct |
14 ms |
24940 KB |
Output is correct |
35 |
Correct |
17 ms |
31084 KB |
Output is correct |
36 |
Correct |
21 ms |
34700 KB |
Output is correct |
37 |
Correct |
22 ms |
40044 KB |
Output is correct |
38 |
Correct |
25 ms |
44140 KB |
Output is correct |
39 |
Correct |
24 ms |
42604 KB |
Output is correct |
40 |
Correct |
112 ms |
184300 KB |
Output is correct |
41 |
Correct |
111 ms |
184256 KB |
Output is correct |
42 |
Correct |
25 ms |
42860 KB |
Output is correct |
43 |
Correct |
44 ms |
59520 KB |
Output is correct |
44 |
Correct |
43 ms |
58476 KB |
Output is correct |
45 |
Correct |
43 ms |
58476 KB |
Output is correct |
46 |
Correct |
27 ms |
41196 KB |
Output is correct |
47 |
Correct |
25 ms |
40812 KB |
Output is correct |
48 |
Correct |
34 ms |
37100 KB |
Output is correct |
49 |
Correct |
39 ms |
41196 KB |
Output is correct |
50 |
Correct |
50 ms |
35308 KB |
Output is correct |
51 |
Correct |
47 ms |
34284 KB |
Output is correct |
52 |
Correct |
42 ms |
37484 KB |
Output is correct |
53 |
Correct |
42 ms |
38508 KB |
Output is correct |
54 |
Correct |
106 ms |
41068 KB |
Output is correct |
55 |
Runtime error |
19 ms |
748 KB |
Execution killed with signal 11 |
56 |
Halted |
0 ms |
0 KB |
- |