Submission #820170

#TimeUsernameProblemLanguageResultExecution timeMemory
820170PenguinsAreCuteFireworks (APIO16_fireworks)C++17
100 / 100
276 ms73264 KiB
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define LL_MAX LONG_LONG_MAX
#define LL_MIN LONG_LONG_MIN
#define speed ios_base::sync_with_stdio(false); cin.tie(0)
#define stMx(a,b) a = max(a,b)
#define stMn(a,b) a = min(a,b)
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef set<int> si;
typedef vector<ii> vii;
typedef set<ii> sii;
#define REP(i, a, b) for(int i = a; i < b; i++)
#define HASTC 0
vii adj[300005];
priority_queue<int> slope[300005]; ii rgt[300005];
void dfs(int x, int p) {
    if(adj[x].size()) {
        for(auto i: adj[x]) {
            dfs(i.fi,i.se);
            if(slope[i.fi].size()>slope[x].size()) swap(slope[i.fi],slope[x]);
            while(slope[i.fi].size()) {
                slope[x].push(slope[i.fi].top());
                slope[i.fi].pop();
            }
            rgt[x].fi+=rgt[i.fi].fi; rgt[x].se+=rgt[i.fi].se;
        }
        while(rgt[x].fi>1) {
            rgt[x].fi--; assert(slope[x].size()); rgt[x].se+=slope[x].top();
            slope[x].pop();
        }
        int t, t2;
        t=slope[x].top(); slope[x].pop();
        t2=slope[x].top(); slope[x].pop();
        slope[x].push(t+p); slope[x].push(t2+p);
        rgt[x].se-=p;
    }
    else {
        slope[x].push(p);
        slope[x].push(p);
        rgt[x]=ii(1,-p);
    }
}
void solvetc(int idx) {
    int N, M, P, C; cin >> N >> M;
    REP(i,2,M+N+1) {
        cin>>P>>C;
        adj[P].pb(ii(i,C));
    }
    dfs(1,0);
    while(rgt[1].fi) {
        rgt[1].fi--; assert(slope[1].size()); rgt[1].se+=slope[1].top();
        slope[1].pop();
    }
    cout<<rgt[1].se;
}
int32_t main() {
    int tc;
    if(HASTC) cin>>tc;
    else tc=1;
    for(int i=0;i<tc;i++) solvetc(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...