Submission #558628

# Submission time Handle Problem Language Result Execution time Memory
558628 2022-05-07T16:27:33 Z Alexandruabcde Fireworks (APIO16_fireworks) C++14
100 / 100
245 ms 71872 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long LL;
constexpr int NMAX = 3e5 + 5;

struct SlopeTrick {
    LL a, b;
    priority_queue <LL> *H;

    SlopeTrick () {
        a = 0;
        b = 0;
        H = new priority_queue<LL>;
    }

    SlopeTrick operator += (SlopeTrick r) {
        a += r.a;
        b += r.b;

        if (H->size() < r.H->size())
            swap(H, r.H);

        while (!r.H->empty()) {
            H->push(r.H->top());
            r.H->pop();
        }

        return *this;
    }
};

int N, M;
int Dad[NMAX];
LL Cost[NMAX];
vector <int> G[NMAX];

void Read () {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    cin >> N >> M;
    for (int i = 2; i <= N + M; ++ i ) {
        cin >> Dad[i] >> Cost[i];

        G[Dad[i]].push_back(i);
    }
}

void Solve (int Node, SlopeTrick &ans) {
    while (ans.a > 1) {
        LL x = ans.H->top();
        ans.H->pop();
        -- ans.a;

        ans.b += x;
    }

    LL slope_1 = ans.H->top();
    ans.H->pop();
    LL slope_0 = ans.H->top();
    ans.H->pop();

    ans.H->push(slope_0 + Cost[Node]);
    ans.H->push(slope_1 + Cost[Node]);
    ans.b -= Cost[Node];
}

void Dfs (int Node, SlopeTrick &ans) {
    bool leaf = true;
    for (auto it : G[Node]) {
        SlopeTrick aux;

        leaf = false;
        Dfs(it, aux);
        ans += aux;
    }

    if (leaf) {
        ans.a = 1;
        ans.b = -Cost[Node];
        ans.H->push(Cost[Node]);
        ans.H->push(Cost[Node]);

        return;
    }

    if (Node == 1) return;

    Solve(Node, ans);
}

