Submission #594006

#TimeUsernameProblemLanguageResultExecution timeMemory
594006penguinhackerMagic Tree (CEOI19_magictree)C++17
100 / 100
107 ms35908 KiB
#include <bits/stdc++.h> using namespace std; #define ll long long #define ar array const int mxN=1e5; int n, m, k; ar<int, 2> fruit[mxN]; vector<int> adj[mxN]; map<int, ll> dp[mxN]; void dfs(int u=0) { for (int v : adj[u]) { dfs(v); if (dp[v].size()>dp[u].size()) swap(dp[u], dp[v]); for (auto x : dp[v]) dp[u][x.first]+=x.second; map<int, ll>().swap(dp[v]); } if (fruit[u][0]) { dp[u][fruit[u][0]]+=fruit[u][1]; auto it=dp[u].find(fruit[u][0]); int rem=fruit[u][1]; for (++it; it!=dp[u].end()&&rem>=it->second; it=dp[u].erase(it)) rem-=it->second; if (it!=dp[u].end()) it->second-=rem; } } int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n >> m >> k; for (int i=1; i<n; ++i) { int p; cin >> p, --p; adj[p].push_back(i); } for (int i=0; i<m; ++i) { int u, d, w; cin >> u >> d >> w, --u; fruit[u]={d, w}; } dfs(); ll ans=0; for (auto x : dp[0]) ans+=x.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...