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>
using namespace std;
typedef long long ll;
const int MX=2e5+5;
int N;
int A[MX],H[MX],C[MX];
vector<int> adj[MX];
multiset<pair<ll,ll>> dp[MX];
int par[MX];
int find(int v) {
return par[v]==v?v:par[v]=find(par[v]);
}
void merge(int u, int v) {
u=find(u),v=find(v);
if(u==v) return;
if(dp[u].size()>dp[v].size()) swap(u,v);
for(auto [pos,val]:dp[u]) {
auto it=dp[v].lower_bound(make_pair(pos,0));
dp[v].insert({pos,val});
}
par[u]=v;
dp[u].clear();
}
void dfs(int v, int p) {
int prv=-1;
for(auto u:adj[v]) {
if(u==p) continue;
dfs(u,v);
if(prv!=-1)
merge(u,prv);
prv=u;
}
if(prv!=-1) merge(v,prv);
int pv=find(v);
auto it=dp[pv].lower_bound(make_pair(H[v],0));
if(it==dp[pv].begin()) {
dp[pv].insert(make_pair(H[v],C[v]));
} else {
it--;
ll cur=C[v];
while(true) {
if(cur>=it->second) {
cur-=it->second;
if(it==dp[pv].begin()) {
dp[pv].erase(it);
break;
} else {
it=dp[prv].erase(it);
}
} else {
dp[pv].insert({it->first,it->second-cur});
dp[pv].erase(it);
cur=0;
break;
}
}
dp[pv].insert(make_pair(H[v],C[v]));
}
}
int main() {
cin.tie(0); ios_base::sync_with_stdio(0);
cin>>N;
ll sum=0;
for(int i=1;i<=N;i++) {
cin>>A[i]>>H[i]>>C[i];
sum+=C[i];
}
for(int i=2;i<=N;i++) {
adj[A[i]].push_back(i);
}
for(int i=1;i<=N;i++) par[i]=i;
dfs(1,0);
ll ans=0;
for(auto [pos,val]:dp[find(1)]) {
ans+=val;
}
cout<<sum-ans<<'\n';
}
Compilation message (stderr)
worst_reporter2.cpp: In function 'void merge(int, int)':
worst_reporter2.cpp:26:8: warning: variable 'it' set but not used [-Wunused-but-set-variable]
26 | auto it=dp[v].lower_bound(make_pair(pos,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... |