This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |