This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//by szh
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pii pair<int,int>
#define pll pair<long long,long long>
#define pb push_back
#define debug(x) cerr<<#x<<"="<<x<<endl
#define pq priority_queue
#define inf 0x3f
#define rep(i,a,b) for (int i=a;i<(b);i++)
#define MP make_pair
#define SZ(x) (int(x.size()))
#define ll long long
#define big 1000000000
#define ALL(x) x.begin(),x.end()
int lowbit(int x) {return x&(-x);}
const int maxn=2e5+10;
int n;
vector <int> edge[maxn];
int h[maxn];
int c[maxn];
void combine(map<int,ll> &a,map<int,ll> b) {
auto cur=a.begin();
ll tmp=0;
for (auto it=b.begin();it!=b.end();it++) {
tmp+=it->se;
cur->se+=tmp;
cur=a.upper_bound(it->fi);
if (cur!=a.end()) cur->se-=tmp;
if (a.find(it->fi)==a.end()) {
if (cur==a.end()) break;
a[it->fi]=cur->se+tmp;
cur->se=-tmp;
}
}
while (cur!=a.end()) cur=a.erase(cur);
return;
}
map<int,ll> solve(int u) {
map<int,ll> ret;
if (edge[u].empty()) {
ret[big]=c[u];
ret[h[u]]=0;
return ret;
}
ret=solve(edge[u][0]);
rep(i,1,SZ(edge[u])) {
auto cur=solve(edge[u][i]);
if (SZ(ret)<SZ(cur)) swap(cur,ret);
combine(ret,cur);
}
// debug(ret.begin()->fi);debug(ret.begin()->se);
auto it=ret.lower_bound(h[u]);
if ((it->fi)!=h[u]) {
ret[h[u]]=(it->se);
it->se=0;
}
it=ret.find(h[u]);
while (it!=ret.begin()&&((it->se)<=c[u])) {
auto tmp=it;tmp--;
it->se+=tmp->se;
it=ret.erase(tmp);
}
it->se-=(ll)c[u];
ret.begin()->se+=(ll)c[u];
it=ret.upper_bound(h[u]);
it->se+=(ll)c[u];
// debug(u);
// for (auto it=ret.begin();it!=ret.end();it++) cout<<(it->fi)<<" "<<(it->se)<<endl;
return ret;
}
int main() {
// freopen("input.txt","r",stdin);
std::ios::sync_with_stdio(false);cin.tie();
cin>>n;
h[0]=c[0]=0;
rep(i,1,n+1) {
int x;
cin>>x>>h[i]>>c[i];
if (x==i) edge[0].pb(i);
else edge[x].pb(i);
}
cout<<(*solve(0).begin()).se;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |