Submission #646272

#TimeUsernameProblemLanguageResultExecution timeMemory
646272PetyMagic Tree (CEOI19_magictree)C++14
100 / 100
136 ms36028 KiB
#include <bits/stdc++.h> #define ll long long using namespace std; const ll INF = 1e9; const ll MOD = 1e9 + 9; ll n, m, w[100002], d[100002], k; vector<ll>G[100002]; map<ll, ll>aib[100002]; void dfs (ll nod) { for (auto it : G[nod]) { dfs(it); if (aib[nod].size() < aib[it].size()) swap(aib[nod], aib[it]); for (auto it2 : aib[it]) aib[nod][it2.first] += it2.second; } if (w[nod]) { aib[nod][d[nod]] += w[nod]; ll x = 0; while (1) { auto it = aib[nod].upper_bound(d[nod]); if (it == aib[nod].end()) break; if (it->second <= w[nod] - x) { x += it->second; aib[nod].erase(it); } else { it->second -= w[nod]; it->second += x; break; } } } } int main () { ios_base::sync_with_stdio(false); cin >> n >> m >> k; for (ll i = 2; i <= n; i++) { ll x; cin >> x; G[x].push_back(i); } for (ll i = 1; i <= m; i++) { ll v; cin >> v; cin >> d[v] >> w[v]; } dfs(1); ll ans = 0; for (auto it : aib[1]) ans += it.second; cout << ans; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...