Submission #1205238

#TimeUsernameProblemLanguageResultExecution timeMemory
1205238woohyun_jngFireworks (APIO16_fireworks)C++20
100 / 100
127 ms80204 KiB
#include <bits/stdc++.h>
#define int long long

using namespace std;
typedef array<int, 2> pr;

const int MAX = 300001;
typedef array<int, 3> tp;

int N, M, V[MAX];
vector<int> adj[MAX];

priority_queue<int> dfs(int node) {
    priority_queue<int> pq, tmp;
    int X, Y;

    if (node > N)
        pq.push(0), pq.push(0);

    for (int i : adj[node]) {
        tmp = dfs(i);
        if (tmp.size() > pq.size())
            swap(tmp, pq);
        while (!tmp.empty())
            pq.push(tmp.top()), tmp.pop();
    }

    for (int i = 0; i + 1 < adj[node].size(); i++)
        pq.pop();

    X = pq.top(), pq.pop(), Y = pq.top(), pq.pop();
    pq.push(X + V[node]), pq.push(Y + V[node]);

    return pq;
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);

    int X, ans = 0;
    priority_queue<int> pq;

    cin >> N >> M;

    for (int i = 2; i <= N + M; i++)
        cin >> X >> V[i], adj[X].push_back(i), ans += V[i];

    pq = dfs(1), pq.pop();
    while (!pq.empty())
        ans -= pq.top(), pq.pop();
    cout << ans << '\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...