# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
175350 | 2020-01-07T06:23:32 Z | onjo0127 | Fireworks (APIO16_fireworks) | C++11 | 428 ms | 86452 KB |
#include <bits/stdc++.h> using namespace std; using ll = long long; using pil = pair<int, ll>; ll zv[300009]; priority_queue<ll> pq[300009]; vector<pil> T[300009]; int sz[300009]; void dfs(int x) { sz[x] = 1; for(auto& it: T[x]) { dfs(it.first); sz[x] += sz[it.first]; } sort(T[x].begin(), T[x].end(), [&](pil P, pil Q) { return sz[P.first] > sz[Q.first]; }); } void go(int x, int p, ll pc) { if(T[x].empty()) return; int mxi = T[x][0].first; for(auto& it: T[x]) go(it.first, p, it.second); for(auto& it: T[x]) { if(it.first == mxi) continue; int t; ll c; tie(t, c) = it; while(pq[t].size()) { pq[mxi].push(pq[t].top()); pq[t].pop(); } zv[mxi] += zv[t]; } vector<ll> S; for(int i=0; i<(int)T[x].size()-1; i++) { S.push_back(pq[mxi].top()); pq[mxi].pop(); } ll a = pq[mxi].top() + pc; pq[mxi].pop(); ll b = pq[mxi].top() + pc; pq[mxi].pop(); pq[mxi].push(a); pq[mxi].push(b); swap(pq[mxi], pq[x]); zv[x] = zv[mxi] + pc; } int main() { int N, M; scanf("%d%d",&N,&M); for(int i=2; i<=N+M; i++) { int p, c; scanf("%d%d",&p,&c); T[p].push_back({i, c}); if(i > N) { pq[i].push(c); pq[i].push(c); zv[i] = c; } } dfs(1); go(1, 1, 0); vector<ll> S; while(pq[1].size()) { S.push_back(pq[1].top()); pq[1].pop(); } reverse(S.begin(), S.end()); ll ans = zv[1], pr = 0, inc = (int)S.size() - 1; for(auto& it: S) { ans -= (it - pr) * inc; pr = it; --inc; } printf("%lld", ans); return 0; }
Compilation message
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 17 ms | 16760 KB | Output is correct |
2 | Correct | 17 ms | 16760 KB | Output is correct |
3 | Correct | 17 ms | 16760 KB | Output is correct |
4 | Correct | 17 ms | 16760 KB | Output is correct |
5 | Correct | 17 ms | 16760 KB | Output is correct |
6 | Correct | 17 ms | 16760 KB | Output is correct |
7 | Correct | 17 ms | 16760 KB | Output is correct |
8 | Correct | 17 ms | 16760 KB | Output is correct |
9 | Correct | 17 ms | 16760 KB | Output is correct |
10 | Correct | 19 ms | 16872 KB | Output is correct |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 17 ms | 16788 KB | Output is correct |
2 | Correct | 17 ms | 16792 KB | Output is correct |
3 | Correct | 17 ms | 16760 KB | Output is correct |
4 | Correct | 17 ms | 16760 KB | Output is correct |
5 | Correct | 17 ms | 16732 KB | Output is correct |
6 | Correct | 17 ms | 16760 KB | Output is correct |
7 | Correct | 17 ms | 16736 KB | Output is correct |
8 | Correct | 17 ms | 16760 KB | Output is correct |
9 | Correct | 17 ms | 16760 KB | Output is correct |
10 | Correct | 22 ms | 16836 KB | Output is correct |
11 | Correct | 17 ms | 16760 KB | Output is correct |
12 | Correct | 17 ms | 16760 KB | Output is correct |
13 | Correct | 17 ms | 16760 KB | Output is correct |
14 | Correct | 17 ms | 16888 KB | Output is correct |
15 | Correct | 17 ms | 16760 KB | Output is correct |
16 | Correct | 17 ms | 16768 KB | Output is correct |
17 | Correct | 17 ms | 16760 KB | Output is correct |
18 | Correct | 17 ms | 16760 KB | Output is correct |
19 | Correct | 17 ms | 16760 KB | Output is correct |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 17 ms | 16760 KB | Output is correct |
2 | Correct | 17 ms | 16760 KB | Output is correct |
3 | Correct | 17 ms | 16760 KB | Output is correct |
4 | Correct | 17 ms | 16760 KB | Output is correct |
5 | Correct | 17 ms | 16760 KB | Output is correct |
6 | Correct | 17 ms | 16760 KB | Output is correct |
7 | Correct | 17 ms | 16760 KB | Output is correct |
8 | Correct | 17 ms | 16760 KB | Output is correct |
9 | Correct | 17 ms | 16760 KB | Output is correct |
10 | Correct | 19 ms | 16872 KB | Output is correct |
11 | Correct | 17 ms | 16788 KB | Output is correct |
12 | Correct | 17 ms | 16792 KB | Output is correct |
13 | Correct | 17 ms | 16760 KB | Output is correct |
14 | Correct | 17 ms | 16760 KB | Output is correct |
15 | Correct | 17 ms | 16732 KB | Output is correct |
16 | Correct | 17 ms | 16760 KB | Output is correct |
17 | Correct | 17 ms | 16736 KB | Output is correct |
18 | Correct | 17 ms | 16760 KB | Output is correct |
19 | Correct | 17 ms | 16760 KB | Output is correct |
20 | Correct | 22 ms | 16836 KB | Output is correct |
21 | Correct | 17 ms | 16760 KB | Output is correct |
22 | Correct | 17 ms | 16760 KB | Output is correct |
23 | Correct | 17 ms | 16760 KB | Output is correct |
24 | Correct | 17 ms | 16888 KB | Output is correct |
25 | Correct | 17 ms | 16760 KB | Output is correct |
26 | Correct | 17 ms | 16768 KB | Output is correct |
27 | Correct | 17 ms | 16760 KB | Output is correct |
28 | Correct | 17 ms | 16760 KB | Output is correct |
29 | Correct | 17 ms | 16760 KB | Output is correct |
30 | Correct | 17 ms | 16760 KB | Output is correct |
31 | Correct | 18 ms | 16916 KB | Output is correct |
32 | Correct | 18 ms | 16888 KB | Output is correct |
33 | Correct | 19 ms | 17016 KB | Output is correct |
34 | Correct | 19 ms | 17016 KB | Output is correct |
35 | Correct | 20 ms | 17148 KB | Output is correct |
36 | Correct | 23 ms | 17272 KB | Output is correct |
37 | Correct | 25 ms | 17244 KB | Output is correct |
38 | Correct | 21 ms | 17144 KB | Output is correct |
39 | Correct | 25 ms | 17272 KB | Output is correct |
40 | Correct | 20 ms | 17912 KB | Output is correct |
41 | Correct | 20 ms | 17912 KB | Output is correct |
42 | Correct | 19 ms | 17040 KB | Output is correct |
43 | Correct | 21 ms | 17656 KB | Output is correct |
44 | Correct | 21 ms | 17528 KB | Output is correct |
45 | Correct | 21 ms | 17472 KB | Output is correct |
46 | Correct | 21 ms | 17400 KB | Output is correct |
47 | Correct | 21 ms | 17400 KB | Output is correct |
48 | Correct | 21 ms | 17272 KB | Output is correct |
49 | Correct | 22 ms | 17400 KB | Output is correct |
50 | Correct | 20 ms | 17272 KB | Output is correct |
51 | Correct | 21 ms | 17244 KB | Output is correct |
52 | Correct | 21 ms | 17272 KB | Output is correct |
53 | Correct | 21 ms | 17272 KB | Output is correct |
54 | Correct | 21 ms | 17400 KB | Output is correct |
# | 결과 | 실행 시간 | 메모리 | Grader output |
---|---|---|---|---|
1 | Correct | 17 ms | 16760 KB | Output is correct |
2 | Correct | 17 ms | 16760 KB | Output is correct |
3 | Correct | 17 ms | 16760 KB | Output is correct |
4 | Correct | 17 ms | 16760 KB | Output is correct |
5 | Correct | 17 ms | 16760 KB | Output is correct |
6 | Correct | 17 ms | 16760 KB | Output is correct |
7 | Correct | 17 ms | 16760 KB | Output is correct |
8 | Correct | 17 ms | 16760 KB | Output is correct |
9 | Correct | 17 ms | 16760 KB | Output is correct |
10 | Correct | 19 ms | 16872 KB | Output is correct |
11 | Correct | 17 ms | 16788 KB | Output is correct |
12 | Correct | 17 ms | 16792 KB | Output is correct |
13 | Correct | 17 ms | 16760 KB | Output is correct |
14 | Correct | 17 ms | 16760 KB | Output is correct |
15 | Correct | 17 ms | 16732 KB | Output is correct |
16 | Correct | 17 ms | 16760 KB | Output is correct |
17 | Correct | 17 ms | 16736 KB | Output is correct |
18 | Correct | 17 ms | 16760 KB | Output is correct |
19 | Correct | 17 ms | 16760 KB | Output is correct |
20 | Correct | 22 ms | 16836 KB | Output is correct |
21 | Correct | 17 ms | 16760 KB | Output is correct |
22 | Correct | 17 ms | 16760 KB | Output is correct |
23 | Correct | 17 ms | 16760 KB | Output is correct |
24 | Correct | 17 ms | 16888 KB | Output is correct |
25 | Correct | 17 ms | 16760 KB | Output is correct |
26 | Correct | 17 ms | 16768 KB | Output is correct |
27 | Correct | 17 ms | 16760 KB | Output is correct |
28 | Correct | 17 ms | 16760 KB | Output is correct |
29 | Correct | 17 ms | 16760 KB | Output is correct |
30 | Correct | 17 ms | 16760 KB | Output is correct |
31 | Correct | 18 ms | 16916 KB | Output is correct |
32 | Correct | 18 ms | 16888 KB | Output is correct |
33 | Correct | 19 ms | 17016 KB | Output is correct |
34 | Correct | 19 ms | 17016 KB | Output is correct |
35 | Correct | 20 ms | 17148 KB | Output is correct |
36 | Correct | 23 ms | 17272 KB | Output is correct |
37 | Correct | 25 ms | 17244 KB | Output is correct |
38 | Correct | 21 ms | 17144 KB | Output is correct |
39 | Correct | 25 ms | 17272 KB | Output is correct |
40 | Correct | 20 ms | 17912 KB | Output is correct |
41 | Correct | 20 ms | 17912 KB | Output is correct |
42 | Correct | 19 ms | 17040 KB | Output is correct |
43 | Correct | 21 ms | 17656 KB | Output is correct |
44 | Correct | 21 ms | 17528 KB | Output is correct |
45 | Correct | 21 ms | 17472 KB | Output is correct |
46 | Correct | 21 ms | 17400 KB | Output is correct |
47 | Correct | 21 ms | 17400 KB | Output is correct |
48 | Correct | 21 ms | 17272 KB | Output is correct |
49 | Correct | 22 ms | 17400 KB | Output is correct |
50 | Correct | 20 ms | 17272 KB | Output is correct |
51 | Correct | 21 ms | 17244 KB | Output is correct |
52 | Correct | 21 ms | 17272 KB | Output is correct |
53 | Correct | 21 ms | 17272 KB | Output is correct |
54 | Correct | 21 ms | 17400 KB | Output is correct |
55 | Correct | 28 ms | 17912 KB | Output is correct |
56 | Correct | 67 ms | 21312 KB | Output is correct |
57 | Correct | 117 ms | 24372 KB | Output is correct |
58 | Correct | 145 ms | 26600 KB | Output is correct |
59 | Correct | 206 ms | 29544 KB | Output is correct |
60 | Correct | 287 ms | 32556 KB | Output is correct |
61 | Correct | 293 ms | 35312 KB | Output is correct |
62 | Correct | 332 ms | 37224 KB | Output is correct |
63 | Correct | 402 ms | 40932 KB | Output is correct |
64 | Correct | 428 ms | 42124 KB | Output is correct |
65 | Correct | 208 ms | 86328 KB | Output is correct |
66 | Correct | 240 ms | 86452 KB | Output is correct |
67 | Correct | 209 ms | 34784 KB | Output is correct |
68 | Correct | 269 ms | 67020 KB | Output is correct |
69 | Correct | 345 ms | 63240 KB | Output is correct |
70 | Correct | 347 ms | 63256 KB | Output is correct |
71 | Correct | 325 ms | 58400 KB | Output is correct |
72 | Correct | 330 ms | 58488 KB | Output is correct |
73 | Correct | 296 ms | 54144 KB | Output is correct |
74 | Correct | 293 ms | 54632 KB | Output is correct |
75 | Correct | 290 ms | 52360 KB | Output is correct |
76 | Correct | 290 ms | 52496 KB | Output is correct |
77 | Correct | 294 ms | 50960 KB | Output is correct |
78 | Correct | 292 ms | 50400 KB | Output is correct |
79 | Correct | 301 ms | 50272 KB | Output is correct |