int main () {
    Read();

    SlopeTrick ans;

    Dfs(1, ans);

    while (ans.a > 0) {
        LL x = ans.H->top();
        ans.H->pop();

        ans.b += x;
        -- ans.a;
    }

    cout << ans.b << '\n';

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 4 ms 7252 KB Output is correct
3 Correct 4 ms 7380 KB Output is correct
4 Correct 4 ms 7380 KB Output is correct
5 Correct 5 ms 7380 KB Output is correct
6 Correct 5 ms 7380 KB Output is correct
7 Correct 4 ms 7380 KB Output is correct
8 Correct 5 ms 7380 KB Output is correct
9 Correct 4 ms 7380 KB Output is correct
10 Correct 4 ms 7380 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 4 ms 7380 KB Output is correct
3 Correct 4 ms 7380 KB Output is correct
4 Correct 4 ms 7380 KB Output is correct
5 Correct 6 ms 7392 KB Output is correct
6 Correct 4 ms 7380 KB Output is correct
7 Correct 5 ms 7380 KB Output is correct
8 Correct 4 ms 7380 KB Output is correct
9 Correct 5 ms 7380 KB Output is correct
10 Correct 5 ms 7380 KB Output is correct
11 Correct 4 ms 7380 KB Output is correct
12 Correct 4 ms 7380 KB Output is correct
13 Correct 4 ms 7380 KB Output is correct
14 Correct 4 ms 7380 KB Output is correct
15 Correct 4 ms 7380 KB Output is correct
16 Correct 5 ms 7408 KB Output is correct
17 Correct 4 ms 7380 KB Output is correct
18 Correct 4 ms 7380 KB Output is correct
19 Correct 5 ms 7380 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 4 ms 7252 KB Output is correct
3 Correct 4 ms 7380 KB Output is correct
4 Correct 4 ms 7380 KB Output is correct
5 Correct 5 ms 7380 KB Output is correct
6 Correct 5 ms 7380 KB Output is correct
7 Correct 4 ms 7380 KB Output is correct
8 Correct 5 ms 7380 KB Output is correct
9 Correct 4 ms 7380 KB Output is correct
10 Correct 4 ms 7380 KB Output is correct
11 Correct 4 ms 7380 KB Output is correct
12 Correct 4 ms 7380 KB Output is correct
13 Correct 4 ms 7380 KB Output is correct
14 Correct 4 ms 7380 KB Output is correct
15 Correct 6 ms 7392 KB Output is correct
16 Correct 4 ms 7380 KB Output is correct
17 Correct 5 ms 7380 KB Output is correct
18 Correct 4 ms 7380 KB Output is correct
19 Correct 5 ms 7380 KB Output is correct
20 Correct 5 ms 7380 KB Output is correct
21 Correct 4 ms 7380 KB Output is correct
22 Correct 4 ms 7380 KB Output is correct
23 Correct 4 ms 7380 KB Output is correct
24 Correct 4 ms 7380 KB Output is correct
25 Correct 4 ms 7380 KB Output is correct
26 Correct 5 ms 7408 KB Output is correct
27 Correct 4 ms 7380 KB Output is correct
28 Correct 4 ms 7380 KB Output is correct
29 Correct 5 ms 7380 KB Output is correct
30 Correct 4 ms 7380 KB Output is correct
31 Correct 4 ms 7504 KB Output is correct
32 Correct 4 ms 7508 KB Output is correct
33 Correct 7 ms 7508 KB Output is correct
34 Correct 7 ms 7636 KB Output is correct
35 Correct 6 ms 7764 KB Output is correct
36 Correct 8 ms 7764 KB Output is correct
37 Correct 6 ms 7764 KB Output is correct
38 Correct 6 ms 7892 KB Output is correct
39 Correct 10 ms 7892 KB Output is correct
40 Correct 5 ms 8404 KB Output is correct
41 Correct 6 ms 8444 KB Output is correct
42 Correct 8 ms 7820 KB Output is correct
43 Correct 8 ms 8148 KB Output is correct
44 Correct 9 ms 8072 KB Output is correct
45 Correct 8 ms 7996 KB Output is correct
46 Correct 6 ms 7892 KB Output is correct
47 Correct 6 ms 8000 KB Output is correct
48 Correct 6 ms 7900 KB Output is correct
49 Correct 7 ms 7892 KB Output is correct
50 Correct 6 ms 7764 KB Output is correct
51 Correct 6 ms 7764 KB Output is correct
52 Correct 6 ms 7848 KB Output is correct
53 Correct 7 ms 7920 KB Output is correct
54 Correct 8 ms 7892 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 4 ms 7252 KB Output is correct
3 Correct 4 ms 7380 KB Output is correct
4 Correct 4 ms 7380 KB Output is correct
5 Correct 5 ms 7380 KB Output is correct
6 Correct 5 ms 7380 KB Output is correct
7 Correct 4 ms 7380 KB Output is correct
8 Correct 5 ms 7380 KB Output is correct
9 Correct 4 ms 7380 KB Output is correct
10 Correct 4 ms 7380 KB Output is correct
11 Correct 4 ms 7380 KB Output is correct
12 Correct 4 ms 7380 KB Output is correct
13 Correct 4 ms 7380 KB Output is correct
14 Correct 4 ms 7380 KB Output is correct
15 Correct 6 ms 7392 KB Output is correct
16 Correct 4 ms 7380 KB Output is correct
17 Correct 5 ms 7380 KB Output is correct
18 Correct 4 ms 7380 KB Output is correct
19 Correct 5 ms 7380 KB Output is correct
20 Correct 5 ms 7380 KB Output is correct
21 Correct 4 ms 7380 KB Output is correct
22 Correct 4 ms 7380 KB Output is correct
23 Correct 4 ms 7380 KB Output is correct
24 Correct 4 ms 7380 KB Output is correct
25 Correct 4 ms 7380 KB Output is correct
26 Correct 5 ms 7408 KB Output is correct
27 Correct 4 ms 7380 KB Output is correct
28 Correct 4 ms 7380 KB Output is correct
29 Correct 5 ms 7380 KB Output is correct
30 Correct 4 ms 7380 KB Output is correct
31 Correct 4 ms 7504 KB Output is correct
32 Correct 4 ms 7508 KB Output is correct
33 Correct 7 ms 7508 KB Output is correct
34 Correct 7 ms 7636 KB Output is correct
35 Correct 6 ms 7764 KB Output is correct
36 Correct 8 ms 7764 KB Output is correct
37 Correct 6 ms 7764 KB Output is correct
38 Correct 6 ms 7892 KB Output is correct
39 Correct 10 ms 7892 KB Output is correct
40 Correct 5 ms 8404 KB Output is correct
41 Correct 6 ms 8444 KB Output is correct
42 Correct 8 ms 7820 KB Output is correct
43 Correct 8 ms 8148 KB Output is correct
44 Correct 9 ms 8072 KB Output is correct
45 Correct 8 ms 7996 KB Output is correct
46 Correct 6 ms 7892 KB Output is correct
47 Correct 6 ms 8000 KB Output is correct
48 Correct 6 ms 7900 KB Output is correct
49 Correct 7 ms 7892 KB Output is correct
50 Correct 6 ms 7764 KB Output is correct
51 Correct 6 ms 7764 KB Output is correct
52 Correct 6 ms 7848 KB Output is correct
53 Correct 7 ms 7920 KB Output is correct
54 Correct 8 ms 7892 KB Output is correct
55 Correct 11 ms 8660 KB Output is correct
56 Correct 41 ms 12480 KB Output is correct
57 Correct 58 ms 16224 KB Output is correct
58 Correct 64 ms 18692 KB Output is correct
59 Correct 95 ms 22408 KB Output is correct
60 Correct 135 ms 26372 KB Output is correct
61 Correct 178 ms 28676 KB Output is correct
62 Correct 147 ms 31032 KB Output is correct
63 Correct 216 ms 35728 KB Output is correct
64 Correct 245 ms 37104 KB Output is correct
65 Correct 115 ms 71872 KB Output is correct
66 Correct 108 ms 71848 KB Output is correct
67 Correct 121 ms 34364 KB Output is correct
68 Correct 153 ms 54248 KB Output is correct
69 Correct 177 ms 55972 KB Output is correct
70 Correct 218 ms 55876 KB Output is correct
71 Correct 170 ms 51608 KB Output is correct
72 Correct 171 ms 51584 KB Output is correct
73 Correct 158 ms 48192 KB Output is correct
74 Correct 148 ms 48236 KB Output is correct
75 Correct 159 ms 47016 KB Output is correct
76 Correct 184 ms 47176 KB Output is correct
77 Correct 157 ms 45752 KB Output is correct
78 Correct 156 ms 44548 KB Output is correct
79 Correct 161 ms 44224 KB Output is correct