Submission #873046

#TimeUsernameProblemLanguageResultExecution timeMemory
873046NeroZeinMagic Tree (CEOI19_magictree)C++17
58 / 100
101 ms35412 KiB
#include "bits/stdc++.h" using namespace std; #ifdef Nero #include "Deb.h" #else #define deb(...) #endif const int N = 1e5 + 5; vector<int> g[N]; pair<int, long long> a[N]; map<int, long long> dp[N]; void merge(map<int, long long>& du, map<int, long long>& dv) { if (du.size() > dv.size()) { swap(du, dv); } for (auto it : du) { dv[it.first] += it.second; } } void dfs(int v) { for (int u : g[v]) { dfs(u); merge(dp[u], dp[v]); } if (!a[v].first) return; long long tmp = a[v].second; dp[v][a[v].first] += a[v].second; auto it = dp[v].upper_bound(a[v].first); while (tmp && it != dp[v].end()) { if (tmp >= it->second) { tmp -= it->second; dp[v].erase(it); } else { it->second -= tmp; tmp = 0; } } } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, m, k; cin >> n >> m >> k; for (int i = 2; i <= n; ++i) { int p; cin >> p; g[p].push_back(i); } for (int i = 0; i < m; ++i) { int v, t; long long w; cin >> v >> t >> w; a[v] = {t, w}; } dfs(1); long long ans = 0; for (auto it : dp[1]) { ans += it.second; } cout << ans << '\n'; 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...