# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
652852 | 2022-10-24T22:14:16 Z | lovrot | Fireworks (APIO16_fireworks) | C++17 | 255 ms | 95672 KB |
#include <bits/stdc++.h> #define X first #define Y second #define pb push_back using namespace std; typedef pair<int, int> pii; typedef long long ll; struct func{ ll a; ll b; int ind; }; const int N = 300010; int n, m, siz[N]; ll edge[N]; vector<int> g[N]; priority_queue<ll> slope[N]; func merge(func a, func b){ func ret; ret.a = a.a + b.a; ret.b = a.b + b.b; ret.ind = a.ind; while(!slope[b.ind].empty()){ slope[a.ind].push(slope[b.ind].top()); slope[b.ind].pop(); } return ret; } func f(int u){ func ret; if(g[u].empty()){ ret.a = 1; ret.b = -edge[u]; ret.ind = u; slope[u].push(edge[u]); slope[u].push(edge[u]); return ret; } ret = f(g[u][0]); int ind = ret.ind; for(int i = 1; i < g[u].size(); i++){ ret = merge(ret, f(g[u][i])); } while(ret.a > 1){ ll val = slope[ind].top(); slope[ind].pop(); ret.a--; ret.b += val; } ll val = slope[ind].top() + edge[u]; slope[ind].pop(); ll val2 = slope[ind].top() + edge[u]; slope[ind].pop(); slope[ind].push(val); slope[ind].push(val2); ret.b -= edge[u]; return ret; } int dfs(int u){ int ret = 1; for(int v : g[u]){ ret += dfs(v); } siz[u] = ret; return ret; } int main(){ scanf("%d%d\n", &n, &m); for(int i = 2; i <= n + m; i++){ int par; ll val; scanf("%d%lld", &par, &val); g[par].pb(i); edge[i] = val; } dfs(1); for(int i = 1; i <= n; i++){ sort(g[i].begin(), g[i].end(), [](int a, int b) -> bool {return (siz[a] > siz[b]) || (siz[a] == siz[b] && a < b);}); } func res = f(1); ll ans = slope[res.ind].top() + res.b; printf("%lld\n", ans); return 0; }
Compilation message
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 8 ms | 16724 KB | Output is correct |
2 | Correct | 9 ms | 16700 KB | Output is correct |
3 | Correct | 9 ms | 16744 KB | Output is correct |
4 | Correct | 9 ms | 16760 KB | Output is correct |
5 | Correct | 9 ms | 16748 KB | Output is correct |
6 | Correct | 9 ms | 16640 KB | Output is correct |
7 | Correct | 9 ms | 16744 KB | Output is correct |
8 | Correct | 9 ms | 16724 KB | Output is correct |
9 | Correct | 9 ms | 16724 KB | Output is correct |
10 | Correct | 10 ms | 16676 KB | Output is correct |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 9 ms | 16724 KB | Output is correct |
2 | Correct | 8 ms | 16724 KB | Output is correct |
3 | Correct | 8 ms | 16724 KB | Output is correct |
4 | Correct | 9 ms | 16720 KB | Output is correct |
5 | Correct | 9 ms | 16724 KB | Output is correct |
6 | Correct | 9 ms | 16724 KB | Output is correct |
7 | Correct | 9 ms | 16680 KB | Output is correct |
8 | Correct | 9 ms | 16724 KB | Output is correct |
9 | Correct | 9 ms | 16724 KB | Output is correct |
10 | Correct | 9 ms | 16724 KB | Output is correct |
11 | Correct | 9 ms | 16724 KB | Output is correct |
12 | Correct | 9 ms | 16724 KB | Output is correct |
13 | Correct | 9 ms | 16736 KB | Output is correct |
14 | Correct | 9 ms | 16812 KB | Output is correct |
15 | Correct | 10 ms | 16768 KB | Output is correct |
16 | Correct | 9 ms | 16724 KB | Output is correct |
17 | Correct | 9 ms | 16724 KB | Output is correct |
18 | Correct | 9 ms | 16724 KB | Output is correct |
19 | Correct | 9 ms | 16724 KB | Output is correct |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 8 ms | 16724 KB | Output is correct |
2 | Correct | 9 ms | 16700 KB | Output is correct |
3 | Correct | 9 ms | 16744 KB | Output is correct |
4 | Correct | 9 ms | 16760 KB | Output is correct |
5 | Correct | 9 ms | 16748 KB | Output is correct |
6 | Correct | 9 ms | 16640 KB | Output is correct |
7 | Correct | 9 ms | 16744 KB | Output is correct |
8 | Correct | 9 ms | 16724 KB | Output is correct |
9 | Correct | 9 ms | 16724 KB | Output is correct |
10 | Correct | 10 ms | 16676 KB | Output is correct |
11 | Correct | 9 ms | 16724 KB | Output is correct |
12 | Correct | 8 ms | 16724 KB | Output is correct |
13 | Correct | 8 ms | 16724 KB | Output is correct |
14 | Correct | 9 ms | 16720 KB | Output is correct |
15 | Correct | 9 ms | 16724 KB | Output is correct |
16 | Correct | 9 ms | 16724 KB | Output is correct |
17 | Correct | 9 ms | 16680 KB | Output is correct |
18 | Correct | 9 ms | 16724 KB | Output is correct |
19 | Correct | 9 ms | 16724 KB | Output is correct |
20 | Correct | 9 ms | 16724 KB | Output is correct |
21 | Correct | 9 ms | 16724 KB | Output is correct |
22 | Correct | 9 ms | 16724 KB | Output is correct |
23 | Correct | 9 ms | 16736 KB | Output is correct |
24 | Correct | 9 ms | 16812 KB | Output is correct |
25 | Correct | 10 ms | 16768 KB | Output is correct |
26 | Correct | 9 ms | 16724 KB | Output is correct |
27 | Correct | 9 ms | 16724 KB | Output is correct |
28 | Correct | 9 ms | 16724 KB | Output is correct |
29 | Correct | 9 ms | 16724 KB | Output is correct |
30 | Correct | 9 ms | 16724 KB | Output is correct |
31 | Correct | 9 ms | 16764 KB | Output is correct |
32 | Correct | 10 ms | 16756 KB | Output is correct |
33 | Correct | 11 ms | 16852 KB | Output is correct |
34 | Correct | 10 ms | 16888 KB | Output is correct |
35 | Correct | 12 ms | 16932 KB | Output is correct |
36 | Correct | 11 ms | 17036 KB | Output is correct |
37 | Correct | 11 ms | 16980 KB | Output is correct |
38 | Correct | 11 ms | 17008 KB | Output is correct |
39 | Correct | 13 ms | 17044 KB | Output is correct |
40 | Correct | 11 ms | 18040 KB | Output is correct |
41 | Correct | 10 ms | 18036 KB | Output is correct |
42 | Correct | 11 ms | 16960 KB | Output is correct |
43 | Correct | 12 ms | 17492 KB | Output is correct |
44 | Correct | 11 ms | 17364 KB | Output is correct |
45 | Correct | 11 ms | 17400 KB | Output is correct |
46 | Correct | 11 ms | 17108 KB | Output is correct |
47 | Correct | 13 ms | 17108 KB | Output is correct |
48 | Correct | 12 ms | 17020 KB | Output is correct |
49 | Correct | 12 ms | 17108 KB | Output is correct |
50 | Correct | 11 ms | 17068 KB | Output is correct |
51 | Correct | 11 ms | 16980 KB | Output is correct |
52 | Correct | 11 ms | 17072 KB | Output is correct |
53 | Correct | 11 ms | 17144 KB | Output is correct |
54 | Correct | 12 ms | 17140 KB | Output is correct |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 8 ms | 16724 KB | Output is correct |
2 | Correct | 9 ms | 16700 KB | Output is correct |
3 | Correct | 9 ms | 16744 KB | Output is correct |
4 | Correct | 9 ms | 16760 KB | Output is correct |
5 | Correct | 9 ms | 16748 KB | Output is correct |
6 | Correct | 9 ms | 16640 KB | Output is correct |
7 | Correct | 9 ms | 16744 KB | Output is correct |
8 | Correct | 9 ms | 16724 KB | Output is correct |
9 | Correct | 9 ms | 16724 KB | Output is correct |
10 | Correct | 10 ms | 16676 KB | Output is correct |
11 | Correct | 9 ms | 16724 KB | Output is correct |
12 | Correct | 8 ms | 16724 KB | Output is correct |
13 | Correct | 8 ms | 16724 KB | Output is correct |
14 | Correct | 9 ms | 16720 KB | Output is correct |
15 | Correct | 9 ms | 16724 KB | Output is correct |
16 | Correct | 9 ms | 16724 KB | Output is correct |
17 | Correct | 9 ms | 16680 KB | Output is correct |
18 | Correct | 9 ms | 16724 KB | Output is correct |
19 | Correct | 9 ms | 16724 KB | Output is correct |
20 | Correct | 9 ms | 16724 KB | Output is correct |
21 | Correct | 9 ms | 16724 KB | Output is correct |
22 | Correct | 9 ms | 16724 KB | Output is correct |
23 | Correct | 9 ms | 16736 KB | Output is correct |
24 | Correct | 9 ms | 16812 KB | Output is correct |
25 | Correct | 10 ms | 16768 KB | Output is correct |
26 | Correct | 9 ms | 16724 KB | Output is correct |
27 | Correct | 9 ms | 16724 KB | Output is correct |
28 | Correct | 9 ms | 16724 KB | Output is correct |
29 | Correct | 9 ms | 16724 KB | Output is correct |
30 | Correct | 9 ms | 16724 KB | Output is correct |
31 | Correct | 9 ms | 16764 KB | Output is correct |
32 | Correct | 10 ms | 16756 KB | Output is correct |
33 | Correct | 11 ms | 16852 KB | Output is correct |
34 | Correct | 10 ms | 16888 KB | Output is correct |
35 | Correct | 12 ms | 16932 KB | Output is correct |
36 | Correct | 11 ms | 17036 KB | Output is correct |
37 | Correct | 11 ms | 16980 KB | Output is correct |
38 | Correct | 11 ms | 17008 KB | Output is correct |
39 | Correct | 13 ms | 17044 KB | Output is correct |
40 | Correct | 11 ms | 18040 KB | Output is correct |
41 | Correct | 10 ms | 18036 KB | Output is correct |
42 | Correct | 11 ms | 16960 KB | Output is correct |
43 | Correct | 12 ms | 17492 KB | Output is correct |
44 | Correct | 11 ms | 17364 KB | Output is correct |
45 | Correct | 11 ms | 17400 KB | Output is correct |
46 | Correct | 11 ms | 17108 KB | Output is correct |
47 | Correct | 13 ms | 17108 KB | Output is correct |
48 | Correct | 12 ms | 17020 KB | Output is correct |
49 | Correct | 12 ms | 17108 KB | Output is correct |
50 | Correct | 11 ms | 17068 KB | Output is correct |
51 | Correct | 11 ms | 16980 KB | Output is correct |
52 | Correct | 11 ms | 17072 KB | Output is correct |
53 | Correct | 11 ms | 17144 KB | Output is correct |
54 | Correct | 12 ms | 17140 KB | Output is correct |
55 | Correct | 17 ms | 17652 KB | Output is correct |
56 | Correct | 41 ms | 20428 KB | Output is correct |
57 | Correct | 63 ms | 22984 KB | Output is correct |
58 | Correct | 102 ms | 24720 KB | Output is correct |
59 | Correct | 125 ms | 27344 KB | Output is correct |
60 | Correct | 149 ms | 30080 KB | Output is correct |
61 | Correct | 170 ms | 32188 KB | Output is correct |
62 | Correct | 201 ms | 33732 KB | Output is correct |
63 | Correct | 232 ms | 36800 KB | Output is correct |
64 | Correct | 255 ms | 37752 KB | Output is correct |
65 | Correct | 131 ms | 95560 KB | Output is correct |
66 | Correct | 133 ms | 95672 KB | Output is correct |
67 | Correct | 129 ms | 34632 KB | Output is correct |
68 | Correct | 171 ms | 66396 KB | Output is correct |
69 | Correct | 217 ms | 61236 KB | Output is correct |
70 | Correct | 211 ms | 61164 KB | Output is correct |
71 | Correct | 185 ms | 46736 KB | Output is correct |
72 | Correct | 175 ms | 46772 KB | Output is correct |
73 | Correct | 155 ms | 43412 KB | Output is correct |
74 | Correct | 152 ms | 43544 KB | Output is correct |
75 | Correct | 161 ms | 42424 KB | Output is correct |
76 | Correct | 160 ms | 42492 KB | Output is correct |
77 | Correct | 162 ms | 41992 KB | Output is correct |
78 | Correct | 161 ms | 40204 KB | Output is correct |
79 | Correct | 147 ms | 41268 KB | Output is correct |