# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
485765 | milisav | Fireworks (APIO16_fireworks) | C++14 | 13 ms | 23860 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<bits/stdc++.h>
#define maxn 1000000
using namespace std;
int n,m;
vector<int> a[maxn];
int p[maxn];
int c[maxn];
long long ans;
long long x[maxn][2];
vector<long long> cur;
void dfs(int u) {
if(a[u].size()==0) {
x[u][0]=c[u];
x[u][1]=c[u];
return;
}
for(auto v:a[u]) {
dfs(v);
}
cur.clear();
for(auto v:a[u]) {
cur.push_back(x[v][0]);
cur.push_back(x[v][1]);
}
sort(cur.begin(),cur.end());
int pos=cur.size()/2-1;
x[u][0]=cur[pos]+c[u];
x[u][1]=cur[pos+1]+c[u];
long long t=cur[pos];
for(auto v:a[u]) {
if(t<x[v][0]) ans+=(x[v][0]-t);
if(t>x[v][1]) ans+=(t-x[v][1]);
}
}
int main() {
scanf("%d %d",&n,&m);
for(int i=2;i<=n+m;i++) {
scanf("%d %d",&p[i],&c[i]);
a[p[i]].push_back(i);
}
dfs(1);
printf("%lld\n",ans);
return 0;
}
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... |