Submission #972704

#TimeUsernameProblemLanguageResultExecution timeMemory
972704huutuanWorst Reporter 4 (JOI21_worst_reporter4)C++14
79 / 100
396 ms524288 KiB
#include<bits/stdc++.h> using namespace std; #define int long long const int N=2e5+10; map<int, int> mp[N]; int n, a[N], h[N], c[N], sz[N]; vector<int> g[N]; void dfs_sz(int u){ sz[u]=1; for (int &v:g[u]){ dfs_sz(v); sz[u]+=sz[v]; if (sz[v]>sz[g[u][0]]) swap(v, g[u][0]); } } void dfs_merge(int u){ if (g[u].empty()){ mp[u][1]=c[u]; mp[u][h[u]+1]=-c[u]; return; } for (int &v:g[u]){ dfs_merge(v); if (v==g[u][0]) mp[u].swap(mp[v]); else{ for (auto &i:mp[v]){ mp[u][i.first]+=i.second; } } } mp[u][h[u]+1]-=c[u]; auto it=mp[u].find(h[u]+1); int cur=c[u]; while (it!=mp[u].begin()){ --it; if (it->second<0 && cur>=-it->second){ cur-=-it->second; it=mp[u].erase(it); }else{ it->second+=cur; cur=0; break; } } } int32_t main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> n; vector<int> vals{-1}; for (int i=1; i<=n; ++i){ cin >> a[i] >> h[i] >> c[i]; vals.push_back(h[i]); if (a[i]!=i) g[a[i]].push_back(i); } sort(vals.begin(), vals.end()); vals.resize(unique(vals.begin(), vals.end())-vals.begin()); for (int i=1; i<=n; ++i) h[i]=lower_bound(vals.begin(), vals.end(), h[i])-vals.begin(); dfs_sz(1); dfs_merge(1); cout << accumulate(c+1, c+n+1, 0ll)-mp[1][1] << '\n'; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...