Submission #470552

# Submission time Handle Problem Language Result Execution time Memory
470552 2021-09-04T08:45:15 Z BThero Fireworks (APIO16_fireworks) C++17
100 / 100
570 ms 58432 KB
#include <bits/stdc++.h>

#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()

using namespace std;

typedef long long ll;

const int MAXN = (int)3e5 + 5;

struct dp_t {
    ll h0, a0;
    multiset<ll> S;

    dp_t() {
        h0 = a0 = 0;
    }

    ll get(ll x) {
        ll val = h0;
        ll slope = a0;
        ll pos = 0;

        for (ll pt : S) {
            if (pt >= x) {
                break;
            }

            val += slope * (pt - pos);
            pos = pt;
            slope++;
        }

        val += (x - pos) * slope;
        return val;
    }
} dp[MAXN];

int par[MAXN], edge_len[MAXN];
vector<int> adj[MAXN];
int n, m;

int main() {
    cin >> n >> m;

    for (int i = 2; i <= n + m; i++) {
        cin >> par[i] >> edge_len[i];
        adj[par[i]].push_back(i);
    }

    for (int v = n + m; v > n; v--) {
        dp[v].h0 = edge_len[v];
        dp[v].a0 = -1;
        dp[v].S.insert(edge_len[v]);
        dp[v].S.insert(edge_len[v]);
    }

    for (int v = n; v > 0; v--) {
        for (int to : adj[v]) {
            dp[v].h0 += dp[to].h0;
            dp[v].a0 += dp[to].a0;

            if (sz(dp[to].S) > sz(dp[v].S)) {
                dp[v].S.swap(dp[to].S);
            }

            for (ll pt : dp[to].S) {
                dp[v].S.insert(pt);
            }

            dp[to].S.clear();
        }

        while (!dp[v].S.empty() && dp[v].a0 + sz(dp[v].S) > 1) {
            dp[v].S.erase(prev(dp[v].S.end()));
        }

        assert(sz(dp[v].S) >= 2);
        dp[v].h0 += edge_len[v];
        ll P = *dp[v].S.rbegin();
        dp[v].S.erase(prev(dp[v].S.end()));
        ll Q = *dp[v].S.rbegin();
        dp[v].S.erase(prev(dp[v].S.end()));
        dp[v].S.insert(P + edge_len[v]);
        dp[v].S.insert(Q + edge_len[v]);
    }

    ll val = dp[1].h0, slope = dp[1].a0;
    ll pos = 0;

    for (ll pt : dp[1].S) {
        val += (pt - pos) * slope;
        pos = pt;
        slope++;
        if (slope == 0) break;
    }

    cout << val << endl;
	return 0;
}

