Submission #249939

# Submission time Handle Problem Language Result Execution time Memory
249939 2020-07-16T14:04:42 Z receed Fireworks (APIO16_fireworks) C++14
100 / 100
581 ms 101240 KB
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <cassert>
#include <string>
#include <set>
#include <map>
#include <random>
#include <bitset>
#include <string>
#include <unordered_set>
#include <unordered_map>
#include <deque>
#include <queue>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

using namespace std;
using ll = long long;
using ul = unsigned long long;
using ld = long double;

const int N = 300001;
ll pw[N], sm[N], sz[N];
multiset<ll> s[N];
vector<int> g[N];

void dfs(int v) {
    sm[v] = pw[v];
    for (int u : g[v]) {
        dfs(u);
        sm[v] += sm[u];
        if (s[v].size() < s[u].size())
            swap(s[u], s[v]);
        for (ll i : s[u])
            s[v].insert(i);
    }
    if (!g[v].empty()) {
        rep(i, g[v].size() - 1)
            s[v].erase(--s[v].end());
        ll v1 = *--s[v].end();
        s[v].erase(--s[v].end());
        ll v2 = *--s[v].end();
        s[v].erase(--s[v].end());
        s[v].insert(v1 + pw[v]);
        s[v].insert(v2 + pw[v]);
    }
    else {
        s[v].insert(pw[v]);
        s[v].insert(pw[v]);
    }
}

int main() {
    ios_base::sync_with_stdio(0); cin.tie(0);
    ll n, m, p, w;
    cin >> n >> m;
    for (int i = 1; i < n + m; i++) {
        cin >> p >> pw[i];
        g[p - 1].push_back(i);
    }
    dfs(0);
    s[0].erase(--s[0].end());
    ll ls = 0, ck = s[0].size(), ans = sm[0];
    for (ll i : s[0]) {
        ans -= (i - ls) * ck;
        ck--;
        ls = i;
    }
    cout << ans;
}

Compilation message

fireworks.cpp: In function 'void dfs(int)':
fireworks.cpp:19:37: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 #define rep(i, n) for (int i = 0; i < (n); i++)
                                     ^
fireworks.cpp:44:9: note: in expansion of macro 'rep'
         rep(i, g[v].size() - 1)
         ^~~
fireworks.cpp: In function 'int main()':
fireworks.cpp:61:17: warning: unused variable 'w' [-Wunused-variable]
     ll n, m, p, w;
                 ^
