#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
#define F first
#define S second
#define PB push_back
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
void FASTIO() {ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); }
const int MXN = 6e5+5;
int n, m;
vi adj[MXN];
ll C[MXN];
pll interval[MXN];
ll ans;
void dfs(int u) {
if(adj[u].empty()) {
interval[u] = {0, 0};
return;
}
vl X;
for(int v : adj[u]) {
dfs(v);
X.PB(interval[v].F+C[v]);
X.PB(interval[v].S+C[v]);
}
sort(all(X));
int k = X.size();
for(int v : adj[u]) {
if(interval[v].F+C[v] <= X[k/2-1] && X[k/2-1] <= interval[v].S+C[v]) continue;
ans += min(labs(X[k/2-1]-(interval[v].F+C[v])), labs(X[k/2-1]-(interval[v].S+C[v])));
}
interval[u] = {X[k/2-1], X[k/2]};
}
int main() {
FASTIO();
cin >> n >> m;
for(int i = 2; i <= n+m; i++) {
int p; cin >> p >> C[i];
adj[p].PB(i);
}
dfs(1);
cout << ans << "\n";
return 0;
}
/*
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
14420 KB |
Output is correct |
2 |
Correct |
7 ms |
14420 KB |
Output is correct |
3 |
Correct |
7 ms |
14420 KB |
Output is correct |
4 |
Correct |
8 ms |
14420 KB |
Output is correct |
5 |
Correct |
8 ms |
14416 KB |
Output is correct |
6 |
Correct |
8 ms |
14424 KB |
Output is correct |
7 |
Correct |
8 ms |
14344 KB |
Output is correct |
8 |
Correct |
8 ms |
14416 KB |
Output is correct |
9 |
Correct |
8 ms |
14400 KB |
Output is correct |
10 |
Correct |
10 ms |
14420 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
14308 KB |
Output is correct |
2 |
Correct |
9 ms |
14420 KB |
Output is correct |
3 |
Incorrect |
8 ms |
14420 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
14420 KB |
Output is correct |
2 |
Correct |
7 ms |
14420 KB |
Output is correct |
3 |
Correct |
7 ms |
14420 KB |
Output is correct |
4 |
Correct |
8 ms |
14420 KB |
Output is correct |
5 |
Correct |
8 ms |
14416 KB |
Output is correct |
6 |
Correct |
8 ms |
14424 KB |
Output is correct |
7 |
Correct |
8 ms |
14344 KB |
Output is correct |
8 |
Correct |
8 ms |
14416 KB |
Output is correct |
9 |
Correct |
8 ms |
14400 KB |
Output is correct |
10 |
Correct |
10 ms |
14420 KB |
Output is correct |
11 |
Correct |
8 ms |
14308 KB |
Output is correct |
12 |
Correct |
9 ms |
14420 KB |
Output is correct |
13 |
Incorrect |
8 ms |
14420 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
14420 KB |
Output is correct |
2 |
Correct |
7 ms |
14420 KB |
Output is correct |
3 |
Correct |
7 ms |
14420 KB |
Output is correct |
4 |
Correct |
8 ms |
14420 KB |
Output is correct |
5 |
Correct |
8 ms |
14416 KB |
Output is correct |
6 |
Correct |
8 ms |
14424 KB |
Output is correct |
7 |
Correct |
8 ms |
14344 KB |
Output is correct |
8 |
Correct |
8 ms |
14416 KB |
Output is correct |
9 |
Correct |
8 ms |
14400 KB |
Output is correct |
10 |
Correct |
10 ms |
14420 KB |
Output is correct |
11 |
Correct |
8 ms |
14308 KB |
Output is correct |
12 |
Correct |
9 ms |
14420 KB |
Output is correct |
13 |
Incorrect |
8 ms |
14420 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |