#include <bits/stdc++.h>
#pragma GCC Optimize("O3")
#define FOR(i, x, y) for (int i = x; i < y; i++)
typedef long long ll;
using namespace std;
struct Node {
ll a, b, c;
priority_queue<ll> *pq;
};
Node dp[300001];
vector<int> graph[300001];
void dfs(int node = 1) {
if (graph[node].size()) {
int mx_cnt = 0, mx_child = 0;
for (int i : graph[node]) {
dfs(i);
dp[node].a += dp[i].a;
dp[node].b += dp[i].b;
if (dp[i].pq->size() > mx_cnt)
mx_cnt = dp[i].pq->size(), mx_child = i;
}
dp[node].pq = dp[mx_child].pq;
for (int i : graph[node])
if (i != mx_child)
while (dp[i].pq->size()) {
dp[node].pq->push(dp[i].pq->top());
dp[i].pq->pop();
}
while (dp[node].a > 1) {
dp[node].a--;
dp[node].b += dp[node].pq->top();
dp[node].pq->pop();
}
ll slope1 = dp[node].pq->top();
dp[node].pq->pop();
ll slope0 = dp[node].pq->top();
dp[node].pq->pop();
dp[node].pq->push(slope1 + dp[node].c);
dp[node].pq->push(slope0 + dp[node].c);
dp[node].b -= dp[node].c;
} else {
dp[node].a = 1, dp[node].b = -dp[node].c;
dp[node].pq->push(dp[node].c);
dp[node].pq->push(dp[node].c);
}
}
int main() {
iostream::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
FOR(i, 1, n + m + 1) dp[i].pq = new priority_queue<ll>;
FOR(i, 2, n + m + 1) {
int p, c;
cin >> p >> c;
graph[p].push_back(i);
dp[i].c = c;
}
dfs();
if (dp[1].a) dp[1].b += dp[1].pq->top();
cout << dp[1].b;
return 0;
}
Compilation message
fireworks.cpp:2:0: warning: ignoring #pragma GCC Optimize [-Wunknown-pragmas]
#pragma GCC Optimize("O3")
fireworks.cpp: In function 'void dfs(int)':
fireworks.cpp:22:34: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (dp[i].pq->size() > mx_cnt)
~~~~~~~~~~~~~~~~~^~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
7416 KB |
Output is correct |
2 |
Correct |
8 ms |
7416 KB |
Output is correct |
3 |
Correct |
8 ms |
7416 KB |
Output is correct |
4 |
Correct |
8 ms |
7416 KB |
Output is correct |
5 |
Correct |
9 ms |
7416 KB |
Output is correct |
6 |
Correct |
9 ms |
7416 KB |
Output is correct |
7 |
Correct |
9 ms |
7416 KB |
Output is correct |
8 |
Correct |
9 ms |
7416 KB |
Output is correct |
9 |
Correct |
9 ms |
7416 KB |
Output is correct |
10 |
Correct |
9 ms |
7416 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
7416 KB |
Output is correct |
2 |
Correct |
8 ms |
7416 KB |
Output is correct |
3 |
Correct |
8 ms |
7416 KB |
Output is correct |
4 |
Correct |
8 ms |
7416 KB |
Output is correct |
5 |
Correct |
9 ms |
7420 KB |
Output is correct |
6 |
Correct |
8 ms |
7416 KB |
Output is correct |
7 |
Correct |
8 ms |
7416 KB |
Output is correct |
8 |
Correct |
9 ms |
7416 KB |
Output is correct |
9 |
Correct |
9 ms |
7416 KB |
Output is correct |
10 |
Correct |
9 ms |
7416 KB |
Output is correct |
11 |
Correct |
8 ms |
7416 KB |
Output is correct |
12 |
Correct |
9 ms |
7416 KB |
Output is correct |
13 |
Correct |
9 ms |
7416 KB |
Output is correct |
14 |
Correct |
8 ms |
7416 KB |
Output is correct |
15 |
Correct |
9 ms |
7420 KB |
Output is correct |
16 |
Correct |
9 ms |
7416 KB |
Output is correct |
17 |
Correct |
9 ms |
7416 KB |
Output is correct |
18 |
Correct |
9 ms |
7416 KB |
Output is correct |
19 |
Correct |
9 ms |
7480 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
7416 KB |
Output is correct |
2 |
Correct |
8 ms |
7416 KB |
Output is correct |
3 |
Correct |
8 ms |
7416 KB |
Output is correct |
4 |
Correct |
8 ms |
7416 KB |
Output is correct |
5 |
Correct |
9 ms |
7416 KB |
Output is correct |
6 |
Correct |
9 ms |
7416 KB |
Output is correct |
7 |
Correct |
9 ms |
7416 KB |
Output is correct |
8 |
Correct |
9 ms |
7416 KB |
Output is correct |
9 |
Correct |
9 ms |
7416 KB |
Output is correct |
10 |
Correct |
9 ms |
7416 KB |
Output is correct |
11 |
Correct |
8 ms |
7416 KB |
Output is correct |
12 |
Correct |
8 ms |
7416 KB |
Output is correct |
13 |
Correct |
8 ms |
7416 KB |
Output is correct |
14 |
Correct |
8 ms |
7416 KB |
Output is correct |
15 |
Correct |
9 ms |
7420 KB |
Output is correct |
16 |
Correct |
8 ms |
7416 KB |
Output is correct |
17 |
Correct |
8 ms |
7416 KB |
Output is correct |
18 |
Correct |
9 ms |
7416 KB |
Output is correct |
19 |
Correct |
9 ms |
7416 KB |
Output is correct |
20 |
Correct |
9 ms |
7416 KB |
Output is correct |
21 |
Correct |
8 ms |
7416 KB |
Output is correct |
22 |
Correct |
9 ms |
7416 KB |
Output is correct |
23 |
Correct |
9 ms |
7416 KB |
Output is correct |
24 |
Correct |
8 ms |
7416 KB |
Output is correct |
25 |
Correct |
9 ms |
7420 KB |
Output is correct |
26 |
Correct |
9 ms |
7416 KB |
Output is correct |
27 |
Correct |
9 ms |
7416 KB |
Output is correct |
28 |
Correct |
9 ms |
7416 KB |
Output is correct |
29 |
Correct |
9 ms |
7480 KB |
Output is correct |
30 |
Correct |
9 ms |
7416 KB |
Output is correct |
31 |
Correct |
10 ms |
7556 KB |
Output is correct |
32 |
Correct |
10 ms |
7544 KB |
Output is correct |
33 |
Correct |
10 ms |
7672 KB |
Output is correct |
34 |
Correct |
11 ms |
7800 KB |
Output is correct |
35 |
Correct |
11 ms |
7928 KB |
Output is correct |
36 |
Correct |
12 ms |
7928 KB |
Output is correct |
37 |
Correct |
12 ms |
8056 KB |
Output is correct |
38 |
Correct |
13 ms |
8060 KB |
Output is correct |
39 |
Correct |
12 ms |
8060 KB |
Output is correct |
40 |
Correct |
12 ms |
8572 KB |
Output is correct |
41 |
Correct |
11 ms |
8568 KB |
Output is correct |
42 |
Correct |
11 ms |
8060 KB |
Output is correct |
43 |
Correct |
12 ms |
8312 KB |
Output is correct |
44 |
Correct |
13 ms |
8312 KB |
Output is correct |
45 |
Correct |
13 ms |
8184 KB |
Output is correct |
46 |
Correct |
12 ms |
8188 KB |
Output is correct |
47 |
Correct |
12 ms |
8184 KB |
Output is correct |
48 |
Correct |
11 ms |
8056 KB |
Output is correct |
49 |
Correct |
12 ms |
8184 KB |
Output is correct |
50 |
Correct |
11 ms |
8056 KB |
Output is correct |
51 |
Correct |
11 ms |
7964 KB |
Output is correct |
52 |
Correct |
11 ms |
8056 KB |
Output is correct |
53 |
Correct |
12 ms |
8184 KB |
Output is correct |
54 |
Correct |
12 ms |
8156 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
7416 KB |
Output is correct |
2 |
Correct |
8 ms |
7416 KB |
Output is correct |
3 |
Correct |
8 ms |
7416 KB |
Output is correct |
4 |
Correct |
8 ms |
7416 KB |
Output is correct |
5 |
Correct |
9 ms |
7416 KB |
Output is correct |
6 |
Correct |
9 ms |
7416 KB |
Output is correct |
7 |
Correct |
9 ms |
7416 KB |
Output is correct |
8 |
Correct |
9 ms |
7416 KB |
Output is correct |
9 |
Correct |
9 ms |
7416 KB |
Output is correct |
10 |
Correct |
9 ms |
7416 KB |
Output is correct |
11 |
Correct |
8 ms |
7416 KB |
Output is correct |
12 |
Correct |
8 ms |
7416 KB |
Output is correct |
13 |
Correct |
8 ms |
7416 KB |
Output is correct |
14 |
Correct |
8 ms |
7416 KB |
Output is correct |
15 |
Correct |
9 ms |
7420 KB |
Output is correct |
16 |
Correct |
8 ms |
7416 KB |
Output is correct |
17 |
Correct |
8 ms |
7416 KB |
Output is correct |
18 |
Correct |
9 ms |
7416 KB |
Output is correct |
19 |
Correct |
9 ms |
7416 KB |
Output is correct |
20 |
Correct |
9 ms |
7416 KB |
Output is correct |
21 |
Correct |
8 ms |
7416 KB |
Output is correct |
22 |
Correct |
9 ms |
7416 KB |
Output is correct |
23 |
Correct |
9 ms |
7416 KB |
Output is correct |
24 |
Correct |
8 ms |
7416 KB |
Output is correct |
25 |
Correct |
9 ms |
7420 KB |
Output is correct |
26 |
Correct |
9 ms |
7416 KB |
Output is correct |
27 |
Correct |
9 ms |
7416 KB |
Output is correct |
28 |
Correct |
9 ms |
7416 KB |
Output is correct |
29 |
Correct |
9 ms |
7480 KB |
Output is correct |
30 |
Correct |
9 ms |
7416 KB |
Output is correct |
31 |
Correct |
10 ms |
7556 KB |
Output is correct |
32 |
Correct |
10 ms |
7544 KB |
Output is correct |
33 |
Correct |
10 ms |
7672 KB |
Output is correct |
34 |
Correct |
11 ms |
7800 KB |
Output is correct |
35 |
Correct |
11 ms |
7928 KB |
Output is correct |
36 |
Correct |
12 ms |
7928 KB |
Output is correct |
37 |
Correct |
12 ms |
8056 KB |
Output is correct |
38 |
Correct |
13 ms |
8060 KB |
Output is correct |
39 |
Correct |
12 ms |
8060 KB |
Output is correct |
40 |
Correct |
12 ms |
8572 KB |
Output is correct |
41 |
Correct |
11 ms |
8568 KB |
Output is correct |
42 |
Correct |
11 ms |
8060 KB |
Output is correct |
43 |
Correct |
12 ms |
8312 KB |
Output is correct |
44 |
Correct |
13 ms |
8312 KB |
Output is correct |
45 |
Correct |
13 ms |
8184 KB |
Output is correct |
46 |
Correct |
12 ms |
8188 KB |
Output is correct |
47 |
Correct |
12 ms |
8184 KB |
Output is correct |
48 |
Correct |
11 ms |
8056 KB |
Output is correct |
49 |
Correct |
12 ms |
8184 KB |
Output is correct |
50 |
Correct |
11 ms |
8056 KB |
Output is correct |
51 |
Correct |
11 ms |
7964 KB |
Output is correct |
52 |
Correct |
11 ms |
8056 KB |
Output is correct |
53 |
Correct |
12 ms |
8184 KB |
Output is correct |
54 |
Correct |
12 ms |
8156 KB |
Output is correct |
55 |
Correct |
18 ms |
9208 KB |
Output is correct |
56 |
Correct |
55 ms |
14344 KB |
Output is correct |
57 |
Correct |
93 ms |
19220 KB |
Output is correct |
58 |
Correct |
117 ms |
22592 KB |
Output is correct |
59 |
Correct |
158 ms |
27472 KB |
Output is correct |
60 |
Correct |
194 ms |
32500 KB |
Output is correct |
61 |
Correct |
235 ms |
36344 KB |
Output is correct |
62 |
Correct |
254 ms |
39260 KB |
Output is correct |
63 |
Correct |
307 ms |
45124 KB |
Output is correct |
64 |
Correct |
319 ms |
47108 KB |
Output is correct |
65 |
Correct |
179 ms |
78060 KB |
Output is correct |
66 |
Correct |
179 ms |
77932 KB |
Output is correct |
67 |
Correct |
186 ms |
45304 KB |
Output is correct |
68 |
Correct |
223 ms |
62956 KB |
Output is correct |
69 |
Correct |
292 ms |
60164 KB |
Output is correct |
70 |
Correct |
278 ms |
60392 KB |
Output is correct |
71 |
Correct |
252 ms |
59140 KB |
Output is correct |
72 |
Correct |
251 ms |
59256 KB |
Output is correct |
73 |
Correct |
222 ms |
56084 KB |
Output is correct |
74 |
Correct |
226 ms |
56036 KB |
Output is correct |
75 |
Correct |
229 ms |
54884 KB |
Output is correct |
76 |
Correct |
224 ms |
54884 KB |
Output is correct |
77 |
Correct |
232 ms |
53388 KB |
Output is correct |
78 |
Correct |
240 ms |
50956 KB |
Output is correct |
79 |
Correct |
233 ms |
53520 KB |
Output is correct |