#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];
int p0 = s[u].top(); s[u].pop();
int 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 |
10 ms |
16728 KB |
Output is correct |
3 |
Correct |
11 ms |
16724 KB |
Output is correct |
4 |
Correct |
9 ms |
16788 KB |
Output is correct |
5 |
Correct |
10 ms |
16676 KB |
Output is correct |
6 |
Correct |
10 ms |
16748 KB |
Output is correct |
7 |
Correct |
9 ms |
16772 KB |
Output is correct |
8 |
Correct |
13 ms |
16728 KB |
Output is correct |
9 |
Correct |
12 ms |
16740 KB |
Output is correct |
10 |
Correct |
9 ms |
16724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
16724 KB |
Output is correct |
2 |
Correct |
11 ms |
16724 KB |
Output is correct |
3 |
Correct |
9 ms |
16676 KB |
Output is correct |
4 |
Correct |
9 ms |
16784 KB |
Output is correct |
5 |
Correct |
9 ms |
16724 KB |
Output is correct |
6 |
Correct |
9 ms |
16724 KB |
Output is correct |
7 |
Correct |
8 ms |
16700 KB |
Output is correct |
8 |
Correct |
9 ms |
16784 KB |
Output is correct |
9 |
Correct |
9 ms |
16716 KB |
Output is correct |
10 |
Correct |
9 ms |
16724 KB |
Output is correct |
11 |
Correct |
9 ms |
16724 KB |
Output is correct |
12 |
Correct |
9 ms |
16724 KB |
Output is correct |
13 |
Correct |
9 ms |
16680 KB |
Output is correct |
14 |
Correct |
9 ms |
16720 KB |
Output is correct |
15 |
Correct |
9 ms |
16724 KB |
Output is correct |
16 |
Correct |
9 ms |
16724 KB |
Output is correct |
17 |
Correct |
10 ms |
16796 KB |
Output is correct |
18 |
Correct |
9 ms |
16724 KB |
Output is correct |
19 |
Correct |
10 ms |
16724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
16724 KB |
Output is correct |
2 |
Correct |
10 ms |
16728 KB |
Output is correct |
3 |
Correct |
11 ms |
16724 KB |
Output is correct |
4 |
Correct |
9 ms |
16788 KB |
Output is correct |
5 |
Correct |
10 ms |
16676 KB |
Output is correct |
6 |
Correct |
10 ms |
16748 KB |
Output is correct |
7 |
Correct |
9 ms |
16772 KB |
Output is correct |
8 |
Correct |
13 ms |
16728 KB |
Output is correct |
9 |
Correct |
12 ms |
16740 KB |
Output is correct |
10 |
Correct |
9 ms |
16724 KB |
Output is correct |
11 |
Correct |
11 ms |
16724 KB |
Output is correct |
12 |
Correct |
11 ms |
16724 KB |
Output is correct |
13 |
Correct |
9 ms |
16676 KB |
Output is correct |
14 |
Correct |
9 ms |
16784 KB |
Output is correct |
15 |
Correct |
9 ms |
16724 KB |
Output is correct |
16 |
Correct |
9 ms |
16724 KB |
Output is correct |
17 |
Correct |
8 ms |
16700 KB |
Output is correct |
18 |
Correct |
9 ms |
16784 KB |
Output is correct |
19 |
Correct |
9 ms |
16716 KB |
Output is correct |
20 |
Correct |
9 ms |
16724 KB |
Output is correct |
21 |
Correct |
9 ms |
16724 KB |
Output is correct |
22 |
Correct |
9 ms |
16724 KB |
Output is correct |
23 |
Correct |
9 ms |
16680 KB |
Output is correct |
24 |
Correct |
9 ms |
16720 KB |
Output is correct |
25 |
Correct |
9 ms |
16724 KB |
Output is correct |
26 |
Correct |
9 ms |
16724 KB |
Output is correct |
27 |
Correct |
10 ms |
16796 KB |
Output is correct |
28 |
Correct |
9 ms |
16724 KB |
Output is correct |
29 |
Correct |
10 ms |
16724 KB |
Output is correct |
30 |
Incorrect |
9 ms |
16768 KB |
Output isn't correct |
31 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
16724 KB |
Output is correct |
2 |
Correct |
10 ms |
16728 KB |
Output is correct |
3 |
Correct |
11 ms |
16724 KB |
Output is correct |
4 |
Correct |
9 ms |
16788 KB |
Output is correct |
5 |
Correct |
10 ms |
16676 KB |
Output is correct |
6 |
Correct |
10 ms |
16748 KB |
Output is correct |
7 |
Correct |
9 ms |
16772 KB |
Output is correct |
8 |
Correct |
13 ms |
16728 KB |
Output is correct |
9 |
Correct |
12 ms |
16740 KB |
Output is correct |
10 |
Correct |
9 ms |
16724 KB |
Output is correct |
11 |
Correct |
11 ms |
16724 KB |
Output is correct |
12 |
Correct |
11 ms |
16724 KB |
Output is correct |
13 |
Correct |
9 ms |
16676 KB |
Output is correct |
14 |
Correct |
9 ms |
16784 KB |
Output is correct |
15 |
Correct |
9 ms |
16724 KB |
Output is correct |
16 |
Correct |
9 ms |
16724 KB |
Output is correct |
17 |
Correct |
8 ms |
16700 KB |
Output is correct |
18 |
Correct |
9 ms |
16784 KB |
Output is correct |
19 |
Correct |
9 ms |
16716 KB |
Output is correct |
20 |
Correct |
9 ms |
16724 KB |
Output is correct |
21 |
Correct |
9 ms |
16724 KB |
Output is correct |
22 |
Correct |
9 ms |
16724 KB |
Output is correct |
23 |
Correct |
9 ms |
16680 KB |
Output is correct |
24 |
Correct |
9 ms |
16720 KB |
Output is correct |
25 |
Correct |
9 ms |
16724 KB |
Output is correct |
26 |
Correct |
9 ms |
16724 KB |
Output is correct |
27 |
Correct |
10 ms |
16796 KB |
Output is correct |
28 |
Correct |
9 ms |
16724 KB |
Output is correct |
29 |
Correct |
10 ms |
16724 KB |
Output is correct |
30 |
Incorrect |
9 ms |
16768 KB |
Output isn't correct |
31 |
Halted |
0 ms |
0 KB |
- |