#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 300300;
bool leaf[N];
vector<pair<int, ll>> adj[N];
ll ansl[N], ansr[N], ans[N];
multiset<ll, greater<ll>> l[N];
multiset<ll> r[N];
void dfs(int v, ll d) {
if (leaf[v]) {
ansl[v] = d;
ansr[v] = d;
return;
}
for (auto& [x, w] : adj[v]) {
dfs(x, d + w);
ans[v] += ans[x];
if (l[v].empty()) {
l[v].insert(ansl[x]);
r[v].insert(ansr[x]);
continue;
}
ll nowl = *l[v].begin(), nowr = *r[v].begin();
ll newl = ansl[x], newr = ansr[x];
if (newr < nowl) {
ans[v] += nowl - newr;
l[v].insert(newl);
l[v].insert(newr);
l[v].erase(l[v].begin());
r[v].insert(nowl);
}
else if (nowr < newl) {
ans[v] += newl - nowr;
r[v].insert(newl);
r[v].insert(newr);
r[v].erase(r[v].begin());
l[v].insert(nowr);
}
else {
l[v].insert(newl);
r[v].insert(newr);
}
}
ansl[v] = *l[v].begin();
ansr[v] = *r[v].begin();
}
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int n, m;
cin >> n >> m;
for (int i = 2;i <= n + m;i++) {
int p;
ll c;
cin >> p >> c;
adj[p].push_back({ i, c });
}
for (int i = n + 1;i <= n + m;i++) leaf[i] = 1;
dfs(1, 0ll);
cout << ans[1] << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
40540 KB |
Output is correct |
2 |
Correct |
10 ms |
40540 KB |
Output is correct |
3 |
Correct |
9 ms |
40540 KB |
Output is correct |
4 |
Correct |
9 ms |
40540 KB |
Output is correct |
5 |
Correct |
9 ms |
40540 KB |
Output is correct |
6 |
Correct |
9 ms |
40540 KB |
Output is correct |
7 |
Correct |
9 ms |
40540 KB |
Output is correct |
8 |
Correct |
9 ms |
40540 KB |
Output is correct |
9 |
Correct |
9 ms |
40548 KB |
Output is correct |
10 |
Correct |
9 ms |
40540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
40540 KB |
Output is correct |
2 |
Correct |
9 ms |
40724 KB |
Output is correct |
3 |
Incorrect |
9 ms |
40716 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
40540 KB |
Output is correct |
2 |
Correct |
10 ms |
40540 KB |
Output is correct |
3 |
Correct |
9 ms |
40540 KB |
Output is correct |
4 |
Correct |
9 ms |
40540 KB |
Output is correct |
5 |
Correct |
9 ms |
40540 KB |
Output is correct |
6 |
Correct |
9 ms |
40540 KB |
Output is correct |
7 |
Correct |
9 ms |
40540 KB |
Output is correct |
8 |
Correct |
9 ms |
40540 KB |
Output is correct |
9 |
Correct |
9 ms |
40548 KB |
Output is correct |
10 |
Correct |
9 ms |
40540 KB |
Output is correct |
11 |
Correct |
9 ms |
40540 KB |
Output is correct |
12 |
Correct |
9 ms |
40724 KB |
Output is correct |
13 |
Incorrect |
9 ms |
40716 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
40540 KB |
Output is correct |
2 |
Correct |
10 ms |
40540 KB |
Output is correct |
3 |
Correct |
9 ms |
40540 KB |
Output is correct |
4 |
Correct |
9 ms |
40540 KB |
Output is correct |
5 |
Correct |
9 ms |
40540 KB |
Output is correct |
6 |
Correct |
9 ms |
40540 KB |
Output is correct |
7 |
Correct |
9 ms |
40540 KB |
Output is correct |
8 |
Correct |
9 ms |
40540 KB |
Output is correct |
9 |
Correct |
9 ms |
40548 KB |
Output is correct |
10 |
Correct |
9 ms |
40540 KB |
Output is correct |
11 |
Correct |
9 ms |
40540 KB |
Output is correct |
12 |
Correct |
9 ms |
40724 KB |
Output is correct |
13 |
Incorrect |
9 ms |
40716 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |