Submission #579433

#TimeUsernameProblemLanguageResultExecution timeMemory
579433tengiz05Magic Tree (CEOI19_magictree)C++17
100 / 100
153 ms37740 KiB
#include <bits/stdc++.h> using namespace std; using i64 = long long; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, m, k; cin >> n >> m >> k; vector<int> p(n, -1); vector<vector<int>> e(n); for (int i = 1; i < n; i++) { cin >> p[i]; p[i]--; e[p[i]].push_back(i); } vector<int> w(n), d(n, k + 1); for (int i = 0; i < m; i++) { int v; cin >> v; v--; cin >> d[v] >> w[v]; } vector<map<int, i64>> g(n); function<void(int)> dfs = [&](int u) { for (int v : e[u]) { dfs(v); if (g[v].size() > g[u].size()) { swap(g[u], g[v]); } for (auto [x, y] : g[v]) { g[u][x] += y; } } g[u][d[u]] += w[u]; i64 x = 0; while (true) { auto it = g[u].upper_bound(d[u]); if (it == g[u].end()) break; if (it->second <= w[u] - x) { x += it->second; g[u].erase(it); } else { it->second -= w[u]; it->second += x; break; } } }; dfs(0); i64 ans = 0; i64 sum = 0; for (auto [x, y] : g[0]) { sum += y; ans = max(ans, sum); } 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...