Submission #387049

#TimeUsernameProblemLanguageResultExecution timeMemory
387049ogibogi2004Worst Reporter 4 (JOI21_worst_reporter4)C++14
14 / 100
2058 ms65228 KiB
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const ll MAXN=2e5+6;
ll n;
vector<ll>g[MAXN];
ll a[MAXN],h[MAXN],c[MAXN],subtree[MAXN];
void dfs1(ll u,ll p)
{
	subtree[u]=1;
	for(auto xd:g[u])
	{
		if(xd==p)continue;
		dfs1(xd,u);
		subtree[u]+=subtree[xd];
	}
}
void dfs(ll u,ll p,map<ll,ll> &mp)
{
	ll bc=-1,maxst=-1;
	for(auto xd:g[u])
	{
		if(xd==p)continue;
		if(subtree[xd]>maxst)
		{
			maxst=subtree[xd];
			bc=xd;
		}
	}
	if(bc==-1)
	{
		mp[h[u]+1]=c[u];
		return;
	}
	dfs(bc,u,mp);
	for(auto xd:g[u])
	{
		if(xd==p||xd==bc)continue;
		map<ll,ll> tmp;
		dfs(xd,u,tmp);
		for(auto it:tmp)
		{
			mp[it.first]+=it.second;
		}
	}
	mp[h[u]+1]+=c[u];
	mp[0]+=c[u];
	mp[h[u]]-=c[u];
	auto it=mp.lower_bound(h[u]);
	while(mp[h[u]]<0)
	{
		it--;
		ll t=(*it).first;
		if(mp[t]+mp[h[u]]>=0)
		{
			mp[t]+=mp[h[u]];
			mp[h[u]]=0;
		}
		else
		{
			mp[h[u]]+=mp[t];
			mp[t]=0;
		}
	}
}
int main()
{
	cin>>n;
	for(ll i=1;i<=n;++i)
	{
		cin>>a[i]>>h[i]>>c[i];
		if(i!=1)g[a[i]].push_back(i);
	}
	dfs1(1,0);
	map<ll,ll>mp;
	dfs(1,0,mp);
	cout<<mp[0]<<endl;
return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...