#include <bits/stdc++.h>
#pragma GCC optimize ("O3")
#pragma GCC optimize ("unroll-loops")
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
template <typename T>
using ordered_set = tree <T, null_type, less <T>, rb_tree_tag, tree_order_statistics_node_update>;
const int N = 3e5 + 10;
int n, m;
vector <int> g[N];
priority_queue <long long> s[N];
long long a[N], b[N], w[N];
int main() {
ios :: sync_with_stdio(0); cin.tie(0);
cin >> n >> m;
n += m;
for (int v = 2; v <= n; ++v) {
int u;
cin >> u;
cin >> w[v];
g[u].push_back(v);
}
for (int u = n; u >= 1; --u) {
if (g[u].empty()) {
s[u].push(w[u]), s[u].push(w[u]);
a[u] = 1, b[u] = - w[u];
continue;
}
for (auto v: g[u]) {
if (s[u].size() < s[v].size()) swap(s[u], s[v]);
while (s[v].size()) s[u].push(s[v].top()), s[v].pop();
a[u] += a[v];
b[u] += b[v];
}
while (a[u] > 1) {
--a[u];
b[u] += s[u].top();
s[u].pop();
}
b[u] -= w[u];
long long p0 = s[u].top(); s[u].pop();
long long p1 = s[u].top(); s[u].pop();
s[u].push(p0 + w[u]);
s[u].push(p1 + w[u]);
}
cout << b[1] + s[1].top();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
16724 KB |
Output is correct |
2 |
Correct |
9 ms |
16680 KB |
Output is correct |
3 |
Correct |
9 ms |
16852 KB |
Output is correct |
4 |
Correct |
9 ms |
16724 KB |
Output is correct |
5 |
Correct |
8 ms |
16724 KB |
Output is correct |
6 |
Correct |
8 ms |
16724 KB |
Output is correct |
7 |
Correct |
10 ms |
16724 KB |
Output is correct |
8 |
Correct |
9 ms |
16724 KB |
Output is correct |
9 |
Correct |
9 ms |
16780 KB |
Output is correct |
10 |
Correct |
9 ms |
16724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
16724 KB |
Output is correct |
2 |
Correct |
8 ms |
16696 KB |
Output is correct |
3 |
Correct |
9 ms |
16724 KB |
Output is correct |
4 |
Correct |
8 ms |
16724 KB |
Output is correct |
5 |
Correct |
9 ms |
16740 KB |
Output is correct |
6 |
Correct |
9 ms |
16724 KB |
Output is correct |
7 |
Correct |
8 ms |
16724 KB |
Output is correct |
8 |
Correct |
9 ms |
16724 KB |
Output is correct |
9 |
Correct |
9 ms |
16736 KB |
Output is correct |
10 |
Correct |
8 ms |
16724 KB |
Output is correct |
11 |
Correct |
8 ms |
16780 KB |
Output is correct |
12 |
Correct |
9 ms |
16724 KB |
Output is correct |
13 |
Correct |
11 ms |
16724 KB |
Output is correct |
14 |
Correct |
9 ms |
16764 KB |
Output is correct |
15 |
Correct |
11 ms |
16724 KB |
Output is correct |
16 |
Correct |
8 ms |
16724 KB |
Output is correct |
17 |
Correct |
8 ms |
16724 KB |
Output is correct |
18 |
Correct |
8 ms |
16724 KB |
Output is correct |
19 |
Correct |
9 ms |
16724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
16724 KB |
Output is correct |
2 |
Correct |
9 ms |
16680 KB |
Output is correct |
3 |
Correct |
9 ms |
16852 KB |
Output is correct |
4 |
Correct |
9 ms |
16724 KB |
Output is correct |
5 |
Correct |
8 ms |
16724 KB |
Output is correct |
6 |
Correct |
8 ms |
16724 KB |
Output is correct |
7 |
Correct |
10 ms |
16724 KB |
Output is correct |
8 |
Correct |
9 ms |
16724 KB |
Output is correct |
9 |
Correct |
9 ms |
16780 KB |
Output is correct |
10 |
Correct |
9 ms |
16724 KB |
Output is correct |
11 |
Correct |
10 ms |
16724 KB |
Output is correct |
12 |
Correct |
8 ms |
16696 KB |
Output is correct |
13 |
Correct |
9 ms |
16724 KB |
Output is correct |
14 |
Correct |
8 ms |
16724 KB |
Output is correct |
15 |
Correct |
9 ms |
16740 KB |
Output is correct |
16 |
Correct |
9 ms |
16724 KB |
Output is correct |
17 |
Correct |
8 ms |
16724 KB |
Output is correct |
18 |
Correct |
9 ms |
16724 KB |
Output is correct |
19 |
Correct |
9 ms |
16736 KB |
Output is correct |
20 |
Correct |
8 ms |
16724 KB |
Output is correct |
21 |
Correct |
8 ms |
16780 KB |
Output is correct |
22 |
Correct |
9 ms |
16724 KB |
Output is correct |
23 |
Correct |
11 ms |
16724 KB |
Output is correct |
24 |
Correct |
9 ms |
16764 KB |
Output is correct |
25 |
Correct |
11 ms |
16724 KB |
Output is correct |
26 |
Correct |
8 ms |
16724 KB |
Output is correct |
27 |
Correct |
8 ms |
16724 KB |
Output is correct |
28 |
Correct |
8 ms |
16724 KB |
Output is correct |
29 |
Correct |
9 ms |
16724 KB |
Output is correct |
30 |
Correct |
8 ms |
16724 KB |
Output is correct |
31 |
Correct |
10 ms |
16852 KB |
Output is correct |
32 |
Correct |
9 ms |
16852 KB |
Output is correct |
33 |
Correct |
10 ms |
16980 KB |
Output is correct |
34 |
Correct |
10 ms |
16960 KB |
Output is correct |
35 |
Correct |
10 ms |
17108 KB |
Output is correct |
36 |
Correct |
10 ms |
17036 KB |
Output is correct |
37 |
Correct |
11 ms |
17104 KB |
Output is correct |
38 |
Correct |
11 ms |
17236 KB |
Output is correct |
39 |
Correct |
11 ms |
17160 KB |
Output is correct |
40 |
Correct |
10 ms |
17108 KB |
Output is correct |
41 |
Correct |
11 ms |
17116 KB |
Output is correct |
42 |
Correct |
10 ms |
17108 KB |
Output is correct |
43 |
Correct |
10 ms |
17108 KB |
Output is correct |
44 |
Correct |
10 ms |
17108 KB |
Output is correct |
45 |
Correct |
10 ms |
17108 KB |
Output is correct |
46 |
Correct |
11 ms |
17288 KB |
Output is correct |
47 |
Correct |
12 ms |
17364 KB |
Output is correct |
48 |
Correct |
10 ms |
17164 KB |
Output is correct |
49 |
Correct |
12 ms |
17280 KB |
Output is correct |
50 |
Correct |
10 ms |
17108 KB |
Output is correct |
51 |
Correct |
10 ms |
17108 KB |
Output is correct |
52 |
Correct |
11 ms |
17236 KB |
Output is correct |
53 |
Correct |
12 ms |
17236 KB |
Output is correct |
54 |
Correct |
11 ms |
17284 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
16724 KB |
Output is correct |
2 |
Correct |
9 ms |
16680 KB |
Output is correct |
3 |
Correct |
9 ms |
16852 KB |
Output is correct |
4 |
Correct |
9 ms |
16724 KB |
Output is correct |
5 |
Correct |
8 ms |
16724 KB |
Output is correct |
6 |
Correct |
8 ms |
16724 KB |
Output is correct |
7 |
Correct |
10 ms |
16724 KB |
Output is correct |
8 |
Correct |
9 ms |
16724 KB |
Output is correct |
9 |
Correct |
9 ms |
16780 KB |
Output is correct |
10 |
Correct |
9 ms |
16724 KB |
Output is correct |
11 |
Correct |
10 ms |
16724 KB |
Output is correct |
12 |
Correct |
8 ms |
16696 KB |
Output is correct |
13 |
Correct |
9 ms |
16724 KB |
Output is correct |
14 |
Correct |
8 ms |
16724 KB |
Output is correct |
15 |
Correct |
9 ms |
16740 KB |
Output is correct |
16 |
Correct |
9 ms |
16724 KB |
Output is correct |
17 |
Correct |
8 ms |
16724 KB |
Output is correct |
18 |
Correct |
9 ms |
16724 KB |
Output is correct |
19 |
Correct |
9 ms |
16736 KB |
Output is correct |
20 |
Correct |
8 ms |
16724 KB |
Output is correct |
21 |
Correct |
8 ms |
16780 KB |
Output is correct |
22 |
Correct |
9 ms |
16724 KB |
Output is correct |
23 |
Correct |
11 ms |
16724 KB |
Output is correct |
24 |
Correct |
9 ms |
16764 KB |
Output is correct |
25 |
Correct |
11 ms |
16724 KB |
Output is correct |
26 |
Correct |
8 ms |
16724 KB |
Output is correct |
27 |
Correct |
8 ms |
16724 KB |
Output is correct |
28 |
Correct |
8 ms |
16724 KB |
Output is correct |
29 |
Correct |
9 ms |
16724 KB |
Output is correct |
30 |
Correct |
8 ms |
16724 KB |
Output is correct |
31 |
Correct |
10 ms |
16852 KB |
Output is correct |
32 |
Correct |
9 ms |
16852 KB |
Output is correct |
33 |
Correct |
10 ms |
16980 KB |
Output is correct |
34 |
Correct |
10 ms |
16960 KB |
Output is correct |
35 |
Correct |
10 ms |
17108 KB |
Output is correct |
36 |
Correct |
10 ms |
17036 KB |
Output is correct |
37 |
Correct |
11 ms |
17104 KB |
Output is correct |
38 |
Correct |
11 ms |
17236 KB |
Output is correct |
39 |
Correct |
11 ms |
17160 KB |
Output is correct |
40 |
Correct |
10 ms |
17108 KB |
Output is correct |
41 |
Correct |
11 ms |
17116 KB |
Output is correct |
42 |
Correct |
10 ms |
17108 KB |
Output is correct |
43 |
Correct |
10 ms |
17108 KB |
Output is correct |
44 |
Correct |
10 ms |
17108 KB |
Output is correct |
45 |
Correct |
10 ms |
17108 KB |
Output is correct |
46 |
Correct |
11 ms |
17288 KB |
Output is correct |
47 |
Correct |
12 ms |
17364 KB |
Output is correct |
48 |
Correct |
10 ms |
17164 KB |
Output is correct |
49 |
Correct |
12 ms |
17280 KB |
Output is correct |
50 |
Correct |
10 ms |
17108 KB |
Output is correct |
51 |
Correct |
10 ms |
17108 KB |
Output is correct |
52 |
Correct |
11 ms |
17236 KB |
Output is correct |
53 |
Correct |
12 ms |
17236 KB |
Output is correct |
54 |
Correct |
11 ms |
17284 KB |
Output is correct |
55 |
Correct |
14 ms |
17876 KB |
Output is correct |
56 |
Correct |
34 ms |
21432 KB |
Output is correct |
57 |
Correct |
49 ms |
24656 KB |
Output is correct |
58 |
Correct |
63 ms |
26852 KB |
Output is correct |
59 |
Correct |
91 ms |
30032 KB |
Output is correct |
60 |
Correct |
106 ms |
33284 KB |
Output is correct |
61 |
Correct |
133 ms |
36040 KB |
Output is correct |
62 |
Correct |
141 ms |
37876 KB |
Output is correct |
63 |
Correct |
164 ms |
41880 KB |
Output is correct |
64 |
Correct |
171 ms |
43076 KB |
Output is correct |
65 |
Correct |
96 ms |
38028 KB |
Output is correct |
66 |
Correct |
89 ms |
38024 KB |
Output is correct |
67 |
Correct |
85 ms |
38032 KB |
Output is correct |
68 |
Correct |
131 ms |
39820 KB |
Output is correct |
69 |
Correct |
194 ms |
39928 KB |
Output is correct |
70 |
Correct |
157 ms |
39956 KB |
Output is correct |
71 |
Correct |
168 ms |
52808 KB |
Output is correct |
72 |
Correct |
168 ms |
52796 KB |
Output is correct |
73 |
Correct |
143 ms |
48956 KB |
Output is correct |
74 |
Correct |
143 ms |
49188 KB |
Output is correct |
75 |
Correct |
145 ms |
48056 KB |
Output is correct |
76 |
Correct |
147 ms |
48056 KB |
Output is correct |
77 |
Correct |
151 ms |
47440 KB |
Output is correct |
78 |
Correct |
151 ms |
46756 KB |
Output is correct |
79 |
Correct |
136 ms |
46196 KB |
Output is correct |