제출 #406178

#제출 시각아이디문제언어결과실행 시간메모리
406178dvdg6566Worst Reporter 4 (JOI21_worst_reporter4)C++14
14 / 100
2072 ms30840 KiB
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pi;
typedef vector<ll> vi;
typedef vector<pi> vpi;
#define pb emplace_back
#define f first 
#define s second
#define mp make_pair
#define SZ(x) (ll)x.size()
#define ALL(x) x.begin(),x.end()
#define lb lower_bound
#define ub upper_bound

const ll MAXN = 200100;
const ll MAXK = 19;
ll p[MAXN];
ll H[MAXN],C[MAXN];
ll N;
vi V[MAXN];
vi roots;
ll ans;
ll dp1[MAXN];
ll dp2[MAXN];

ll calc(ll x,ll c){
	ll r=0;
	// if you don't choose it
	for(auto v:V[x])r+=calc(v,c);
	if(H[x]>=c)r=max(r,dp1[x]); // best you can get by choosing that node
	return r;
}

ll solve(ll x){
	ll r1=0;
	for(auto v:V[x])r1+=solve(v);
	// if you choose to continue, everything else chosen must be not less
	ll tx=C[x];
	for(auto v:V[x]){
		// cerr<<"Calc "<<v<<' '<<calc(v,H[x])<<'\n';
		tx+=calc(v,H[x]);
	}
	// cerr<<"DP "<<x<<' '<<tx<<' '<<r1<<'\n';
	dp1[x]=tx; // best you can get by choosing x
	dp2[x]=r1; // best you can get by NOT choosing x
	return max(dp1[x],dp2[x]);
}

int main(){
	cin>>N;
	for(ll i=1;i<=N;++i){
		cin>>p[i]>>H[i]>>C[i];
		if(p[i]==i){
			p[i]=-1;
			roots.pb(i);
		}else V[p[i]].pb(i);
		ans+=C[i];
	}
	for(auto i:roots){
		ans-=solve(i);
	}
	cout<<ans;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...