#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 <int> s[N];
int a[N];
long long 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();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
16724 KB |
Output is correct |
2 |
Correct |
9 ms |
16724 KB |
Output is correct |
3 |
Correct |
9 ms |
16664 KB |
Output is correct |
4 |
Correct |
10 ms |
16680 KB |
Output is correct |
5 |
Correct |
9 ms |
16752 KB |
Output is correct |
6 |
Correct |
11 ms |
16748 KB |
Output is correct |
7 |
Correct |
9 ms |
16764 KB |
Output is correct |
8 |
Correct |
12 ms |
16724 KB |
Output is correct |
9 |
Correct |
10 ms |
16724 KB |
Output is correct |
10 |
Correct |
9 ms |
16772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
16760 KB |
Output is correct |
2 |
Correct |
8 ms |
16724 KB |
Output is correct |
3 |
Correct |
10 ms |
16724 KB |
Output is correct |
4 |
Correct |
8 ms |
16780 KB |
Output is correct |
5 |
Correct |
8 ms |
16728 KB |
Output is correct |
6 |
Correct |
12 ms |
16740 KB |
Output is correct |
7 |
Correct |
12 ms |
16724 KB |
Output is correct |
8 |
Correct |
9 ms |
16724 KB |
Output is correct |
9 |
Correct |
9 ms |
16724 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 |
16724 KB |
Output is correct |
14 |
Correct |
10 ms |
16700 KB |
Output is correct |
15 |
Correct |
9 ms |
16724 KB |
Output is correct |
16 |
Correct |
9 ms |
16724 KB |
Output is correct |
17 |
Correct |
9 ms |
16724 KB |
Output is correct |
18 |
Correct |
9 ms |
16724 KB |
Output is correct |
19 |
Correct |
9 ms |
16724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
16724 KB |
Output is correct |
2 |
Correct |
9 ms |
16724 KB |
Output is correct |
3 |
Correct |
9 ms |
16664 KB |
Output is correct |
4 |
Correct |
10 ms |
16680 KB |
Output is correct |
5 |
Correct |
9 ms |
16752 KB |
Output is correct |
6 |
Correct |
11 ms |
16748 KB |
Output is correct |
7 |
Correct |
9 ms |
16764 KB |
Output is correct |
8 |
Correct |
12 ms |
16724 KB |
Output is correct |
9 |
Correct |
10 ms |
16724 KB |
Output is correct |
10 |
Correct |
9 ms |
16772 KB |
Output is correct |
11 |
Correct |
8 ms |
16760 KB |
Output is correct |
12 |
Correct |
8 ms |
16724 KB |
Output is correct |
13 |
Correct |
10 ms |
16724 KB |
Output is correct |
14 |
Correct |
8 ms |
16780 KB |
Output is correct |
15 |
Correct |
8 ms |
16728 KB |
Output is correct |
16 |
Correct |
12 ms |
16740 KB |
Output is correct |
17 |
Correct |
12 ms |
16724 KB |
Output is correct |
18 |
Correct |
9 ms |
16724 KB |
Output is correct |
19 |
Correct |
9 ms |
16724 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 |
16724 KB |
Output is correct |
24 |
Correct |
10 ms |
16700 KB |
Output is correct |
25 |
Correct |
9 ms |
16724 KB |
Output is correct |
26 |
Correct |
9 ms |
16724 KB |
Output is correct |
27 |
Correct |
9 ms |
16724 KB |
Output is correct |
28 |
Correct |
9 ms |
16724 KB |
Output is correct |
29 |
Correct |
9 ms |
16724 KB |
Output is correct |
30 |
Incorrect |
8 ms |
16724 KB |
Output isn't correct |
31 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
16724 KB |
Output is correct |
2 |
Correct |
9 ms |
16724 KB |
Output is correct |
3 |
Correct |
9 ms |
16664 KB |
Output is correct |
4 |
Correct |
10 ms |
16680 KB |
Output is correct |
5 |
Correct |
9 ms |
16752 KB |
Output is correct |
6 |
Correct |
11 ms |
16748 KB |
Output is correct |
7 |
Correct |
9 ms |
16764 KB |
Output is correct |
8 |
Correct |
12 ms |
16724 KB |
Output is correct |
9 |
Correct |
10 ms |
16724 KB |
Output is correct |
10 |
Correct |
9 ms |
16772 KB |
Output is correct |
11 |
Correct |
8 ms |
16760 KB |
Output is correct |
12 |
Correct |
8 ms |
16724 KB |
Output is correct |
13 |
Correct |
10 ms |
16724 KB |
Output is correct |
14 |
Correct |
8 ms |
16780 KB |
Output is correct |
15 |
Correct |
8 ms |
16728 KB |
Output is correct |
16 |
Correct |
12 ms |
16740 KB |
Output is correct |
17 |
Correct |
12 ms |
16724 KB |
Output is correct |
18 |
Correct |
9 ms |
16724 KB |
Output is correct |
19 |
Correct |
9 ms |
16724 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 |
16724 KB |
Output is correct |
24 |
Correct |
10 ms |
16700 KB |
Output is correct |
25 |
Correct |
9 ms |
16724 KB |
Output is correct |
26 |
Correct |
9 ms |
16724 KB |
Output is correct |
27 |
Correct |
9 ms |
16724 KB |
Output is correct |
28 |
Correct |
9 ms |
16724 KB |
Output is correct |
29 |
Correct |
9 ms |
16724 KB |
Output is correct |
30 |
Incorrect |
8 ms |
16724 KB |
Output isn't correct |
31 |
Halted |
0 ms |
0 KB |
- |