Submission #99955

#TimeUsernameProblemLanguageResultExecution timeMemory
99955TadijaSebezFireworks (APIO16_fireworks)C++11
100 / 100
192 ms30184 KiB
#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)

fireworks.cpp: In function 'int main()':
fireworks.cpp:12:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%i %i",&n,&m);
  ~~~~~^~~~~~~~~~~~~~~
fireworks.cpp:15:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%i %i",&par[i],&len[i]);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...