# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
163995 | Akashi | Fireworks (APIO16_fireworks) | C++14 | 264 ms | 51112 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;
struct graph{
long long a, b;
priority_queue <long long> *pq;
graph operator+(graph aux){
graph sum;
sum.a = a + aux.a;
sum.b = b + aux.b;
if(pq->size() < aux.pq->size()){
sum.pq = aux.pq;
while(!pq->empty()){
sum.pq->push(pq->top());
pq->pop();
}
}
else{
sum.pq = pq;
while(!aux.pq->empty()){
sum.pq->push(aux.pq->top());
aux.pq->pop();
}
}
return sum;
}
};
graph d[300005];
int n, m;
int P[300005], C[300005];
int main()
{
// freopen("1.in", "r", stdin);
scanf("%d%d", &n, &m);
for(int i = 2; i <= n + m ; ++i) scanf("%d%d", &P[i], &C[i]);
for(int i = 1; i <= n + m ; ++i) d[i].pq = new priority_queue <long long>;
for(int i = n + 1; i <= n + m ; ++i){
d[i].a = 1; d[i].b = -C[i];
d[i].pq->push(C[i]);
d[i].pq->push(C[i]);
d[P[i]] = d[P[i]] + d[i];
}
for(int i = n; i > 1 ; --i){
while(d[i].a > 1){
--d[i].a;
d[i].b += d[i].pq->top();
d[i].pq->pop();
}
long long x, y;
x = d[i].pq->top();
d[i].pq->pop();
y = d[i].pq->top();
d[i].pq->pop();
d[i].pq->push(x + C[i]);
d[i].pq->push(y + C[i]);
d[i].b -= C[i];
d[P[i]] = d[P[i]] + d[i];
}
while(d[1].a > 0){
--d[1].a;
d[1].b += d[1].pq->top();
d[1].pq->pop();
}
printf("%lld", d[1].b);
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... |