#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], 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 |
9 ms |
16724 KB |
Output is correct |
2 |
Correct |
11 ms |
16764 KB |
Output is correct |
3 |
Incorrect |
11 ms |
16708 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
16724 KB |
Output is correct |
2 |
Correct |
9 ms |
16724 KB |
Output is correct |
3 |
Correct |
9 ms |
16768 KB |
Output is correct |
4 |
Correct |
10 ms |
16724 KB |
Output is correct |
5 |
Correct |
10 ms |
16724 KB |
Output is correct |
6 |
Correct |
9 ms |
16772 KB |
Output is correct |
7 |
Correct |
10 ms |
16780 KB |
Output is correct |
8 |
Correct |
9 ms |
16772 KB |
Output is correct |
9 |
Correct |
9 ms |
16768 KB |
Output is correct |
10 |
Correct |
10 ms |
16672 KB |
Output is correct |
11 |
Correct |
9 ms |
16792 KB |
Output is correct |
12 |
Correct |
10 ms |
16724 KB |
Output is correct |
13 |
Correct |
9 ms |
16724 KB |
Output is correct |
14 |
Correct |
9 ms |
16724 KB |
Output is correct |
15 |
Correct |
9 ms |
16768 KB |
Output is correct |
16 |
Correct |
8 ms |
16684 KB |
Output is correct |
17 |
Correct |
9 ms |
16724 KB |
Output is correct |
18 |
Correct |
9 ms |
16724 KB |
Output is correct |
19 |
Correct |
8 ms |
16724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
16724 KB |
Output is correct |
2 |
Correct |
11 ms |
16764 KB |
Output is correct |
3 |
Incorrect |
11 ms |
16708 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
16724 KB |
Output is correct |
2 |
Correct |
11 ms |
16764 KB |
Output is correct |
3 |
Incorrect |
11 ms |
16708 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |