Submission #833961

#TimeUsernameProblemLanguageResultExecution timeMemory
833961MODDIMagic Tree (CEOI19_magictree)C++14
100 / 100
136 ms40660 KiB
#include <bits/stdc++.h> using namespace std; #define pb push_back #define mp make_pair typedef long long ll; typedef pair<long long, long long> pll; typedef pair<int,int> pii; typedef vector<long long> vl; typedef vector<int> vi; int n, m, k; vi G[100100]; vector<pll> fruits; map<int, ll> dp[100100]; void dfs(int node, int p){ // cout<<"AT: "<<node<<endl; for(auto next : G[node]){ if(next !=p){ dfs(next, node); if(dp[node].size() < dp[next].size()) swap(dp[node], dp[next]); for(auto t : dp[next]) dp[node][t.first] += t.second; } } if(fruits[node].first != -1){ ll hold = fruits[node].second; while(hold > 0){ auto rem = dp[node].upper_bound(fruits[node].first); if(rem == dp[node].end()) break; if(rem->second <= hold){ hold -= rem->second; dp[node].erase(rem); } else{ rem->second -= hold; break; } } dp[node][fruits[node].first] += fruits[node].second; } } int main(){ ios_base::sync_with_stdio(false); cin.tie(0); cin>>n>>m>>k; for(int i = 2; i <= n; i++){ int a; cin>>a; G[a-1].pb(i-1); G[i-1].pb(a-1); } fruits.resize(n+1); for(int i = 0; i < n; i++) fruits[i] = mp(-1, -1); for(int i = 0; i < m; i++){ int node, day, add; cin>>node>>day>>add; node--; fruits[node] = mp(day, add); } ll ans = 0; dfs(0, -1); // cout<<dp[0].size()<<endl; for(auto t : dp[0]) ans += t.second; cout<<ans<<endl; 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...