#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 int MAXN = 200100;
const int MAXK = 19;
int p[MAXN];
int H[MAXN],C[MAXN];
int N;
vi V[MAXN];
vi roots;
ll ans;
ll dp1[MAXN];
ll calc(ll x,ll c){
ll r=0;
if(H[x]>=c)r=C[x];
for(auto v:V[x])r=max(r,calc(v,c));
return r;
}
ll solve(int 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;
// if(x==2)exit(0);
return max(tx,r1);
}
int main(){
cin>>N;
for(int 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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4940 KB |
Output is correct |
2 |
Correct |
4 ms |
5008 KB |
Output is correct |
3 |
Correct |
4 ms |
4940 KB |
Output is correct |
4 |
Correct |
4 ms |
4940 KB |
Output is correct |
5 |
Incorrect |
12 ms |
5324 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
11 ms |
5276 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4940 KB |
Output is correct |
2 |
Correct |
4 ms |
5008 KB |
Output is correct |
3 |
Correct |
4 ms |
4940 KB |
Output is correct |
4 |
Correct |
4 ms |
4940 KB |
Output is correct |
5 |
Incorrect |
12 ms |
5324 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |