# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
377684 | 2021-03-14T18:38:48 Z | LucaDantas | Fireworks (APIO16_fireworks) | C++17 | 289 ms | 68588 KB |
#include<bits/stdc++.h> using namespace std; #define pb push_back constexpr int maxn = 3e5+10; vector<int> g[maxn]; priority_queue<long long> changes[maxn]; int c[maxn], start_slope[maxn], n; long long start_y[maxn]; void combine(int a, int b) { auto& q = changes[a]; start_y[a] += start_y[b]; start_slope[a] += start_slope[b]; for(; changes[b].size(); changes[b].pop()) q.push(changes[b].top()); } void dfs(int u) { if(u >= n) { start_slope[u] = -1; start_y[u] = c[u]; changes[u].push((long long)c[u]); changes[u].push((long long)c[u]); } else { int big = g[u][0]; for(int v : g[u]) { dfs(v); if(changes[v].size() > changes[big].size()) big = v; } swap(changes[u], changes[big]); start_slope[u] = start_slope[big]; start_y[u] = start_y[big]; for(int v : g[u]) if(v != big) combine(u, v); auto& q = changes[u]; while((int)q.size() > -start_slope[u] + 1) q.pop(); if(!u) return; start_y[u] += (long long)c[u]; assert(q.size() >= 2); long long a = q.top(); q.pop(); long long b = q.top(); q.pop(); q.push(a + c[u]); q.push(b + c[u]); } } int main() { int m; scanf("%d %d", &n, &m); for(int i = 1; i < n+m; i++) { int p; scanf("%d %d", &p, &c[i]); g[p-1].pb(i); } dfs(0); long long ans = start_y[0], last = 0; int slope = start_slope[0]; auto& q = changes[0]; vector<long long> opa; while(q.size()) opa.pb(q.top()), q.pop(); while(opa.size()) { ans += 1ll * slope * (opa.back() - last); last = opa.back(); opa.pop_back(); ++slope; } printf("%lld\n", ans); }
Compilation message
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 11 ms | 16748 KB | Output is correct |
2 | Correct | 12 ms | 16768 KB | Output is correct |
3 | Correct | 12 ms | 16748 KB | Output is correct |
4 | Correct | 11 ms | 16748 KB | Output is correct |
5 | Correct | 11 ms | 16748 KB | Output is correct |
6 | Correct | 12 ms | 16748 KB | Output is correct |
7 | Correct | 11 ms | 16748 KB | Output is correct |
8 | Correct | 12 ms | 16748 KB | Output is correct |
9 | Correct | 11 ms | 16748 KB | Output is correct |
10 | Correct | 12 ms | 16748 KB | Output is correct |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 11 ms | 16748 KB | Output is correct |
2 | Correct | 12 ms | 16748 KB | Output is correct |
3 | Correct | 11 ms | 16748 KB | Output is correct |
4 | Correct | 11 ms | 16748 KB | Output is correct |
5 | Correct | 12 ms | 16748 KB | Output is correct |
6 | Correct | 13 ms | 16748 KB | Output is correct |
7 | Correct | 12 ms | 16748 KB | Output is correct |
8 | Correct | 12 ms | 16748 KB | Output is correct |
9 | Correct | 11 ms | 16748 KB | Output is correct |
10 | Correct | 12 ms | 16748 KB | Output is correct |
11 | Correct | 12 ms | 16768 KB | Output is correct |
12 | Correct | 12 ms | 16748 KB | Output is correct |
13 | Correct | 12 ms | 16748 KB | Output is correct |
14 | Correct | 12 ms | 16748 KB | Output is correct |
15 | Correct | 12 ms | 16748 KB | Output is correct |
16 | Correct | 12 ms | 16856 KB | Output is correct |
17 | Correct | 12 ms | 16748 KB | Output is correct |
18 | Correct | 12 ms | 16748 KB | Output is correct |
19 | Correct | 12 ms | 16748 KB | Output is correct |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 11 ms | 16748 KB | Output is correct |
2 | Correct | 12 ms | 16768 KB | Output is correct |
3 | Correct | 12 ms | 16748 KB | Output is correct |
4 | Correct | 11 ms | 16748 KB | Output is correct |
5 | Correct | 11 ms | 16748 KB | Output is correct |
6 | Correct | 12 ms | 16748 KB | Output is correct |
7 | Correct | 11 ms | 16748 KB | Output is correct |
8 | Correct | 12 ms | 16748 KB | Output is correct |
9 | Correct | 11 ms | 16748 KB | Output is correct |
10 | Correct | 12 ms | 16748 KB | Output is correct |
11 | Correct | 11 ms | 16748 KB | Output is correct |
12 | Correct | 12 ms | 16748 KB | Output is correct |
13 | Correct | 11 ms | 16748 KB | Output is correct |
14 | Correct | 11 ms | 16748 KB | Output is correct |
15 | Correct | 12 ms | 16748 KB | Output is correct |
16 | Correct | 13 ms | 16748 KB | Output is correct |
17 | Correct | 12 ms | 16748 KB | Output is correct |
18 | Correct | 12 ms | 16748 KB | Output is correct |
19 | Correct | 11 ms | 16748 KB | Output is correct |
20 | Correct | 12 ms | 16748 KB | Output is correct |
21 | Correct | 12 ms | 16768 KB | Output is correct |
22 | Correct | 12 ms | 16748 KB | Output is correct |
23 | Correct | 12 ms | 16748 KB | Output is correct |
24 | Correct | 12 ms | 16748 KB | Output is correct |
25 | Correct | 12 ms | 16748 KB | Output is correct |
26 | Correct | 12 ms | 16856 KB | Output is correct |
27 | Correct | 12 ms | 16748 KB | Output is correct |
28 | Correct | 12 ms | 16748 KB | Output is correct |
29 | Correct | 12 ms | 16748 KB | Output is correct |
30 | Correct | 12 ms | 16748 KB | Output is correct |
31 | Correct | 12 ms | 16876 KB | Output is correct |
32 | Correct | 14 ms | 16876 KB | Output is correct |
33 | Correct | 13 ms | 17004 KB | Output is correct |
34 | Correct | 14 ms | 17004 KB | Output is correct |
35 | Correct | 14 ms | 17004 KB | Output is correct |
36 | Correct | 14 ms | 17132 KB | Output is correct |
37 | Correct | 14 ms | 17132 KB | Output is correct |
38 | Correct | 15 ms | 17132 KB | Output is correct |
39 | Correct | 15 ms | 17132 KB | Output is correct |
40 | Correct | 15 ms | 17792 KB | Output is correct |
41 | Correct | 14 ms | 17772 KB | Output is correct |
42 | Correct | 14 ms | 17132 KB | Output is correct |
43 | Correct | 14 ms | 17388 KB | Output is correct |
44 | Correct | 15 ms | 17388 KB | Output is correct |
45 | Correct | 15 ms | 17388 KB | Output is correct |
46 | Correct | 15 ms | 17388 KB | Output is correct |
47 | Correct | 15 ms | 17388 KB | Output is correct |
48 | Correct | 15 ms | 17260 KB | Output is correct |
49 | Correct | 15 ms | 17260 KB | Output is correct |
50 | Correct | 15 ms | 17132 KB | Output is correct |
51 | Correct | 14 ms | 17132 KB | Output is correct |
52 | Correct | 14 ms | 17260 KB | Output is correct |
53 | Correct | 14 ms | 17260 KB | Output is correct |
54 | Correct | 15 ms | 17260 KB | Output is correct |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 11 ms | 16748 KB | Output is correct |
2 | Correct | 12 ms | 16768 KB | Output is correct |
3 | Correct | 12 ms | 16748 KB | Output is correct |
4 | Correct | 11 ms | 16748 KB | Output is correct |
5 | Correct | 11 ms | 16748 KB | Output is correct |
6 | Correct | 12 ms | 16748 KB | Output is correct |
7 | Correct | 11 ms | 16748 KB | Output is correct |
8 | Correct | 12 ms | 16748 KB | Output is correct |
9 | Correct | 11 ms | 16748 KB | Output is correct |
10 | Correct | 12 ms | 16748 KB | Output is correct |
11 | Correct | 11 ms | 16748 KB | Output is correct |
12 | Correct | 12 ms | 16748 KB | Output is correct |
13 | Correct | 11 ms | 16748 KB | Output is correct |
14 | Correct | 11 ms | 16748 KB | Output is correct |
15 | Correct | 12 ms | 16748 KB | Output is correct |
16 | Correct | 13 ms | 16748 KB | Output is correct |
17 | Correct | 12 ms | 16748 KB | Output is correct |
18 | Correct | 12 ms | 16748 KB | Output is correct |
19 | Correct | 11 ms | 16748 KB | Output is correct |
20 | Correct | 12 ms | 16748 KB | Output is correct |
21 | Correct | 12 ms | 16768 KB | Output is correct |
22 | Correct | 12 ms | 16748 KB | Output is correct |
23 | Correct | 12 ms | 16748 KB | Output is correct |
24 | Correct | 12 ms | 16748 KB | Output is correct |
25 | Correct | 12 ms | 16748 KB | Output is correct |
26 | Correct | 12 ms | 16856 KB | Output is correct |
27 | Correct | 12 ms | 16748 KB | Output is correct |
28 | Correct | 12 ms | 16748 KB | Output is correct |
29 | Correct | 12 ms | 16748 KB | Output is correct |
30 | Correct | 12 ms | 16748 KB | Output is correct |
31 | Correct | 12 ms | 16876 KB | Output is correct |
32 | Correct | 14 ms | 16876 KB | Output is correct |
33 | Correct | 13 ms | 17004 KB | Output is correct |
34 | Correct | 14 ms | 17004 KB | Output is correct |
35 | Correct | 14 ms | 17004 KB | Output is correct |
36 | Correct | 14 ms | 17132 KB | Output is correct |
37 | Correct | 14 ms | 17132 KB | Output is correct |
38 | Correct | 15 ms | 17132 KB | Output is correct |
39 | Correct | 15 ms | 17132 KB | Output is correct |
40 | Correct | 15 ms | 17792 KB | Output is correct |
41 | Correct | 14 ms | 17772 KB | Output is correct |
42 | Correct | 14 ms | 17132 KB | Output is correct |
43 | Correct | 14 ms | 17388 KB | Output is correct |
44 | Correct | 15 ms | 17388 KB | Output is correct |
45 | Correct | 15 ms | 17388 KB | Output is correct |
46 | Correct | 15 ms | 17388 KB | Output is correct |
47 | Correct | 15 ms | 17388 KB | Output is correct |
48 | Correct | 15 ms | 17260 KB | Output is correct |
49 | Correct | 15 ms | 17260 KB | Output is correct |
50 | Correct | 15 ms | 17132 KB | Output is correct |
51 | Correct | 14 ms | 17132 KB | Output is correct |
52 | Correct | 14 ms | 17260 KB | Output is correct |
53 | Correct | 14 ms | 17260 KB | Output is correct |
54 | Correct | 15 ms | 17260 KB | Output is correct |
55 | Correct | 20 ms | 17900 KB | Output is correct |
56 | Correct | 48 ms | 20968 KB | Output is correct |
57 | Correct | 82 ms | 23784 KB | Output is correct |
58 | Correct | 105 ms | 25828 KB | Output is correct |
59 | Correct | 135 ms | 28664 KB | Output is correct |
60 | Correct | 179 ms | 31588 KB | Output is correct |
61 | Correct | 199 ms | 34092 KB | Output is correct |
62 | Correct | 220 ms | 35808 KB | Output is correct |
63 | Correct | 274 ms | 39392 KB | Output is correct |
64 | Correct | 289 ms | 40336 KB | Output is correct |
65 | Correct | 145 ms | 68588 KB | Output is correct |
66 | Correct | 146 ms | 68588 KB | Output is correct |
67 | Correct | 159 ms | 35820 KB | Output is correct |
68 | Correct | 214 ms | 56284 KB | Output is correct |
69 | Correct | 243 ms | 53596 KB | Output is correct |
70 | Correct | 244 ms | 53724 KB | Output is correct |
71 | Correct | 257 ms | 54140 KB | Output is correct |
72 | Correct | 266 ms | 54228 KB | Output is correct |
73 | Correct | 238 ms | 50936 KB | Output is correct |
74 | Correct | 240 ms | 51028 KB | Output is correct |
75 | Correct | 234 ms | 49880 KB | Output is correct |
76 | Correct | 238 ms | 49864 KB | Output is correct |
77 | Correct | 241 ms | 48608 KB | Output is correct |
78 | Correct | 243 ms | 47960 KB | Output is correct |
79 | Correct | 230 ms | 46664 KB | Output is correct |