# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
153951 | songc | Fireworks (APIO16_fireworks) | C++14 | 367 ms | 75876 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int,int> pii;
int N, M;
LL C[303030], S[303030], Y[303030];
vector<int> adj[303030];
priority_queue<LL> PQ[303030];
void dfs(int u){
if (adj[u].empty()){
S[u] = -1, Y[u] = C[u];
PQ[u].push(C[u]);
PQ[u].push(C[u]);
return;
}
for (int v : adj[u]){
dfs(v);
S[u] += S[v], Y[u] += Y[v];
if (PQ[u].size() < PQ[v].size()) swap(PQ[u], PQ[v]);
while (!PQ[v].empty()){
PQ[u].push(PQ[v].top());
PQ[v].pop();
}
}
LL a=-1, b=-1;
Y[u] += C[u];
while (S[u] + PQ[u].size() > 1) PQ[u].pop();
if (S[u] + PQ[u].size() == 1) a=PQ[u].top(), PQ[u].pop();
if (S[u] + PQ[u].size() == 0) b=PQ[u].top(), PQ[u].pop();
if (a != -1) PQ[u].push(a+C[u]);
if (b != -1) PQ[u].push(b+C[u]);
}
int main(){
scanf("%d %d", &N, &M);
for (int i=2; i<=N+M; i++){
int p;
scanf("%d %lld", &p, &C[i]);
adj[p].push_back(i);
}
dfs(1);
LL x = PQ[1].top();
PQ[1].pop();
PQ[1].push(0);
for (int i=0; !PQ[1].empty(); i++){
Y[1] -= i * (x - PQ[1].top());
x = PQ[1].top();
PQ[1].pop();
}
printf("%lld\n", Y[1]);
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |