#include<bits/stdc++.h>
using namespace std;
#define f first
#define s second
typedef long long ll;
const int maxn = 3e5+5;
const ll inf = 1e18;
ll ans = 0;
int n, m;
int w[maxn];
vector<int> tree[maxn];
pair<ll, int> dfs(int x){
if(x > n)return {w[x], w[x]};
vector<pair<ll, int>> op;
for(auto v: tree[x]){
auto tmp = dfs(v);
op.push_back({tmp.f, 0});
op.push_back({tmp.s, 1});
}
sort(op.begin(), op.end());
ll mid = op[op.size()/2].f;
for(auto v: op){
if(v.s)ans += max(0LL, mid - v.f);
else ans += max(0LL, v.f-mid);
}
return {op[op.size()/2-1].f+w[x], op[op.size()/2].f+w[x]};
}
int main(){
cin >> n >> m;
for(int i = 2; i <= n+m; i++){
int x;
cin >> x >> w[i];
tree[x].push_back(i);
}
dfs(1);
cout << ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7252 KB |
Output is correct |
2 |
Correct |
3 ms |
7252 KB |
Output is correct |
3 |
Correct |
3 ms |
7252 KB |
Output is correct |
4 |
Correct |
5 ms |
7252 KB |
Output is correct |
5 |
Correct |
5 ms |
7268 KB |
Output is correct |
6 |
Correct |
4 ms |
7252 KB |
Output is correct |
7 |
Correct |
4 ms |
7356 KB |
Output is correct |
8 |
Correct |
4 ms |
7356 KB |
Output is correct |
9 |
Correct |
5 ms |
7252 KB |
Output is correct |
10 |
Correct |
4 ms |
7252 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
7252 KB |
Output is correct |
2 |
Correct |
4 ms |
7252 KB |
Output is correct |
3 |
Incorrect |
4 ms |
7356 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7252 KB |
Output is correct |
2 |
Correct |
3 ms |
7252 KB |
Output is correct |
3 |
Correct |
3 ms |
7252 KB |
Output is correct |
4 |
Correct |
5 ms |
7252 KB |
Output is correct |
5 |
Correct |
5 ms |
7268 KB |
Output is correct |
6 |
Correct |
4 ms |
7252 KB |
Output is correct |
7 |
Correct |
4 ms |
7356 KB |
Output is correct |
8 |
Correct |
4 ms |
7356 KB |
Output is correct |
9 |
Correct |
5 ms |
7252 KB |
Output is correct |
10 |
Correct |
4 ms |
7252 KB |
Output is correct |
11 |
Correct |
3 ms |
7252 KB |
Output is correct |
12 |
Correct |
4 ms |
7252 KB |
Output is correct |
13 |
Incorrect |
4 ms |
7356 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
7252 KB |
Output is correct |
2 |
Correct |
3 ms |
7252 KB |
Output is correct |
3 |
Correct |
3 ms |
7252 KB |
Output is correct |
4 |
Correct |
5 ms |
7252 KB |
Output is correct |
5 |
Correct |
5 ms |
7268 KB |
Output is correct |
6 |
Correct |
4 ms |
7252 KB |
Output is correct |
7 |
Correct |
4 ms |
7356 KB |
Output is correct |
8 |
Correct |
4 ms |
7356 KB |
Output is correct |
9 |
Correct |
5 ms |
7252 KB |
Output is correct |
10 |
Correct |
4 ms |
7252 KB |
Output is correct |
11 |
Correct |
3 ms |
7252 KB |
Output is correct |
12 |
Correct |
4 ms |
7252 KB |
Output is correct |
13 |
Incorrect |
4 ms |
7356 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |