Submission #708261

# Submission time Handle Problem Language Result Execution time Memory
708261 2023-03-11T13:06:18 Z four_specks Fireworks (APIO16_fireworks) C++17
100 / 100
217 ms 89724 KB
#include <bits/stdc++.h>

using namespace std;

namespace
{
    template <typename Fun>
    struct YCombinator
    {
        template <typename T>
        YCombinator(T &&_fun) : fun(forward<T>(_fun)) {}

        template <typename... Args>
        decltype(auto) operator()(Args &&...args) { return fun(ref(*this), forward<Args>(args)...); }

    private:
        Fun fun;
    };

    template <typename T>
    YCombinator(T &&) -> YCombinator<decay_t<T>>;

} // namespace

void solve()
{
    int n, m;
    cin >> n >> m;

    int k = n + m;

    vector<long> a(k);
    vector<vector<int>> adj(k);
    for (int u = 1; u < k; u++)
    {
        int p;
        cin >> p >> a[u], --p;

        adj[p].push_back(u);
    }

    long res = get<0>(YCombinator(
        [&](auto self, int u) -> tuple<long, priority_queue<long>, priority_queue<long, vector<long>, greater<long>>>
        {
            tuple<long, priority_queue<long>, priority_queue<long, vector<long>, greater<long>>> cost;
            auto &[val, lft, rgt] = cost;

            if (adj[u].empty())
            {
                lft.push(0);
                rgt.push(0);
            }

            for (int v : adj[u])
            {
                tuple cost_v = self(v);
                auto &[val_v, lft_v, rgt_v] = cost_v;

                if ((int)lft.size() + (int)rgt.size() < (int)lft_v.size() + (int)rgt_v.size())
                    swap(cost, cost_v);

                val += val_v;

                while (!lft_v.empty())
                {
                    long x = lft_v.top();
                    lft_v.pop();

                    rgt.push(x);
                    val += x - rgt.top();
                    lft.push(rgt.top());
                    rgt.pop();
                }

                while (!rgt_v.empty())
                {
                    long x = rgt_v.top();
                    rgt_v.pop();

                    lft.push(x);
                    val += lft.top() - x;
                    rgt.push(lft.top());
                    lft.pop();
                }
            }

            {
                long x = lft.top() + a[u];
                lft.pop();
                lft.push(x);
            }

            {
                long x = rgt.top() + a[u];
                while (!rgt.empty())
                    rgt.pop();
                rgt.push(x);
            }

            return cost;
        })(0));

    cout << res << '\n';
}

int main()
{
    ios_base::sync_with_stdio(false), cin.tie(NULL);

    solve();

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 216 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 320 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 316 KB Output is correct
15 Correct 1 ms 320 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 316 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 216 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 320 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 1 ms 316 KB Output is correct
25 Correct 1 ms 320 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 316 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 320 KB Output is correct
31 Correct 1 ms 328 KB Output is correct
32 Correct 1 ms 464 KB Output is correct
33 Correct 1 ms 468 KB Output is correct
34 Correct 2 ms 468 KB Output is correct
35 Correct 3 ms 596 KB Output is correct
36 Correct 2 ms 584 KB Output is correct
37 Correct 3 ms 596 KB Output is correct
38 Correct 4 ms 724 KB Output is correct
39 Correct 3 ms 596 KB Output is correct
40 Correct 3 ms 1720 KB Output is correct
41 Correct 3 ms 1748 KB Output is correct
42 Correct 2 ms 716 KB Output is correct
43 Correct 3 ms 1236 KB Output is correct
44 Correct 4 ms 980 KB Output is correct
45 Correct 3 ms 976 KB Output is correct
46 Correct 4 ms 596 KB Output is correct
47 Correct 3 ms 584 KB Output is correct
48 Correct 3 ms 644 KB Output is correct
49 Correct 4 ms 712 KB Output is correct
50 Correct 3 ms 596 KB Output is correct
51 Correct 3 ms 636 KB Output is correct
52 Correct 4 ms 584 KB Output is correct
53 Correct 3 ms 716 KB Output is correct
54 Correct 4 ms 724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 0 ms 216 KB Output is correct
8 Correct 0 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 1 ms 212 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 320 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 1 ms 316 KB Output is correct
25 Correct 1 ms 320 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 316 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 320 KB Output is correct
31 Correct 1 ms 328 KB Output is correct
32 Correct 1 ms 464 KB Output is correct
33 Correct 1 ms 468 KB Output is correct
34 Correct 2 ms 468 KB Output is correct
35 Correct 3 ms 596 KB Output is correct
36 Correct 2 ms 584 KB Output is correct
37 Correct 3 ms 596 KB Output is correct
38 Correct 4 ms 724 KB Output is correct
39 Correct 3 ms 596 KB Output is correct
40 Correct 3 ms 1720 KB Output is correct
41 Correct 3 ms 1748 KB Output is correct
42 Correct 2 ms 716 KB Output is correct
43 Correct 3 ms 1236 KB Output is correct
44 Correct 4 ms 980 KB Output is correct
45 Correct 3 ms 976 KB Output is correct
46 Correct 4 ms 596 KB Output is correct
47 Correct 3 ms 584 KB Output is correct
48 Correct 3 ms 644 KB Output is correct
49 Correct 4 ms 712 KB Output is correct
50 Correct 3 ms 596 KB Output is correct
51 Correct 3 ms 636 KB Output is correct
52 Correct 4 ms 584 KB Output is correct
53 Correct 3 ms 716 KB Output is correct
54 Correct 4 ms 724 KB Output is correct
55 Correct 8 ms 1364 KB Output is correct
56 Correct 24 ms 4044 KB Output is correct
57 Correct 54 ms 6460 KB Output is correct
58 Correct 59 ms 8356 KB Output is correct
59 Correct 88 ms 10908 KB Output is correct
60 Correct 120 ms 13256 KB Output is correct
61 Correct 138 ms 15508 KB Output is correct
62 Correct 159 ms 16864 KB Output is correct
63 Correct 187 ms 19680 KB Output is correct
64 Correct 196 ms 20772 KB Output is correct
65 Correct 120 ms 89724 KB Output is correct
66 Correct 113 ms 89624 KB Output is correct
67 Correct 99 ms 24148 KB Output is correct
68 Correct 144 ms 54208 KB Output is correct
69 Correct 168 ms 48536 KB Output is correct
70 Correct 160 ms 48596 KB Output is correct
71 Correct 193 ms 20240 KB Output is correct
72 Correct 203 ms 20252 KB Output is correct
73 Correct 181 ms 19588 KB Output is correct
74 Correct 168 ms 19540 KB Output is correct
75 Correct 193 ms 19292 KB Output is correct
76 Correct 182 ms 19444 KB Output is correct
77 Correct 191 ms 19856 KB Output is correct
78 Correct 217 ms 20576 KB Output is correct
79 Correct 186 ms 22500 KB Output is correct