# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1024192 | vjudge1 | Fireworks (APIO16_fireworks) | C++17 | 3 ms | 7516 KiB |
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<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int n,m,p,c;
long long r=0;
vector<pair<int,int>>ad[300000];
pair<long long,long long>dfs(int u){
if(ad[u].empty())return {0,0};
long long c1=0,c2=0;
pair<long long,long long>op;
vector<pair<long long,long long>>t;
for(auto v:ad[u]){
t.push_back(dfs(v.first));
t.back().first+=v.second;
t.back().second+=v.second;
}
sort(t.begin(),t.end());
if(t.size()&1)op=t[t.size()/2];
else op={t[t.size()/2-1].second,t[t.size()/2].first};
if(op.second<op.first)swap(op.first,op.second);
for(auto v:t){
c1+=min(abs(op.first-v.first),abs(op.first-v.second));
c2+=min(abs(op.second-v.first),abs(op.second-v.second));
}
if(c1==c2)r+=c1;
else if(c1<c2)op.second=op.first,r+=c1;
else op.first=op.second,r+=c2;
return op;
}
int main(){
cin.tie(0)->sync_with_stdio(0);
#ifndef ONLINE_JUDGE
freopen("test.in","r",stdin);
freopen("test.out","w",stdout);
#endif
cin>>n>>m;
for(int i=2;i<=n+m;i++){
cin>>p>>c;
ad[p].push_back({i,c});
}
dfs(1);
cout<<r;
return 1<<32;
}
Compilation message (stderr)
# | 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... |