# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
99946 | TadijaSebez | Fireworks (APIO16_fireworks) | C++11 | 10 ms | 7464 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>
using namespace std;
#define ll long long
#define pb push_back
const int N=300050;
int par[N],len[N];
ll dep[N],dp[N],sz[N];
vector<int> E[N];
void DFS(int u)
{
if(E[u].size()==0) return;
vector<ll> work;
for(int v:E[u])
{
DFS(v);
dp[u]+=dp[v];
work.pb(sz[v]+len[v]);
}
sort(work.begin(),work.end());
int mid=(work.size()+1)/2;
sz[u]=work[mid-1];
for(ll p:work) dp[u]+=abs(sz[u]-p);
//printf("u:%i dp:%lld sz:%lld\n",u,dp[u],sz[u]);
}
int main()
{
int n,m;
scanf("%i %i",&n,&m);
n+=m;
for(int i=2;i<=n;i++)
{
scanf("%i %i",&par[i],&len[i]);
dep[i]=dep[par[i]]+len[i];
E[par[i]].pb(i);
}
DFS(1);
printf("%lld\n",dp[1]);
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... |