#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 300300;
bool leaf[N];
vector<pair<int, int>> adj[N];
ll ansl[N], ansr[N], ans[N];
priority_queue<ll> l[N];
priority_queue<ll, vector<ll>, greater<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);
if (l[v].empty()) {
ans[v] = ans[x];
l[v].push(ansl[x]);
r[v].push(ansr[x]);
continue;
}
ll nowl = l[v].top(), nowr = r[v].top();
ans[v] += ans[x];
if (ansr[x] < nowl) {
ans[v] += nowl - ansr[x];
l[v].push(ansl[x]);
l[v].push(ansr[x]);
l[v].pop();
r[v].push(nowl);
}
else if (ansl[x] > nowr) {
ans[v] += ansl[x] - nowr;
r[v].push(ansl[x]);
r[v].push(ansr[x]);
r[v].pop();
l[v].push(nowr);
}
else {
l[v].push(ansl[x]);
r[v].push(ansr[x]);
}
}
ansl[v] = l[v].top();
ansr[v] = r[v].top();
}
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, c;
cin >> p >> c;
adj[p].push_back({ i, c });
if (i > n) leaf[i] = 1;
}
dfs(1, 0);
cout << ans[1];
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
33372 KB |
Output is correct |
2 |
Correct |
7 ms |
33372 KB |
Output is correct |
3 |
Correct |
8 ms |
33372 KB |
Output is correct |
4 |
Correct |
7 ms |
33540 KB |
Output is correct |
5 |
Correct |
7 ms |
33372 KB |
Output is correct |
6 |
Correct |
9 ms |
33624 KB |
Output is correct |
7 |
Correct |
7 ms |
33372 KB |
Output is correct |
8 |
Correct |
7 ms |
33372 KB |
Output is correct |
9 |
Correct |
7 ms |
33412 KB |
Output is correct |
10 |
Correct |
7 ms |
33372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
33372 KB |
Output is correct |
2 |
Correct |
7 ms |
33420 KB |
Output is correct |
3 |
Incorrect |
6 ms |
33372 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
33372 KB |
Output is correct |
2 |
Correct |
7 ms |
33372 KB |
Output is correct |
3 |
Correct |
8 ms |
33372 KB |
Output is correct |
4 |
Correct |
7 ms |
33540 KB |
Output is correct |
5 |
Correct |
7 ms |
33372 KB |
Output is correct |
6 |
Correct |
9 ms |
33624 KB |
Output is correct |
7 |
Correct |
7 ms |
33372 KB |
Output is correct |
8 |
Correct |
7 ms |
33372 KB |
Output is correct |
9 |
Correct |
7 ms |
33412 KB |
Output is correct |
10 |
Correct |
7 ms |
33372 KB |
Output is correct |
11 |
Correct |
7 ms |
33372 KB |
Output is correct |
12 |
Correct |
7 ms |
33420 KB |
Output is correct |
13 |
Incorrect |
6 ms |
33372 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
33372 KB |
Output is correct |
2 |
Correct |
7 ms |
33372 KB |
Output is correct |
3 |
Correct |
8 ms |
33372 KB |
Output is correct |
4 |
Correct |
7 ms |
33540 KB |
Output is correct |
5 |
Correct |
7 ms |
33372 KB |
Output is correct |
6 |
Correct |
9 ms |
33624 KB |
Output is correct |
7 |
Correct |
7 ms |
33372 KB |
Output is correct |
8 |
Correct |
7 ms |
33372 KB |
Output is correct |
9 |
Correct |
7 ms |
33412 KB |
Output is correct |
10 |
Correct |
7 ms |
33372 KB |
Output is correct |
11 |
Correct |
7 ms |
33372 KB |
Output is correct |
12 |
Correct |
7 ms |
33420 KB |
Output is correct |
13 |
Incorrect |
6 ms |
33372 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |