#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();
while (dp[1].a) {
dp[1].a--;
dp[1].b += dp[1].pq->top();
dp[1].pq->pop();
}
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:23:34: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (dp[i].pq->size() > mx_cnt) mx_cnt = dp[i].pq->size(), mx_child = i;
~~~~~~~~~~~~~~~~~^~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
7544 KB |
Output is correct |
2 |
Correct |
9 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 |
8 ms |
7416 KB |
Output is correct |
6 |
Correct |
8 ms |
7416 KB |
Output is correct |
7 |
Correct |
9 ms |
7416 KB |
Output is correct |
8 |
Correct |
9 ms |
7332 KB |
Output is correct |
9 |
Correct |
9 ms |
7388 KB |
Output is correct |
10 |
Correct |
9 ms |
7468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
7544 KB |
Output is correct |
2 |
Correct |
9 ms |
7372 KB |
Output is correct |
3 |
Correct |
9 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 |
10 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 |
7356 KB |
Output is correct |
10 |
Correct |
9 ms |
7416 KB |
Output is correct |
11 |
Correct |
10 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 |
6 ms |
7464 KB |
Output is correct |
15 |
Correct |
9 ms |
7416 KB |
Output is correct |
16 |
Correct |
9 ms |
7416 KB |
Output is correct |
17 |
Correct |
9 ms |
7544 KB |
Output is correct |
18 |
Correct |
8 ms |
7416 KB |
Output is correct |
19 |
Correct |
9 ms |
7544 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
7544 KB |
Output is correct |
2 |
Correct |
9 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 |
8 ms |
7416 KB |
Output is correct |
6 |
Correct |
8 ms |
7416 KB |
Output is correct |
7 |
Correct |
9 ms |
7416 KB |
Output is correct |
8 |
Correct |
9 ms |
7332 KB |
Output is correct |
9 |
Correct |
9 ms |
7388 KB |
Output is correct |
10 |
Correct |
9 ms |
7468 KB |
Output is correct |
11 |
Correct |
9 ms |
7544 KB |
Output is correct |
12 |
Correct |
9 ms |
7372 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 |
10 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 |
7356 KB |
Output is correct |
20 |
Correct |
9 ms |
7416 KB |
Output is correct |
21 |
Correct |
10 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 |
6 ms |
7464 KB |
Output is correct |
25 |
Correct |
9 ms |
7416 KB |
Output is correct |
26 |
Correct |
9 ms |
7416 KB |
Output is correct |
27 |
Correct |
9 ms |
7544 KB |
Output is correct |
28 |
Correct |
8 ms |
7416 KB |
Output is correct |
29 |
Correct |
9 ms |
7544 KB |
Output is correct |
30 |
Correct |
9 ms |
7544 KB |
Output is correct |
31 |
Correct |
9 ms |
7544 KB |
Output is correct |
32 |
Correct |
10 ms |
7684 KB |
Output is correct |
33 |
Correct |
10 ms |
7672 KB |
Output is correct |
34 |
Correct |
11 ms |
7720 KB |
Output is correct |
35 |
Correct |
12 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 |
12 ms |
8056 KB |
Output is correct |
39 |
Correct |
12 ms |
8056 KB |
Output is correct |
40 |
Correct |
12 ms |
8568 KB |
Output is correct |
41 |
Correct |
12 ms |
8524 KB |
Output is correct |
42 |
Correct |
12 ms |
8056 KB |
Output is correct |
43 |
Correct |
13 ms |
8312 KB |
Output is correct |
44 |
Correct |
12 ms |
8184 KB |
Output is correct |
45 |
Correct |
12 ms |
8184 KB |
Output is correct |
46 |
Correct |
12 ms |
8184 KB |
Output is correct |
47 |
Correct |
12 ms |
8184 KB |
Output is correct |
48 |
Correct |
12 ms |
8056 KB |
Output is correct |
49 |
Correct |
12 ms |
8184 KB |
Output is correct |
50 |
Correct |
14 ms |
8056 KB |
Output is correct |
51 |
Correct |
11 ms |
7928 KB |
Output is correct |
52 |
Correct |
12 ms |
8056 KB |
Output is correct |
53 |
Correct |
12 ms |
8184 KB |
Output is correct |
54 |
Correct |
12 ms |
8256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
7544 KB |
Output is correct |
2 |
Correct |
9 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 |
8 ms |
7416 KB |
Output is correct |
6 |
Correct |
8 ms |
7416 KB |
Output is correct |
7 |
Correct |
9 ms |
7416 KB |
Output is correct |
8 |
Correct |
9 ms |
7332 KB |
Output is correct |
9 |
Correct |
9 ms |
7388 KB |
Output is correct |
10 |
Correct |
9 ms |
7468 KB |
Output is correct |
11 |
Correct |
9 ms |
7544 KB |
Output is correct |
12 |
Correct |
9 ms |
7372 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 |
10 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 |
7356 KB |
Output is correct |
20 |
Correct |
9 ms |
7416 KB |
Output is correct |
21 |
Correct |
10 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 |
6 ms |
7464 KB |
Output is correct |
25 |
Correct |
9 ms |
7416 KB |
Output is correct |
26 |
Correct |
9 ms |
7416 KB |
Output is correct |
27 |
Correct |
9 ms |
7544 KB |
Output is correct |
28 |
Correct |
8 ms |
7416 KB |
Output is correct |
29 |
Correct |
9 ms |
7544 KB |
Output is correct |
30 |
Correct |
9 ms |
7544 KB |
Output is correct |
31 |
Correct |
9 ms |
7544 KB |
Output is correct |
32 |
Correct |
10 ms |
7684 KB |
Output is correct |
33 |
Correct |
10 ms |
7672 KB |
Output is correct |
34 |
Correct |
11 ms |
7720 KB |
Output is correct |
35 |
Correct |
12 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 |
12 ms |
8056 KB |
Output is correct |
39 |
Correct |
12 ms |
8056 KB |
Output is correct |
40 |
Correct |
12 ms |
8568 KB |
Output is correct |
41 |
Correct |
12 ms |
8524 KB |
Output is correct |
42 |
Correct |
12 ms |
8056 KB |
Output is correct |
43 |
Correct |
13 ms |
8312 KB |
Output is correct |
44 |
Correct |
12 ms |
8184 KB |
Output is correct |
45 |
Correct |
12 ms |
8184 KB |
Output is correct |
46 |
Correct |
12 ms |
8184 KB |
Output is correct |
47 |
Correct |
12 ms |
8184 KB |
Output is correct |
48 |
Correct |
12 ms |
8056 KB |
Output is correct |
49 |
Correct |
12 ms |
8184 KB |
Output is correct |
50 |
Correct |
14 ms |
8056 KB |
Output is correct |
51 |
Correct |
11 ms |
7928 KB |
Output is correct |
52 |
Correct |
12 ms |
8056 KB |
Output is correct |
53 |
Correct |
12 ms |
8184 KB |
Output is correct |
54 |
Correct |
12 ms |
8256 KB |
Output is correct |
55 |
Correct |
19 ms |
9208 KB |
Output is correct |
56 |
Correct |
56 ms |
14328 KB |
Output is correct |
57 |
Correct |
94 ms |
19192 KB |
Output is correct |
58 |
Correct |
138 ms |
22456 KB |
Output is correct |
59 |
Correct |
161 ms |
27632 KB |
Output is correct |
60 |
Correct |
193 ms |
32500 KB |
Output is correct |
61 |
Correct |
229 ms |
36336 KB |
Output is correct |
62 |
Correct |
251 ms |
39212 KB |
Output is correct |
63 |
Correct |
309 ms |
45292 KB |
Output is correct |
64 |
Correct |
354 ms |
47308 KB |
Output is correct |
65 |
Correct |
176 ms |
77936 KB |
Output is correct |
66 |
Correct |
180 ms |
77932 KB |
Output is correct |
67 |
Correct |
186 ms |
45300 KB |
Output is correct |
68 |
Correct |
226 ms |
62908 KB |
Output is correct |
69 |
Correct |
276 ms |
60172 KB |
Output is correct |
70 |
Correct |
277 ms |
60108 KB |
Output is correct |
71 |
Correct |
254 ms |
59148 KB |
Output is correct |
72 |
Correct |
256 ms |
59232 KB |
Output is correct |
73 |
Correct |
224 ms |
56044 KB |
Output is correct |
74 |
Correct |
226 ms |
56024 KB |
Output is correct |
75 |
Correct |
257 ms |
54776 KB |
Output is correct |
76 |
Correct |
243 ms |
54956 KB |
Output is correct |
77 |
Correct |
235 ms |
53340 KB |
Output is correct |
78 |
Correct |
235 ms |
50788 KB |
Output is correct |
79 |
Correct |
224 ms |
53280 KB |
Output is correct |