Submission #957215

#TimeUsernameProblemLanguageResultExecution timeMemory
957215thinknoexitFireworks (APIO16_fireworks)C++17
0 / 100
17 ms40540 KiB
#include <bits/stdc++.h> using namespace std; using ll = long long; const int N = 300300; bool leaf[N]; vector<pair<int, ll>> adj[N]; ll ansl[N], ansr[N], ans[N]; multiset<ll> l[N]; multiset<ll, greater<ll>> r[N]; void dfs(int v, ll d) { if (leaf[v]) { ansl[v] = d; ansr[v] = d; return; } for (auto& [x, w] : adj[v]) { dfs(x, d + w); ans[v] += ans[x]; if (l[v].empty()) { l[v].insert(ansl[x]); r[v].insert(ansr[x]); continue; } ll nowl = *l[v].begin(), nowr = *r[v].begin(); ll newl = ansl[x], newr = ansr[x]; if (newr < nowl) { ans[v] += nowl - newr; l[v].insert(newl); l[v].insert(newr); l[v].erase(l[v].begin()); r[v].insert(nowl); } else if (nowr < newl) { ans[v] += newl - nowr; r[v].insert(newl); r[v].insert(newr); r[v].erase(r[v].begin()); l[v].insert(nowr); } else { l[v].insert(newl); r[v].insert(newr); } } ansl[v] = *l[v].begin(); ansr[v] = *r[v].begin(); } int main() { cin.tie(nullptr)->sync_with_stdio(false); int n, m; cin >> n >> m; for (int i = 2;i <= n + m;i++) { int p; ll c; cin >> p >> c; adj[p].push_back({ i, c }); } for (int i = n + 1;i <= n + m;i++) leaf[i] = 1; dfs(1, 0ll); cout << ans[1] << '\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...