Submission #579384

#TimeUsernameProblemLanguageResultExecution timeMemory
579384amunduzbaevMagic Tree (CEOI19_magictree)C++17
15 / 100
1356 ms1048576 KiB
#include "bits/stdc++.h" using namespace std; #define ar array typedef int64_t ll; #define int ll const int N = 1e5 + 5; vector<int> edges[N]; int is[N], d[N], w[N], mx[N]; map<int, int> sub[N]; void dfs(int u){ mx[u] = u; for(auto x : edges[u]){ dfs(x); if(sub[x].size() > sub[mx[u]].size()) mx[u] = x; } swap(sub[u], sub[mx[u]]); for(auto x : edges[u]){ if(x == mx[u]) continue; for(auto v : sub[x]){ sub[u][v.first] += v.second; } } if(is[u]){ sub[u][d[u]] += w[u]; auto it = sub[u].upper_bound(d[u]); vector<int> er; int sum = 0; while(it != sub[u].end()){ sum += (*it).second; if(sum >= w[u]){ sub[u][(*it).first] = sum - w[u]; break; } er.push_back((*it).first); } for(auto x : er){ sub[u].erase(x); } } } signed main(){ ios::sync_with_stdio(0); cin.tie(0); int n, m, k; cin>>n>>m>>k; for(int i=2;i<=n;i++){ int x; cin>>x; edges[x].push_back(i); } for(int i=0;i<m;i++){ int v; cin>>v; cin>>d[v]>>w[v]; is[v] = 1; } dfs(1); int sum = 0; for(auto x : sub[1]) sum += x.second; cout<<sum<<"\n"; }
#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...