Submission #163995

#TimeUsernameProblemLanguageResultExecution timeMemory
163995AkashiFireworks (APIO16_fireworks)C++14
100 / 100
264 ms51112 KiB
#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)

fireworks.cpp: In function 'int main()':
fireworks.cpp:44:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d%d", &n, &m);
     ~~~~~^~~~~~~~~~~~~~~~
fireworks.cpp:46:43: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     for(int i = 2; i <= n + m ; ++i) scanf("%d%d", &P[i], &C[i]);
                                      ~~~~~^~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...