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>
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define plx pair<ll,int>
#define f first
#define s second
#define pb push_back
#define all(x) x.begin(),x.end()
#define vi vector<int>
#define vvi vector<vi>
#define pp pair<ll,int>
#define ub(x,i) upper_bound(all(x),i)-x.begin()
using namespace std;
const int mxn=2e5+5;
vector<int> g[mxn];
int a[mxn],h[mxn],c[mxn];
map<int,ll>dp[mxn];
bool vis[mxn]{0};
void dfs(int u){
vis[u]=1;
for(auto v:g[u]){
if(vis[v])continue;
dfs(v);
if(dp[v].size()>dp[u].size())swap(dp[u],dp[v]);
for(auto it:dp[v])dp[u][it.f]+=it.s;
}
auto it = dp[u].lower_bound(h[u]);
int cur=c[u];
for(;it!=dp[u].end();){
if(cur>=it->s){cur-=it->s;auto ij=it;it++;dp[u].erase(ij);}
else {it->s-=cur;break;}
}dp[u][h[u]]+=c[u];
}
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);
int n;cin>>n;ll rs=0;
for(int i=1;i<=n;i++){
cin>>a[i]>>h[i]>>c[i];h[i]=-h[i];
g[a[i]].pb(i);rs+=c[i];
}dfs(1);ll rs2=0;
for(auto it:dp[1])rs2+=it.s;
cout<<rs-rs2;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |