#include <algorithm>
#include <cstdio>
using ll = long long;
int lson[1200005], rson[1200005], dep[1200005], t_cnt;
ll val[1200005];
int merge(int u, int v)
{
if (!u || !v)
return u | v;
if (val[u] < val[v])
std::swap(u, v);
rson[u] = merge(rson[u], v);
if (dep[lson[u]] < dep[rson[u]])
std::swap(lson[u], rson[u]);
dep[u] = rson[u] ? dep[rson[u]] + 1 : 0;
return u;
}
inline int new_node(ll x) { val[++t_cnt] = x; return t_cnt; }
void pop(int &u) { u = merge(lson[u], rson[u]); }
int fa[600005], faw[600005], deg[600005], rt[600005];
int main()
{
// freopen("uoj-205.in", "r", stdin);
int n, m;
scanf("%d%d", &n, &m);
ll ans = 0;
for (int i = 1; i < n + m; i++)
{
scanf("%d%d", fa + i, faw + i);
deg[--fa[i]]++;
ans += faw[i];
}
for (int u = n + m - 1; u; u--)
{
ll l = 0, r = 0;
if (u < n)
{
while (--deg[u])
pop(rt[u]);
r = val[rt[u]];
pop(rt[u]);
l = val[rt[u]];
pop(rt[u]);
}
rt[u] = merge(rt[u], merge(new_node(l + faw[u]), new_node(r + faw[u])));
rt[fa[u]] = merge(rt[fa[u]], rt[u]);
}
while (deg[0]--)
pop(rt[0]);
while (rt[0])
{
ans -= val[rt[0]];
pop(rt[0]);
}
printf("%lld\n", ans);
return 0;
}
Compilation message
fireworks.cpp: In function 'int main()':
fireworks.cpp:25:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &n, &m);
~~~~~^~~~~~~~~~~~~~~~
fireworks.cpp:29:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", fa + i, faw + i);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
40 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
484 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
3 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
356 KB |
Output is correct |
2 |
Correct |
18 ms |
384 KB |
Output is correct |
3 |
Correct |
3 ms |
384 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
2 ms |
384 KB |
Output is correct |
13 |
Correct |
2 ms |
384 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
15 |
Correct |
3 ms |
384 KB |
Output is correct |
16 |
Correct |
2 ms |
384 KB |
Output is correct |
17 |
Correct |
2 ms |
384 KB |
Output is correct |
18 |
Correct |
3 ms |
384 KB |
Output is correct |
19 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
40 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
484 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
3 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
356 KB |
Output is correct |
12 |
Correct |
18 ms |
384 KB |
Output is correct |
13 |
Correct |
3 ms |
384 KB |
Output is correct |
14 |
Correct |
3 ms |
384 KB |
Output is correct |
15 |
Correct |
2 ms |
384 KB |
Output is correct |
16 |
Correct |
2 ms |
384 KB |
Output is correct |
17 |
Correct |
2 ms |
384 KB |
Output is correct |
18 |
Correct |
0 ms |
384 KB |
Output is correct |
19 |
Correct |
2 ms |
384 KB |
Output is correct |
20 |
Correct |
2 ms |
384 KB |
Output is correct |
21 |
Correct |
2 ms |
384 KB |
Output is correct |
22 |
Correct |
2 ms |
384 KB |
Output is correct |
23 |
Correct |
2 ms |
384 KB |
Output is correct |
24 |
Correct |
2 ms |
384 KB |
Output is correct |
25 |
Correct |
3 ms |
384 KB |
Output is correct |
26 |
Correct |
2 ms |
384 KB |
Output is correct |
27 |
Correct |
2 ms |
384 KB |
Output is correct |
28 |
Correct |
3 ms |
384 KB |
Output is correct |
29 |
Correct |
2 ms |
384 KB |
Output is correct |
30 |
Correct |
2 ms |
384 KB |
Output is correct |
31 |
Correct |
2 ms |
384 KB |
Output is correct |
32 |
Correct |
2 ms |
384 KB |
Output is correct |
33 |
Correct |
2 ms |
512 KB |
Output is correct |
34 |
Correct |
3 ms |
512 KB |
Output is correct |
35 |
Correct |
3 ms |
512 KB |
Output is correct |
36 |
Correct |
4 ms |
640 KB |
Output is correct |
37 |
Correct |
5 ms |
640 KB |
Output is correct |
38 |
Correct |
4 ms |
640 KB |
Output is correct |
39 |
Correct |
5 ms |
640 KB |
Output is correct |
40 |
Correct |
4 ms |
640 KB |
Output is correct |
41 |
Correct |
4 ms |
640 KB |
Output is correct |
42 |
Correct |
4 ms |
640 KB |
Output is correct |
43 |
Correct |
5 ms |
640 KB |
Output is correct |
44 |
Correct |
4 ms |
640 KB |
Output is correct |
45 |
Correct |
4 ms |
640 KB |
Output is correct |
46 |
Correct |
6 ms |
640 KB |
Output is correct |
47 |
Correct |
6 ms |
640 KB |
Output is correct |
48 |
Correct |
6 ms |
640 KB |
Output is correct |
49 |
Correct |
5 ms |
640 KB |
Output is correct |
50 |
Correct |
5 ms |
640 KB |
Output is correct |
51 |
Correct |
2 ms |
384 KB |
Output is correct |
52 |
Correct |
5 ms |
640 KB |
Output is correct |
53 |
Correct |
7 ms |
640 KB |
Output is correct |
54 |
Correct |
5 ms |
640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
40 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
484 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
3 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
356 KB |
Output is correct |
12 |
Correct |
18 ms |
384 KB |
Output is correct |
13 |
Correct |
3 ms |
384 KB |
Output is correct |
14 |
Correct |
3 ms |
384 KB |
Output is correct |
15 |
Correct |
2 ms |
384 KB |
Output is correct |
16 |
Correct |
2 ms |
384 KB |
Output is correct |
17 |
Correct |
2 ms |
384 KB |
Output is correct |
18 |
Correct |
0 ms |
384 KB |
Output is correct |
19 |
Correct |
2 ms |
384 KB |
Output is correct |
20 |
Correct |
2 ms |
384 KB |
Output is correct |
21 |
Correct |
2 ms |
384 KB |
Output is correct |
22 |
Correct |
2 ms |
384 KB |
Output is correct |
23 |
Correct |
2 ms |
384 KB |
Output is correct |
24 |
Correct |
2 ms |
384 KB |
Output is correct |
25 |
Correct |
3 ms |
384 KB |
Output is correct |
26 |
Correct |
2 ms |
384 KB |
Output is correct |
27 |
Correct |
2 ms |
384 KB |
Output is correct |
28 |
Correct |
3 ms |
384 KB |
Output is correct |
29 |
Correct |
2 ms |
384 KB |
Output is correct |
30 |
Correct |
2 ms |
384 KB |
Output is correct |
31 |
Correct |
2 ms |
384 KB |
Output is correct |
32 |
Correct |
2 ms |
384 KB |
Output is correct |
33 |
Correct |
2 ms |
512 KB |
Output is correct |
34 |
Correct |
3 ms |
512 KB |
Output is correct |
35 |
Correct |
3 ms |
512 KB |
Output is correct |
36 |
Correct |
4 ms |
640 KB |
Output is correct |
37 |
Correct |
5 ms |
640 KB |
Output is correct |
38 |
Correct |
4 ms |
640 KB |
Output is correct |
39 |
Correct |
5 ms |
640 KB |
Output is correct |
40 |
Correct |
4 ms |
640 KB |
Output is correct |
41 |
Correct |
4 ms |
640 KB |
Output is correct |
42 |
Correct |
4 ms |
640 KB |
Output is correct |
43 |
Correct |
5 ms |
640 KB |
Output is correct |
44 |
Correct |
4 ms |
640 KB |
Output is correct |
45 |
Correct |
4 ms |
640 KB |
Output is correct |
46 |
Correct |
6 ms |
640 KB |
Output is correct |
47 |
Correct |
6 ms |
640 KB |
Output is correct |
48 |
Correct |
6 ms |
640 KB |
Output is correct |
49 |
Correct |
5 ms |
640 KB |
Output is correct |
50 |
Correct |
5 ms |
640 KB |
Output is correct |
51 |
Correct |
2 ms |
384 KB |
Output is correct |
52 |
Correct |
5 ms |
640 KB |
Output is correct |
53 |
Correct |
7 ms |
640 KB |
Output is correct |
54 |
Correct |
5 ms |
640 KB |
Output is correct |
55 |
Correct |
8 ms |
1024 KB |
Output is correct |
56 |
Correct |
27 ms |
2936 KB |
Output is correct |
57 |
Correct |
45 ms |
4864 KB |
Output is correct |
58 |
Correct |
63 ms |
6180 KB |
Output is correct |
59 |
Correct |
80 ms |
8020 KB |
Output is correct |
60 |
Correct |
104 ms |
9940 KB |
Output is correct |
61 |
Correct |
111 ms |
11128 KB |
Output is correct |
62 |
Correct |
185 ms |
12440 KB |
Output is correct |
63 |
Correct |
200 ms |
14700 KB |
Output is correct |
64 |
Correct |
301 ms |
15352 KB |
Output is correct |
65 |
Correct |
81 ms |
14456 KB |
Output is correct |
66 |
Correct |
86 ms |
14428 KB |
Output is correct |
67 |
Correct |
88 ms |
16504 KB |
Output is correct |
68 |
Correct |
177 ms |
16320 KB |
Output is correct |
69 |
Correct |
198 ms |
16348 KB |
Output is correct |
70 |
Correct |
215 ms |
16240 KB |
Output is correct |
71 |
Correct |
347 ms |
15808 KB |
Output is correct |
72 |
Correct |
276 ms |
15608 KB |
Output is correct |
73 |
Correct |
374 ms |
15700 KB |
Output is correct |
74 |
Correct |
390 ms |
15608 KB |
Output is correct |
75 |
Correct |
391 ms |
15728 KB |
Output is correct |
76 |
Correct |
364 ms |
15608 KB |
Output is correct |
77 |
Correct |
396 ms |
15356 KB |
Output is correct |
78 |
Correct |
457 ms |
15480 KB |
Output is correct |
79 |
Correct |
609 ms |
15664 KB |
Output is correct |