Submission #395101

# Submission time Handle Problem Language Result Execution time Memory
395101 2021-04-27T19:00:32 Z osaaateiasavtnl Fireworks (APIO16_fireworks) C++14
100 / 100
232 ms 41448 KB
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <bitset>
#include <fstream>
using namespace std;
#define int long long
#define ii pair <int, int>
#define app push_back
#define all(a) a.begin(), a.end()
#define bp __builtin_popcountll
#define ll long long
#define mp make_pair
#define x first
#define y second
#define Time (double)clock()/CLOCKS_PER_SEC
#define debug(x) std::cerr << #x << ": " << x << '\n';
#define FOR(i, n) for (int i = 0; i < n; ++i)
#define FORN(i, n) for (int i = 1; i <= n; ++i)
#define pb push_back
#define trav(a, x) for (auto& a : x)
using vi = vector<int>;
template <typename T>
std::istream& operator >>(std::istream& input, std::vector<T>& data)
{
    for (T& x : data)
        input >> x;
    return input;
}
template <typename T>
std::ostream& operator <<(std::ostream& output, const pair <T, T> & data)
{
    output << "(" << data.x << "," << data.y << ")";
    return output;
}
template <typename T>
std::ostream& operator <<(std::ostream& output, const std::vector<T>& data)
{
    for (const T& x : data)
        output << x << " ";
    return output;
}
ll div_up(ll a, ll b) { return a/b+((a^b)>0&&a%b); } // divide a by b rounded up
ll div_down(ll a, ll b) { return a/b-((a^b)<0&&a%b); } // divide a by b rounded down 
ll math_mod(ll a, ll b) { return a - b * div_down(a, b); }
#define tcT template<class T
#define tcTU tcT, class U
tcT> using V = vector<T>; 
tcT> void re(V<T>& x) { 
    trav(a, x)
        cin >> a;
}
tcT> bool ckmin(T& a, const T& b) {
    return b < a ? a = b, 1 : 0; 
} // set a = min(a,b)
tcT> bool ckmax(T& a, const T& b) {
    return a < b ? a = b, 1 : 0; 
}
ll gcd(ll a, ll b) {
    while (b) {
        tie(a, b) = mp(b, a % b);
    }
    return a;
}
const int N = 3e5+7;
int n, m;
int par[N], cost[N];
priority_queue <int> dp[N];
int children[N];
signed main() {
    #ifdef LOCAL
    #else
    #define endl '\n'
    ios_base::sync_with_stdio(0); cin.tie(0);
    #endif
    cin >> n >> m;
    int ans = 0;
    for (int i = 2; i <= n + m; ++i) {
        cin >> par[i] >> cost[i];
        children[par[i]]++;
        ans += cost[i];
    }
    for (int u = n + m; u >= 2; --u) {
        if (children[u]) {
            FOR (i, children[u] - 1) {
                dp[u].pop();
            }
            int r = dp[u].top(); dp[u].pop();
            int l = dp[u].top(); dp[u].pop();
            dp[u].push(l + cost[u]);
            dp[u].push(r + cost[u]);
        }
        else {
            dp[u].push(cost[u]);
            dp[u].push(cost[u]);
        }
        if (dp[u].size() > dp[par[u]].size()) {
            swap(dp[u], dp[par[u]]);
        }
        while (dp[u].size()) {
            dp[par[u]].push(dp[u].top());
            dp[u].pop();
        }
    }
    FOR (i, children[1]) {
        dp[1].pop();
    }
    int l = dp[1].top();
    while (dp[1].size()) {
        ans -= dp[1].top();
        dp[1].pop();
    }
    cout << ans << endl;
}

Compilation message

fireworks.cpp: In function 'int main()':
fireworks.cpp:123:9: warning: unused variable 'l' [-Wunused-variable]
  123 |     int l = dp[1].top();
      |         ^
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9676 KB Output is correct
2 Correct 6 ms 9676 KB Output is correct
3 Correct 5 ms 9676 KB Output is correct
4 Correct 5 ms 9728 KB Output is correct
5 Correct 6 ms 9676 KB Output is correct
6 Correct 6 ms 9720 KB Output is correct
7 Correct 6 ms 9676 KB Output is correct
8 Correct 6 ms 9728 KB Output is correct
9 Correct 6 ms 9692 KB Output is correct
10 Correct 6 ms 9676 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9720 KB Output is correct
2 Correct 7 ms 9724 KB Output is correct
3 Correct 5 ms 9676 KB Output is correct
4 Correct 6 ms 9724 KB Output is correct
5 Correct 7 ms 9676 KB Output is correct
6 Correct 7 ms 9720 KB Output is correct
7 Correct 6 ms 9720 KB Output is correct
8 Correct 6 ms 9744 KB Output is correct
9 Correct 6 ms 9724 KB Output is correct
10 Correct 6 ms 9724 KB Output is correct
11 Correct 6 ms 9724 KB Output is correct
12 Correct 6 ms 9676 KB Output is correct
13 Correct 6 ms 9676 KB Output is correct
14 Correct 6 ms 9720 KB Output is correct
15 Correct 7 ms 9720 KB Output is correct
16 Correct 6 ms 9676 KB Output is correct
17 Correct 6 ms 9752 KB Output is correct
18 Correct 7 ms 9676 KB Output is correct
19 Correct 6 ms 9676 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9676 KB Output is correct
2 Correct 6 ms 9676 KB Output is correct
3 Correct 5 ms 9676 KB Output is correct
4 Correct 5 ms 9728 KB Output is correct
5 Correct 6 ms 9676 KB Output is correct
6 Correct 6 ms 9720 KB Output is correct
7 Correct 6 ms 9676 KB Output is correct
8 Correct 6 ms 9728 KB Output is correct
9 Correct 6 ms 9692 KB Output is correct
10 Correct 6 ms 9676 KB Output is correct
11 Correct 6 ms 9720 KB Output is correct
12 Correct 7 ms 9724 KB Output is correct
13 Correct 5 ms 9676 KB Output is correct
14 Correct 6 ms 9724 KB Output is correct
15 Correct 7 ms 9676 KB Output is correct
16 Correct 7 ms 9720 KB Output is correct
17 Correct 6 ms 9720 KB Output is correct
18 Correct 6 ms 9744 KB Output is correct
19 Correct 6 ms 9724 KB Output is correct
20 Correct 6 ms 9724 KB Output is correct
21 Correct 6 ms 9724 KB Output is correct
22 Correct 6 ms 9676 KB Output is correct
23 Correct 6 ms 9676 KB Output is correct
24 Correct 6 ms 9720 KB Output is correct
25 Correct 7 ms 9720 KB Output is correct
26 Correct 6 ms 9676 KB Output is correct
27 Correct 6 ms 9752 KB Output is correct
28 Correct 7 ms 9676 KB Output is correct
29 Correct 6 ms 9676 KB Output is correct
30 Correct 6 ms 9676 KB Output is correct
31 Correct 6 ms 9804 KB Output is correct
32 Correct 8 ms 9804 KB Output is correct
33 Correct 7 ms 9772 KB Output is correct
34 Correct 7 ms 9932 KB Output is correct
35 Correct 8 ms 10060 KB Output is correct
36 Correct 8 ms 9932 KB Output is correct
37 Correct 8 ms 10060 KB Output is correct
38 Correct 9 ms 10060 KB Output is correct
39 Correct 9 ms 10044 KB Output is correct
40 Correct 7 ms 9836 KB Output is correct
41 Correct 7 ms 9804 KB Output is correct
42 Correct 7 ms 9804 KB Output is correct
43 Correct 8 ms 9932 KB Output is correct
44 Correct 8 ms 10060 KB Output is correct
45 Correct 8 ms 10060 KB Output is correct
46 Correct 9 ms 10188 KB Output is correct
47 Correct 9 ms 10188 KB Output is correct
48 Correct 8 ms 10060 KB Output is correct
49 Correct 9 ms 10188 KB Output is correct
50 Correct 8 ms 10076 KB Output is correct
51 Correct 8 ms 9992 KB Output is correct
52 Correct 9 ms 10128 KB Output is correct
53 Correct 9 ms 10060 KB Output is correct
54 Correct 8 ms 10060 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9676 KB Output is correct
2 Correct 6 ms 9676 KB Output is correct
3 Correct 5 ms 9676 KB Output is correct
4 Correct 5 ms 9728 KB Output is correct
5 Correct 6 ms 9676 KB Output is correct
6 Correct 6 ms 9720 KB Output is correct
7 Correct 6 ms 9676 KB Output is correct
8 Correct 6 ms 9728 KB Output is correct
9 Correct 6 ms 9692 KB Output is correct
10 Correct 6 ms 9676 KB Output is correct
11 Correct 6 ms 9720 KB Output is correct
12 Correct 7 ms 9724 KB Output is correct
13 Correct 5 ms 9676 KB Output is correct
14 Correct 6 ms 9724 KB Output is correct
15 Correct 7 ms 9676 KB Output is correct
16 Correct 7 ms 9720 KB Output is correct
17 Correct 6 ms 9720 KB Output is correct
18 Correct 6 ms 9744 KB Output is correct
19 Correct 6 ms 9724 KB Output is correct
20 Correct 6 ms 9724 KB Output is correct
21 Correct 6 ms 9724 KB Output is correct
22 Correct 6 ms 9676 KB Output is correct
23 Correct 6 ms 9676 KB Output is correct
24 Correct 6 ms 9720 KB Output is correct
25 Correct 7 ms 9720 KB Output is correct
26 Correct 6 ms 9676 KB Output is correct
27 Correct 6 ms 9752 KB Output is correct
28 Correct 7 ms 9676 KB Output is correct
29 Correct 6 ms 9676 KB Output is correct
30 Correct 6 ms 9676 KB Output is correct
31 Correct 6 ms 9804 KB Output is correct
32 Correct 8 ms 9804 KB Output is correct
33 Correct 7 ms 9772 KB Output is correct
34 Correct 7 ms 9932 KB Output is correct
35 Correct 8 ms 10060 KB Output is correct
36 Correct 8 ms 9932 KB Output is correct
37 Correct 8 ms 10060 KB Output is correct
38 Correct 9 ms 10060 KB Output is correct
39 Correct 9 ms 10044 KB Output is correct
40 Correct 7 ms 9836 KB Output is correct
41 Correct 7 ms 9804 KB Output is correct
42 Correct 7 ms 9804 KB Output is correct
43 Correct 8 ms 9932 KB Output is correct
44 Correct 8 ms 10060 KB Output is correct
45 Correct 8 ms 10060 KB Output is correct
46 Correct 9 ms 10188 KB Output is correct
47 Correct 9 ms 10188 KB Output is correct
48 Correct 8 ms 10060 KB Output is correct
49 Correct 9 ms 10188 KB Output is correct
50 Correct 8 ms 10076 KB Output is correct
51 Correct 8 ms 9992 KB Output is correct
52 Correct 9 ms 10128 KB Output is correct
53 Correct 9 ms 10060 KB Output is correct
54 Correct 8 ms 10060 KB Output is correct
55 Correct 12 ms 10576 KB Output is correct
56 Correct 31 ms 13348 KB Output is correct
57 Correct 50 ms 15876 KB Output is correct
58 Correct 62 ms 17652 KB Output is correct
59 Correct 86 ms 20184 KB Output is correct
60 Correct 109 ms 22612 KB Output is correct
61 Correct 118 ms 24812 KB Output is correct
62 Correct 133 ms 26168 KB Output is correct
63 Correct 166 ms 29232 KB Output is correct
64 Correct 167 ms 30160 KB Output is correct
65 Correct 85 ms 21508 KB Output is correct
66 Correct 83 ms 21672 KB Output is correct
67 Correct 81 ms 21572 KB Output is correct
68 Correct 181 ms 27124 KB Output is correct
69 Correct 165 ms 27808 KB Output is correct
70 Correct 167 ms 27684 KB Output is correct
71 Correct 232 ms 41364 KB Output is correct
72 Correct 227 ms 41448 KB Output is correct
73 Correct 198 ms 37964 KB Output is correct
74 Correct 223 ms 38468 KB Output is correct
75 Correct 203 ms 36924 KB Output is correct
76 Correct 205 ms 36976 KB Output is correct
77 Correct 226 ms 36460 KB Output is correct
78 Correct 220 ms 35944 KB Output is correct
79 Correct 193 ms 34220 KB Output is correct