# Verdict Execution time Memory Grader output
1 Correct 18 ms 26060 KB Output is correct
2 Correct 14 ms 26076 KB Output is correct
3 Correct 14 ms 26060 KB Output is correct
4 Correct 14 ms 26108 KB Output is correct
5 Correct 14 ms 26060 KB Output is correct
6 Correct 15 ms 26060 KB Output is correct
7 Correct 15 ms 26152 KB Output is correct
8 Correct 15 ms 26060 KB Output is correct
9 Correct 15 ms 26060 KB Output is correct
10 Correct 14 ms 26024 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 26080 KB Output is correct
2 Correct 16 ms 26112 KB Output is correct
3 Correct 17 ms 26060 KB Output is correct
4 Correct 16 ms 26096 KB Output is correct
5 Correct 15 ms 26060 KB Output is correct
6 Correct 17 ms 26104 KB Output is correct
7 Correct 16 ms 26068 KB Output is correct
8 Correct 14 ms 26096 KB Output is correct
9 Correct 14 ms 26072 KB Output is correct
10 Correct 17 ms 26064 KB Output is correct
11 Correct 15 ms 26060 KB Output is correct
12 Correct 16 ms 26044 KB Output is correct
13 Correct 15 ms 26060 KB Output is correct
14 Correct 15 ms 26060 KB Output is correct
15 Correct 14 ms 26120 KB Output is correct
16 Correct 15 ms 26048 KB Output is correct
17 Correct 15 ms 26104 KB Output is correct
18 Correct 14 ms 26060 KB Output is correct
19 Correct 15 ms 26140 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 26060 KB Output is correct
2 Correct 14 ms 26076 KB Output is correct
3 Correct 14 ms 26060 KB Output is correct
4 Correct 14 ms 26108 KB Output is correct
5 Correct 14 ms 26060 KB Output is correct
6 Correct 15 ms 26060 KB Output is correct
7 Correct 15 ms 26152 KB Output is correct
8 Correct 15 ms 26060 KB Output is correct
9 Correct 15 ms 26060 KB Output is correct
10 Correct 14 ms 26024 KB Output is correct
11 Correct 14 ms 26080 KB Output is correct
12 Correct 16 ms 26112 KB Output is correct
13 Correct 17 ms 26060 KB Output is correct
14 Correct 16 ms 26096 KB Output is correct
15 Correct 15 ms 26060 KB Output is correct
16 Correct 17 ms 26104 KB Output is correct
17 Correct 16 ms 26068 KB Output is correct
18 Correct 14 ms 26096 KB Output is correct
19 Correct 14 ms 26072 KB Output is correct
20 Correct 17 ms 26064 KB Output is correct
21 Correct 15 ms 26060 KB Output is correct
22 Correct 16 ms 26044 KB Output is correct
23 Correct 15 ms 26060 KB Output is correct
24 Correct 15 ms 26060 KB Output is correct
25 Correct 14 ms 26120 KB Output is correct
26 Correct 15 ms 26048 KB Output is correct
27 Correct 15 ms 26104 KB Output is correct
28 Correct 14 ms 26060 KB Output is correct
29 Correct 15 ms 26140 KB Output is correct
30 Correct 14 ms 26060 KB Output is correct
31 Correct 15 ms 26188 KB Output is correct
32 Correct 17 ms 26152 KB Output is correct
33 Correct 18 ms 26180 KB Output is correct
34 Correct 18 ms 26316 KB Output is correct
35 Correct 20 ms 26340 KB Output is correct
36 Correct 19 ms 26348 KB Output is correct
37 Correct 24 ms 26324 KB Output is correct
38 Correct 21 ms 26444 KB Output is correct
39 Correct 20 ms 26344 KB Output is correct
40 Correct 19 ms 26276 KB Output is correct
41 Correct 18 ms 26316 KB Output is correct
42 Correct 20 ms 26316 KB Output is correct
43 Correct 20 ms 26428 KB Output is correct
44 Correct 20 ms 26444 KB Output is correct
45 Correct 20 ms 26444 KB Output is correct
46 Correct 23 ms 26572 KB Output is correct
47 Correct 20 ms 26676 KB Output is correct
48 Correct 21 ms 26608 KB Output is correct
49 Correct 22 ms 26580 KB Output is correct
50 Correct 20 ms 26588 KB Output is correct
51 Correct 20 ms 26476 KB Output is correct
52 Correct 20 ms 26552 KB Output is correct
53 Correct 20 ms 26572 KB Output is correct
54 Correct 22 ms 26636 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 26060 KB Output is correct
2 Correct 14 ms 26076 KB Output is correct
3 Correct 14 ms 26060 KB Output is correct
4 Correct 14 ms 26108 KB Output is correct
5 Correct 14 ms 26060 KB Output is correct
6 Correct 15 ms 26060 KB Output is correct
7 Correct 15 ms 26152 KB Output is correct
8 Correct 15 ms 26060 KB Output is correct
9 Correct 15 ms 26060 KB Output is correct
10 Correct 14 ms 26024 KB Output is correct
11 Correct 14 ms 26080 KB Output is correct
12 Correct 16 ms 26112 KB Output is correct
13 Correct 17 ms 26060 KB Output is correct
14 Correct 16 ms 26096 KB Output is correct
15 Correct 15 ms 26060 KB Output is correct
16 Correct 17 ms 26104 KB Output is correct
17 Correct 16 ms 26068 KB Output is correct
18 Correct 14 ms 26096 KB Output is correct
19 Correct 14 ms 26072 KB Output is correct
20 Correct 17 ms 26064 KB Output is correct
21 Correct 15 ms 26060 KB Output is correct
22 Correct 16 ms 26044 KB Output is correct
23 Correct 15 ms 26060 KB Output is correct
24 Correct 15 ms 26060 KB Output is correct
25 Correct 14 ms 26120 KB Output is correct
26 Correct 15 ms 26048 KB Output is correct
27 Correct 15 ms 26104 KB Output is correct
28 Correct 14 ms 26060 KB Output is correct
29 Correct 15 ms 26140 KB Output is correct
30 Correct 14 ms 26060 KB Output is correct
31 Correct 15 ms 26188 KB Output is correct
32 Correct 17 ms 26152 KB Output is correct
33 Correct 18 ms 26180 KB Output is correct
34 Correct 18 ms 26316 KB Output is correct
35 Correct 20 ms 26340 KB Output is correct
36 Correct 19 ms 26348 KB Output is correct
37 Correct 24 ms 26324 KB Output is correct
38 Correct 21 ms 26444 KB Output is correct
39 Correct 20 ms 26344 KB Output is correct
40 Correct 19 ms 26276 KB Output is correct
41 Correct 18 ms 26316 KB Output is correct
42 Correct 20 ms 26316 KB Output is correct
43 Correct 20 ms 26428 KB Output is correct
44 Correct 20 ms 26444 KB Output is correct
45 Correct 20 ms 26444 KB Output is correct
46 Correct 23 ms 26572 KB Output is correct
47 Correct 20 ms 26676 KB Output is correct
48 Correct 21 ms 26608 KB Output is correct
49 Correct 22 ms 26580 KB Output is correct
50 Correct 20 ms 26588 KB Output is correct
51 Correct 20 ms 26476 KB Output is correct
52 Correct 20 ms 26552 KB Output is correct
53 Correct 20 ms 26572 KB Output is correct
54 Correct 22 ms 26636 KB Output is correct
55 Correct 29 ms 26952 KB Output is correct
56 Correct 78 ms 29180 KB Output is correct
57 Correct 124 ms 31328 KB Output is correct
58 Correct 164 ms 32772 KB Output is correct
59 Correct 223 ms 35296 KB Output is correct
60 Correct 301 ms 37440 KB Output is correct
61 Correct 340 ms 38980 KB Output is correct
62 Correct 383 ms 40248 KB Output is correct
63 Correct 467 ms 42960 KB Output is correct
64 Correct 517 ms 43856 KB Output is correct
65 Correct 294 ms 38084 KB Output is correct
66 Correct 348 ms 38000 KB Output is correct
67 Correct 297 ms 38204 KB Output is correct
68 Correct 388 ms 47392 KB Output is correct
69 Correct 465 ms 47780 KB Output is correct
70 Correct 434 ms 47688 KB Output is correct
71 Correct 516 ms 58328 KB Output is correct
72 Correct 528 ms 58308 KB Output is correct
73 Correct 496 ms 58316 KB Output is correct
74 Correct 551 ms 58432 KB Output is correct
75 Correct 487 ms 58180 KB Output is correct
76 Correct 463 ms 58308 KB Output is correct
77 Correct 503 ms 57284 KB Output is correct
78 Correct 494 ms 57336 KB Output is correct
79 Correct 570 ms 58176 KB Output is correct