Submission #500979

#TimeUsernameProblemLanguageResultExecution timeMemory
500979sidonMagic Tree (CEOI19_magictree)C++17
100 / 100
149 ms37624 KiB
#include <bits/stdc++.h> using namespace std; #define int long long signed main(){ ios_base::sync_with_stdio(0); cin.tie(0); int N, M, K, res = 0; cin >> N >> M >> K; vector<int> g[N]; int d[N] {}, w[N] {}; for(int i = 1; i < N; i++) { int p; cin >> p; g[p-1].push_back(i); } while(M--) { int u; cin >> u >> d[u-1] >> w[u-1]; } map<int, int> dp[N]; for(int u = N; --u >= 0; ) { int h = 0; for(int v : g[u]) if(size(dp[v]) > size(dp[h])) h = v; dp[h].swap(dp[u]); for(int v : g[u]) if(v != h) for(auto &[i, j] : dp[v]) dp[u][i] += j; dp[u][d[u]] += w[u]; auto f = dp[u].upper_bound(d[u]); while(f != end(dp[u]) && ((*f).second -= w[u]) < 0) w[u] = -(*f).second, f = dp[u].erase(f); } for(auto &[i, j] : dp[0]) res += j; cout << res; }
#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...