# Verdict Execution time Memory Grader output
1 Correct 13 ms 21504 KB Output is correct
2 Correct 12 ms 21504 KB Output is correct
3 Correct 11 ms 21504 KB Output is correct
4 Correct 12 ms 21504 KB Output is correct
5 Correct 11 ms 21504 KB Output is correct
6 Correct 12 ms 21504 KB Output is correct
7 Correct 12 ms 21504 KB Output is correct
8 Correct 12 ms 21504 KB Output is correct
9 Correct 13 ms 21504 KB Output is correct
10 Correct 12 ms 21504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 21504 KB Output is correct
2 Correct 12 ms 21504 KB Output is correct
3 Correct 14 ms 21504 KB Output is correct
4 Correct 14 ms 21504 KB Output is correct
5 Correct 12 ms 21504 KB Output is correct
6 Correct 13 ms 21504 KB Output is correct
7 Correct 12 ms 21504 KB Output is correct
8 Correct 13 ms 21504 KB Output is correct
9 Correct 12 ms 21504 KB Output is correct
10 Correct 17 ms 21504 KB Output is correct
11 Correct 12 ms 21504 KB Output is correct
12 Correct 30 ms 21504 KB Output is correct
13 Correct 12 ms 21504 KB Output is correct
14 Correct 31 ms 21504 KB Output is correct
15 Correct 30 ms 21496 KB Output is correct
16 Correct 12 ms 21504 KB Output is correct
17 Correct 17 ms 21504 KB Output is correct
18 Correct 17 ms 21504 KB Output is correct
19 Correct 18 ms 21576 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 21504 KB Output is correct
2 Correct 12 ms 21504 KB Output is correct
3 Correct 11 ms 21504 KB Output is correct
4 Correct 12 ms 21504 KB Output is correct
5 Correct 11 ms 21504 KB Output is correct
6 Correct 12 ms 21504 KB Output is correct
7 Correct 12 ms 21504 KB Output is correct
8 Correct 12 ms 21504 KB Output is correct
9 Correct 13 ms 21504 KB Output is correct
10 Correct 12 ms 21504 KB Output is correct
11 Correct 12 ms 21504 KB Output is correct
12 Correct 12 ms 21504 KB Output is correct
13 Correct 14 ms 21504 KB Output is correct
14 Correct 14 ms 21504 KB Output is correct
15 Correct 12 ms 21504 KB Output is correct
16 Correct 13 ms 21504 KB Output is correct
17 Correct 12 ms 21504 KB Output is correct
18 Correct 13 ms 21504 KB Output is correct
19 Correct 12 ms 21504 KB Output is correct
20 Correct 17 ms 21504 KB Output is correct
21 Correct 12 ms 21504 KB Output is correct
22 Correct 30 ms 21504 KB Output is correct
23 Correct 12 ms 21504 KB Output is correct
24 Correct 31 ms 21504 KB Output is correct
25 Correct 30 ms 21496 KB Output is correct
26 Correct 12 ms 21504 KB Output is correct
27 Correct 17 ms 21504 KB Output is correct
28 Correct 17 ms 21504 KB Output is correct
29 Correct 18 ms 21576 KB Output is correct
30 Correct 12 ms 21504 KB Output is correct
31 Correct 13 ms 21632 KB Output is correct
32 Correct 14 ms 21760 KB Output is correct
33 Correct 14 ms 21760 KB Output is correct
34 Correct 16 ms 21888 KB Output is correct
35 Correct 14 ms 22016 KB Output is correct
36 Correct 15 ms 22016 KB Output is correct
37 Correct 22 ms 22144 KB Output is correct
38 Correct 16 ms 22144 KB Output is correct
39 Correct 39 ms 22264 KB Output is correct
40 Correct 15 ms 22400 KB Output is correct
41 Correct 21 ms 22264 KB Output is correct
42 Correct 14 ms 21760 KB Output is correct
43 Correct 16 ms 22272 KB Output is correct
44 Correct 16 ms 22272 KB Output is correct
45 Correct 16 ms 22272 KB Output is correct
46 Correct 20 ms 22648 KB Output is correct
47 Correct 42 ms 22524 KB Output is correct
48 Correct 17 ms 22400 KB Output is correct
49 Correct 22 ms 22528 KB Output is correct
50 Correct 16 ms 22272 KB Output is correct
51 Correct 16 ms 22272 KB Output is correct
52 Correct 24 ms 22400 KB Output is correct
53 Correct 17 ms 22400 KB Output is correct
54 Correct 16 ms 22528 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 21504 KB Output is correct
2 Correct 12 ms 21504 KB Output is correct
3 Correct 11 ms 21504 KB Output is correct
4 Correct 12 ms 21504 KB Output is correct
5 Correct 11 ms 21504 KB Output is correct
6 Correct 12 ms 21504 KB Output is correct
7 Correct 12 ms 21504 KB Output is correct
8 Correct 12 ms 21504 KB Output is correct
9 Correct 13 ms 21504 KB Output is correct
10 Correct 12 ms 21504 KB Output is correct
11 Correct 12 ms 21504 KB Output is correct
12 Correct 12 ms 21504 KB Output is correct
13 Correct 14 ms 21504 KB Output is correct
14 Correct 14 ms 21504 KB Output is correct
15 Correct 12 ms 21504 KB Output is correct
16 Correct 13 ms 21504 KB Output is correct
17 Correct 12 ms 21504 KB Output is correct
18 Correct 13 ms 21504 KB Output is correct
19 Correct 12 ms 21504 KB Output is correct
20 Correct 17 ms 21504 KB Output is correct
21 Correct 12 ms 21504 KB Output is correct
22 Correct 30 ms 21504 KB Output is correct
23 Correct 12 ms 21504 KB Output is correct
24 Correct 31 ms 21504 KB Output is correct
25 Correct 30 ms 21496 KB Output is correct
26 Correct 12 ms 21504 KB Output is correct
27 Correct 17 ms 21504 KB Output is correct
28 Correct 17 ms 21504 KB Output is correct
29 Correct 18 ms 21576 KB Output is correct
30 Correct 12 ms 21504 KB Output is correct
31 Correct 13 ms 21632 KB Output is correct
32 Correct 14 ms 21760 KB Output is correct
33 Correct 14 ms 21760 KB Output is correct
34 Correct 16 ms 21888 KB Output is correct
35 Correct 14 ms 22016 KB Output is correct
36 Correct 15 ms 22016 KB Output is correct
37 Correct 22 ms 22144 KB Output is correct
38 Correct 16 ms 22144 KB Output is correct
39 Correct 39 ms 22264 KB Output is correct
40 Correct 15 ms 22400 KB Output is correct
41 Correct 21 ms 22264 KB Output is correct
42 Correct 14 ms 21760 KB Output is correct
43 Correct 16 ms 22272 KB Output is correct
44 Correct 16 ms 22272 KB Output is correct
45 Correct 16 ms 22272 KB Output is correct
46 Correct 20 ms 22648 KB Output is correct
47 Correct 42 ms 22524 KB Output is correct
48 Correct 17 ms 22400 KB Output is correct
49 Correct 22 ms 22528 KB Output is correct
50 Correct 16 ms 22272 KB Output is correct
51 Correct 16 ms 22272 KB Output is correct
52 Correct 24 ms 22400 KB Output is correct
53 Correct 17 ms 22400 KB Output is correct
54 Correct 16 ms 22528 KB Output is correct
55 Correct 22 ms 23296 KB Output is correct
56 Correct 57 ms 28664 KB Output is correct
57 Correct 99 ms 34168 KB Output is correct
58 Correct 148 ms 37880 KB Output is correct
59 Correct 180 ms 43256 KB Output is correct
60 Correct 216 ms 48944 KB Output is correct
61 Correct 247 ms 52728 KB Output is correct
62 Correct 281 ms 56568 KB Output is correct
63 Correct 332 ms 64120 KB Output is correct
64 Correct 358 ms 64888 KB Output is correct
65 Correct 164 ms 73336 KB Output is correct
66 Correct 154 ms 73336 KB Output is correct
67 Correct 162 ms 40696 KB Output is correct
68 Correct 269 ms 73208 KB Output is correct
69 Correct 331 ms 70776 KB Output is correct
70 Correct 360 ms 70828 KB Output is correct
71 Correct 426 ms 101240 KB Output is correct
72 Correct 536 ms 100728 KB Output is correct
73 Correct 581 ms 88056 KB Output is correct
74 Correct 445 ms 87928 KB Output is correct
75 Correct 456 ms 87340 KB Output is correct
76 Correct 459 ms 87672 KB Output is correct
77 Correct 455 ms 85496 KB Output is correct
78 Correct 501 ms 82676 KB Output is correct
79 Correct 514 ms 87148 KB Output is correct