Submission #155871

#TimeUsernameProblemLanguageResultExecution timeMemory
155871Alexa2001Fireworks (APIO16_fireworks)C++17
100 / 100
308 ms52840 KiB
#include <bits/stdc++.h> using namespace std; const int Nmax = 3e5 + 5; typedef long long ll; ll A[Nmax], B[Nmax]; int up[Nmax], w[Nmax]; priority_queue<ll> heap[Nmax]; vector<int> edge[Nmax]; int n, m, N; void combine(int node, int son) { A[node] += A[son]; B[node] += B[son]; while(heap[son].size()) { heap[node].push(heap[son].top()); heap[son].pop(); } } void merge_sons(int node) { int xson = 0; for(auto son : edge[node]) { if(w[son] > w[xson]) xson = son; w[node] += w[son]; } swap(heap[node], heap[xson]); A[node] = A[xson]; B[node] = B[xson]; for(auto son : edge[node]) if(son != xson) combine(node, son); } int main() { // freopen("input", "r", stdin); cin.sync_with_stdio(false); cin.tie(0); cin >> n >> m; N = n + m; int i, node; for(i=2; i<=N; ++i) { int parent; cin >> parent >> up[i]; edge[parent].push_back(i); } for(node = N; node; --node) { w[node] = 1; if(edge[node].empty()) { heap[node].push(up[node]); heap[node].push(up[node]); A[node] = 1; B[node] = -up[node]; continue; } merge_sons(node); while(A[node] > 1) { ll last_change = heap[node].top(); heap[node].pop(); A[node]--; B[node] += last_change; } ll change0, change1; change1 = heap[node].top(); heap[node].pop(); change0 = heap[node].top(); heap[node].pop(); heap[node].push(change1 + up[node]); heap[node].push(change0 + up[node]); B[node] -= up[node]; } cout << A[1] * heap[1].top() + B[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...