# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
99955 | TadijaSebez | Fireworks (APIO16_fireworks) | C++11 | 192 ms | 30184 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;
priority_queue<ll> pq[N];
int par[N],len[N],deg[N];
ll dp[N];
int main()
{
int n,m;
scanf("%i %i",&n,&m);
for(int i=2;i<=n+m;i++)
{
scanf("%i %i",&par[i],&len[i]);
deg[par[i]]++;
if(i>n)
{
dp[par[i]]-=len[i];
pq[par[i]].push(len[i]);
pq[par[i]].push(len[i]);
}
}
for(int i=n;i>=2;i--)
{
for(int j=1;j<=deg[i]-1;j++)
{
dp[i]+=pq[i].top();
pq[i].pop();
}
ll x=pq[i].top();
pq[i].pop();
ll y=pq[i].top();
pq[i].pop();
pq[i].push(x+len[i]);
pq[i].push(y+len[i]);
dp[i]-=len[i];
dp[par[i]]+=dp[i];
if(pq[par[i]].size()<pq[i].size()) pq[par[i]].swap(pq[i]);
while(pq[i].size()) pq[par[i]].push(pq[i].top()),pq[i].pop();
}
ll ans=dp[1];
for(int j=1;j<=deg[1];j++) ans+=pq[1].top(),pq[1].pop();
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... |