#include <bits/stdc++.h>
#define ll long long
using namespace std;
int n,m,c[300005];
priority_queue<int> slope[300005];
vector <int> G[300005];
ll val[300005], numkid[300005];
// numkid = startslope ?
void dfs(int u){
if (u > n)
return;
for(auto v: G[u]){
val[u] += c[v];
dfs(v);
if (v > n){
slope[v].push(c[v]);
slope[v].push(c[v]);
}
else{
slope[v].pop();
slope[v].push(slope[v].top() + c[v]);
}
}
for(auto v: G[u]){
if (slope[v].size() > slope[u].size())
slope[u].swap(slope[v]);
}
for(auto v: G[u]){
while(slope[v].size())
slope[u].push(slope[v].top()),
slope[v].pop();
}
for(int _=0;_<(int)G[u].size()-1;_++)
slope[u].pop();
}
int main(){
iostream::sync_with_stdio(0);
cin >> n >> m;
for(int i=2;i<=n+m;i++){
int p;
cin >> p >> c[i];
G[p].push_back(i);
}
dfs(1);
int last = slope[1].top();
int cnt = 0;
while(!slope[1].empty()){
val[1] -= 1ll * cnt * (last - slope[1].top());
cnt ++;
}
cout << val[1];
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
15 ms |
16760 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
14 ms |
16868 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
15 ms |
16760 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
15 ms |
16760 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |