Submission #471806

# Submission time Handle Problem Language Result Execution time Memory
471806 2021-09-10T22:27:05 Z Windazz Fireworks (APIO16_fireworks) C++17
100 / 100
271 ms 70084 KB
#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>

#define x first
#define y second
#define ndl '\n'
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define up_b upper_bound
#define low_b lower_bound
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()

using namespace std;
//using namespace __gnu_pbds;

//template<typename T> using indexed_set = tree <T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());//uniform_int_distribution<int> (l, r)

typedef long long ll;
typedef long double ld;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef pair<int, ll> pil;
typedef pair<ll, int> pli;
typedef pair<int, ull> piu;
typedef vector<vector<int>> matrix;

const ll INF = 1e18 + 123;
const ld EPS = 1e-9;
const int inf = 1e9 + 123;
const int MOD = 1e9 + 7;
const int N = 3e5 + 123;
const int M = 1e6 + 123;
const double pi = 3.14159265359;
const int dx[] = {0, 0, 1, -1};
const int dy[] = {1, -1, 0, 0};

struct node{
    ll a = 0, b = 0;
    priority_queue<ll> q;
}dp[N];

vector<int> g[N];

int c[N];

void dfs(int v){
    if (g[v].empty()){
        dp[v].a = -1;
        dp[v].b = c[v];
        dp[v].q.push(c[v]);
        dp[v].q.push(c[v]);
        return;
    }
    for (int to : g[v]){
        dfs(to);
        dp[v].a += dp[to].a;
        dp[v].b += dp[to].b;
        if (sz(dp[v].q) < sz(dp[to].q)){
            swap(dp[v].q, dp[to].q);
        }
        while (!dp[to].q.empty()){
            dp[v].q.push(dp[to].q.top());
            dp[to].q.pop();
        }
    }
    while (dp[v].a + sz(dp[v].q) > 1){
        dp[v].q.pop();
    }
    ll x1 = dp[v].q.top();
    dp[v].q.pop();
    ll x2 = dp[v].q.top();
    dp[v].q.pop();

    dp[v].b += c[v];
    dp[v].q.push(x1+c[v]);
    dp[v].q.push(x2+c[v]);

}
    
void solve(){
    int n, m;
    cin >> n >> m;
    for (int i = 2, p; i <= n+m; i++){
        cin >> p >> c[i];
        g[p].pb(i);
    }
    dfs(1);

    vector<ll> v;

    while (!dp[1].q.empty()) {
        v.push_back(dp[1].q.top());
        dp[1].q.pop();
    }
    v.pb(0);

    for (int i = sz(v)-2; dp[1].a < 0; i--, dp[1].a++){
        dp[1].b += (v[i] - v[i+1]) * dp[1].a;
    }
    cout << dp[1].b;


}

int main(){
    #ifdef me
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int t = 1;  
    //cin >> t;
    for (int i = 1; i <= t; i++){
        //cout << "Case #" << i << ": ";
        solve();
    }
    #ifdef KAZAKH
      //  cout << endl << 1.0 * clock() / CLOCKS_PER_SEC << endl;
    #endif
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 13 ms 21452 KB Output is correct
2 Correct 13 ms 21352 KB Output is correct
3 Correct 13 ms 21452 KB Output is correct
4 Correct 13 ms 21452 KB Output is correct
5 Correct 13 ms 21488 KB Output is correct
6 Correct 13 ms 21468 KB Output is correct
7 Correct 13 ms 21456 KB Output is correct
8 Correct 13 ms 21404 KB Output is correct
9 Correct 14 ms 21408 KB Output is correct
10 Correct 13 ms 21456 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 21452 KB Output is correct
2 Correct 12 ms 21464 KB Output is correct
3 Correct 12 ms 21444 KB Output is correct
4 Correct 13 ms 21408 KB Output is correct
5 Correct 13 ms 21384 KB Output is correct
6 Correct 13 ms 21452 KB Output is correct
7 Correct 13 ms 21424 KB Output is correct
8 Correct 13 ms 21452 KB Output is correct
9 Correct 13 ms 21380 KB Output is correct
10 Correct 13 ms 21468 KB Output is correct
11 Correct 13 ms 21468 KB Output is correct
12 Correct 13 ms 21468 KB Output is correct
13 Correct 13 ms 21456 KB Output is correct
14 Correct 13 ms 21456 KB Output is correct
15 Correct 15 ms 21464 KB Output is correct
16 Correct 14 ms 21448 KB Output is correct
17 Correct 13 ms 21452 KB Output is correct
18 Correct 13 ms 21380 KB Output is correct
19 Correct 13 ms 21372 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 21452 KB Output is correct
2 Correct 13 ms 21352 KB Output is correct
3 Correct 13 ms 21452 KB Output is correct
4 Correct 13 ms 21452 KB Output is correct
5 Correct 13 ms 21488 KB Output is correct
6 Correct 13 ms 21468 KB Output is correct
7 Correct 13 ms 21456 KB Output is correct
8 Correct 13 ms 21404 KB Output is correct
9 Correct 14 ms 21408 KB Output is correct
10 Correct 13 ms 21456 KB Output is correct
11 Correct 12 ms 21452 KB Output is correct
12 Correct 12 ms 21464 KB Output is correct
13 Correct 12 ms 21444 KB Output is correct
14 Correct 13 ms 21408 KB Output is correct
15 Correct 13 ms 21384 KB Output is correct
16 Correct 13 ms 21452 KB Output is correct
17 Correct 13 ms 21424 KB Output is correct
18 Correct 13 ms 21452 KB Output is correct
19 Correct 13 ms 21380 KB Output is correct
20 Correct 13 ms 21468 KB Output is correct
21 Correct 13 ms 21468 KB Output is correct
22 Correct 13 ms 21468 KB Output is correct
23 Correct 13 ms 21456 KB Output is correct
24 Correct 13 ms 21456 KB Output is correct
25 Correct 15 ms 21464 KB Output is correct
26 Correct 14 ms 21448 KB Output is correct
27 Correct 13 ms 21452 KB Output is correct
28 Correct 13 ms 21380 KB Output is correct
29 Correct 13 ms 21372 KB Output is correct
30 Correct 12 ms 21456 KB Output is correct
31 Correct 13 ms 21452 KB Output is correct
32 Correct 14 ms 21580 KB Output is correct
33 Correct 14 ms 21580 KB Output is correct
34 Correct 15 ms 21596 KB Output is correct
35 Correct 15 ms 21708 KB Output is correct
36 Correct 15 ms 21740 KB Output is correct
37 Correct 15 ms 21760 KB Output is correct
38 Correct 16 ms 21784 KB Output is correct
39 Correct 15 ms 21736 KB Output is correct
40 Correct 14 ms 22220 KB Output is correct
41 Correct 15 ms 22340 KB Output is correct
42 Correct 15 ms 21636 KB Output is correct
43 Correct 16 ms 22016 KB Output is correct
44 Correct 16 ms 21964 KB Output is correct
45 Correct 16 ms 21964 KB Output is correct
46 Correct 16 ms 21964 KB Output is correct
47 Correct 16 ms 21964 KB Output is correct
48 Correct 15 ms 21868 KB Output is correct
49 Correct 17 ms 21864 KB Output is correct
50 Correct 16 ms 21840 KB Output is correct
51 Correct 16 ms 21756 KB Output is correct
52 Correct 16 ms 21784 KB Output is correct
53 Correct 16 ms 21836 KB Output is correct
54 Correct 17 ms 21844 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 13 ms 21452 KB Output is correct
2 Correct 13 ms 21352 KB Output is correct
3 Correct 13 ms 21452 KB Output is correct
4 Correct 13 ms 21452 KB Output is correct
5 Correct 13 ms 21488 KB Output is correct
6 Correct 13 ms 21468 KB Output is correct
7 Correct 13 ms 21456 KB Output is correct
8 Correct 13 ms 21404 KB Output is correct
9 Correct 14 ms 21408 KB Output is correct
10 Correct 13 ms 21456 KB Output is correct
11 Correct 12 ms 21452 KB Output is correct
12 Correct 12 ms 21464 KB Output is correct
13 Correct 12 ms 21444 KB Output is correct
14 Correct 13 ms 21408 KB Output is correct
15 Correct 13 ms 21384 KB Output is correct
16 Correct 13 ms 21452 KB Output is correct
17 Correct 13 ms 21424 KB Output is correct
18 Correct 13 ms 21452 KB Output is correct
19 Correct 13 ms 21380 KB Output is correct
20 Correct 13 ms 21468 KB Output is correct
21 Correct 13 ms 21468 KB Output is correct
22 Correct 13 ms 21468 KB Output is correct
23 Correct 13 ms 21456 KB Output is correct
24 Correct 13 ms 21456 KB Output is correct
25 Correct 15 ms 21464 KB Output is correct
26 Correct 14 ms 21448 KB Output is correct
27 Correct 13 ms 21452 KB Output is correct
28 Correct 13 ms 21380 KB Output is correct
29 Correct 13 ms 21372 KB Output is correct
30 Correct 12 ms 21456 KB Output is correct
31 Correct 13 ms 21452 KB Output is correct
32 Correct 14 ms 21580 KB Output is correct
33 Correct 14 ms 21580 KB Output is correct
34 Correct 15 ms 21596 KB Output is correct
35 Correct 15 ms 21708 KB Output is correct
36 Correct 15 ms 21740 KB Output is correct
37 Correct 15 ms 21760 KB Output is correct
38 Correct 16 ms 21784 KB Output is correct
39 Correct 15 ms 21736 KB Output is correct
40 Correct 14 ms 22220 KB Output is correct
41 Correct 15 ms 22340 KB Output is correct
42 Correct 15 ms 21636 KB Output is correct
43 Correct 16 ms 22016 KB Output is correct
44 Correct 16 ms 21964 KB Output is correct
45 Correct 16 ms 21964 KB Output is correct
46 Correct 16 ms 21964 KB Output is correct
47 Correct 16 ms 21964 KB Output is correct
48 Correct 15 ms 21868 KB Output is correct
49 Correct 17 ms 21864 KB Output is correct
50 Correct 16 ms 21840 KB Output is correct
51 Correct 16 ms 21756 KB Output is correct
52 Correct 16 ms 21784 KB Output is correct
53 Correct 16 ms 21836 KB Output is correct
54 Correct 17 ms 21844 KB Output is correct
55 Correct 20 ms 22416 KB Output is correct
56 Correct 45 ms 24792 KB Output is correct
57 Correct 74 ms 26628 KB Output is correct
58 Correct 96 ms 28180 KB Output is correct
59 Correct 130 ms 29984 KB Output is correct
60 Correct 166 ms 31768 KB Output is correct
61 Correct 187 ms 33720 KB Output is correct
62 Correct 210 ms 34880 KB Output is correct
63 Correct 259 ms 37320 KB Output is correct
64 Correct 271 ms 37764 KB Output is correct
65 Correct 130 ms 70084 KB Output is correct
66 Correct 132 ms 70048 KB Output is correct
67 Correct 139 ms 32568 KB Output is correct
68 Correct 200 ms 55472 KB Output is correct
69 Correct 226 ms 56704 KB Output is correct
70 Correct 238 ms 56788 KB Output is correct
71 Correct 252 ms 55464 KB Output is correct
72 Correct 253 ms 55364 KB Output is correct
73 Correct 229 ms 52052 KB Output is correct
74 Correct 227 ms 52140 KB Output is correct
75 Correct 227 ms 50900 KB Output is correct
76 Correct 227 ms 51056 KB Output is correct
77 Correct 232 ms 49892 KB Output is correct
78 Correct 229 ms 48688 KB Output is correct
79 Correct 216 ms 47948 KB Output is correct