#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N=300010, M = 1e9 + 7;
int n,m,a,b,plague[N],root[N],ans;
vector<int> edges[N];
priority_queue<int> q[N];
void dfs(int v){
root[v]=v;
if(v>n){
q[v].push(plague[v]); q[v].push(plague[v]);
return;
}
for(auto i:edges[v]){
dfs(i);
if(q[root[i]].size() > q[root[v]].size()) root[v]=root[i];
}
for(auto i:edges[v]) if(root[i]!=root[v]){
while(q[root[i]].size()){
q[root[v]].push(q[root[i]].top());
q[root[i]].pop();
}
}
for (int i = 1; i < (int) edges[v].size(); i++) q[root[v]].pop();
a=q[root[v]].top(); q[root[v]].pop(); b=q[root[v]].top(); q[root[v]].pop();
a+=plague[v]; b+=plague[v]; q[root[v]].push(b); q[root[v]].push(a);
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> n >> m;
m += n;
for (int i = 2; i <= m; i++){
cin>>a>>b;
edges[a].push_back(i);
plague[i]=b; ans+=b;
}
dfs(1);
q[root[1]].pop();
a=q[root[1]].top();
q[root[1]].push(0);
b=0;
while(q[root[1]].size() > 0){
ans-=(a - q[root[1]].top()) * b;
b++;
a=q[root[1]].top();
q[root[1]].pop();
}
cout<<ans << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
16724 KB |
Output is correct |
2 |
Correct |
9 ms |
16724 KB |
Output is correct |
3 |
Incorrect |
8 ms |
16732 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
16688 KB |
Output is correct |
2 |
Correct |
9 ms |
16656 KB |
Output is correct |
3 |
Correct |
8 ms |
16708 KB |
Output is correct |
4 |
Correct |
9 ms |
16724 KB |
Output is correct |
5 |
Correct |
9 ms |
16724 KB |
Output is correct |
6 |
Correct |
11 ms |
16724 KB |
Output is correct |
7 |
Correct |
10 ms |
16780 KB |
Output is correct |
8 |
Correct |
9 ms |
16696 KB |
Output is correct |
9 |
Correct |
9 ms |
16724 KB |
Output is correct |
10 |
Correct |
9 ms |
16724 KB |
Output is correct |
11 |
Correct |
8 ms |
16724 KB |
Output is correct |
12 |
Correct |
9 ms |
16692 KB |
Output is correct |
13 |
Correct |
9 ms |
16764 KB |
Output is correct |
14 |
Correct |
8 ms |
16724 KB |
Output is correct |
15 |
Correct |
9 ms |
16728 KB |
Output is correct |
16 |
Correct |
8 ms |
16724 KB |
Output is correct |
17 |
Correct |
9 ms |
16724 KB |
Output is correct |
18 |
Correct |
8 ms |
16724 KB |
Output is correct |
19 |
Correct |
9 ms |
16744 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
16724 KB |
Output is correct |
2 |
Correct |
9 ms |
16724 KB |
Output is correct |
3 |
Incorrect |
8 ms |
16732 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
16724 KB |
Output is correct |
2 |
Correct |
9 ms |
16724 KB |
Output is correct |
3 |
Incorrect |
8 ms |
16732 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |