Submission #626737

#TimeUsernameProblemLanguageResultExecution timeMemory
626737elalabajFireworks (APIO16_fireworks)C++17
100 / 100
236 ms75596 KiB
#include <bits/stdc++.h> typedef long long ll; struct V { ll w = 0; std::vector<int> edges; ll y0 = 0; ll d0 = 0; std::priority_queue<ll> points; }; void solve(ll i, std::vector<V>& graph) { V& v = graph[i]; if (v.edges.empty()) return; for (auto j : v.edges) { solve(j, graph); V& w = graph[j]; v.y0 += w.y0; v.d0 += w.d0; if (v.points.size() < w.points.size()) { std::swap(v.points, w.points); } while (!w.points.empty()) { v.points.push(w.points.top()); w.points.pop(); } } if (i == 1) return; v.y0 += v.w; ll pv1 = v.points.top(); v.points.pop(); ll pv2 = v.points.top(); v.points.pop(); while (v.d0 + (ll)v.points.size() > -1) { pv1 = pv2; pv2 = v.points.top(); v.points.pop(); } v.points.push(pv2 + v.w); v.points.push(pv1 + v.w); } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); std::cout.tie(nullptr); ll n, m; std::cin >> n >> m; std::vector<V> graph(n + m + 1); for (ll i = 2; i <= n + m; i++) { ll a, b; std::cin >> a >> b; graph[a].edges.push_back(i); graph[i].w = b; } for (ll i = n + 1; i <= n + m; i++) { graph[i].y0 = graph[i].w; graph[i].d0 = -1; graph[i].points.push(graph[i].w); graph[i].points.push(graph[i].w); } solve(1, graph); std::vector<ll> points; while (!graph[1].points.empty()) { points.push_back(graph[1].points.top()); graph[1].points.pop(); } std::reverse(points.begin(), points.end()); ll x = 0; ll y = graph[1].y0; ll d = graph[1].d0; ll min = y; for (auto p : points) { y += (ll) d * (p - x); d++; x = p; min = std::min(min, y); } std::cout << min << '\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...