#include <bits/stdc++.h>
using namespace std;
const int N = 300005;
struct Node {
long long startingValue;
long long startingSlope;
priority_queue<long long> pq;
};
int n, m, c[N], p[N];
vector<int> G[N];
Node a[N];
void dfs(int u) {
if (u > n) return;
int mx = 0;
for (auto v : G[u]) {
dfs(v);
if (!mx || a[v].pq.size() > a[mx].pq.size()) mx = v;
}
swap(a[mx], a[u]);
for (auto v : G[u]) {
if (v == mx) continue;
while (a[v].pq.size()) {
a[u].pq.push(a[v].pq.top()), a[v].pq.pop();
}
a[u].startingValue += a[v].startingValue;
a[u].startingSlope += a[v].startingSlope;
}
while (a[u].startingSlope + a[u].pq.size() > 1) a[u].pq.pop();
long long x1 = a[u].pq.top(); a[u].pq.pop();
long long x0 = a[u].pq.top(); a[u].pq.pop();
a[u].startingValue += c[u];
a[u].pq.push(x0 + c[u]), a[u].pq.push(x1 + c[u]);
}
int main() {
ios::sync_with_stdio(false);
cin >> n >> m;
for (int i = 2; i <= n + m; ++i) {
cin >> p[i] >> c[i];
G[p[i]].push_back(i);
}
for (int i = n + 1; i <= n + m; ++i) {
a[i].startingSlope = -1;
a[i].startingValue = c[i];
a[i].pq.push(c[i]), a[i].pq.push(c[i]);
}
dfs(1);
vector<long long> v;
while (a[1].pq.size()) {
v.push_back(a[1].pq.top()), a[1].pq.pop();
}
reverse(v.begin(), v.end());
long long cur = 0;
long long curSlope = a[1].startingSlope;
long long res = a[1].startingValue;
// cout << curSlope << ' ' << res << '\n';
// for (auto i : v) cout << i << ' '; cout << '\n';
for (int i = 0; i < v.size(); ++i) {
res += (v[i] - cur) * curSlope, curSlope++, cur = v[i];
}
cout << res;
}
Compilation message
fireworks.cpp: In function 'int main()':
fireworks.cpp:62:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < v.size(); ++i) {
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
25620 KB |
Output is correct |
2 |
Correct |
6 ms |
25620 KB |
Output is correct |
3 |
Correct |
6 ms |
25620 KB |
Output is correct |
4 |
Correct |
0 ms |
25620 KB |
Output is correct |
5 |
Correct |
4 ms |
25620 KB |
Output is correct |
6 |
Correct |
6 ms |
25620 KB |
Output is correct |
7 |
Correct |
13 ms |
25620 KB |
Output is correct |
8 |
Correct |
7 ms |
25620 KB |
Output is correct |
9 |
Correct |
3 ms |
25620 KB |
Output is correct |
10 |
Correct |
3 ms |
25620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
25620 KB |
Output is correct |
2 |
Correct |
6 ms |
25620 KB |
Output is correct |
3 |
Correct |
0 ms |
25620 KB |
Output is correct |
4 |
Correct |
7 ms |
25620 KB |
Output is correct |
5 |
Correct |
6 ms |
25620 KB |
Output is correct |
6 |
Correct |
7 ms |
25620 KB |
Output is correct |
7 |
Correct |
3 ms |
25620 KB |
Output is correct |
8 |
Correct |
6 ms |
25620 KB |
Output is correct |
9 |
Correct |
3 ms |
25620 KB |
Output is correct |
10 |
Correct |
3 ms |
25620 KB |
Output is correct |
11 |
Correct |
3 ms |
25620 KB |
Output is correct |
12 |
Correct |
3 ms |
25620 KB |
Output is correct |
13 |
Correct |
3 ms |
25620 KB |
Output is correct |
14 |
Correct |
5 ms |
25620 KB |
Output is correct |
15 |
Correct |
9 ms |
25620 KB |
Output is correct |
16 |
Correct |
6 ms |
25620 KB |
Output is correct |
17 |
Correct |
7 ms |
25620 KB |
Output is correct |
18 |
Correct |
6 ms |
25620 KB |
Output is correct |
19 |
Correct |
3 ms |
25620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
25620 KB |
Output is correct |
2 |
Correct |
6 ms |
25620 KB |
Output is correct |
3 |
Correct |
6 ms |
25620 KB |
Output is correct |
4 |
Correct |
0 ms |
25620 KB |
Output is correct |
5 |
Correct |
4 ms |
25620 KB |
Output is correct |
6 |
Correct |
6 ms |
25620 KB |
Output is correct |
7 |
Correct |
13 ms |
25620 KB |
Output is correct |
8 |
Correct |
7 ms |
25620 KB |
Output is correct |
9 |
Correct |
3 ms |
25620 KB |
Output is correct |
10 |
Correct |
3 ms |
25620 KB |
Output is correct |
11 |
Correct |
4 ms |
25620 KB |
Output is correct |
12 |
Correct |
6 ms |
25620 KB |
Output is correct |
13 |
Correct |
0 ms |
25620 KB |
Output is correct |
14 |
Correct |
7 ms |
25620 KB |
Output is correct |
15 |
Correct |
6 ms |
25620 KB |
Output is correct |
16 |
Correct |
7 ms |
25620 KB |
Output is correct |
17 |
Correct |
3 ms |
25620 KB |
Output is correct |
18 |
Correct |
6 ms |
25620 KB |
Output is correct |
19 |
Correct |
3 ms |
25620 KB |
Output is correct |
20 |
Correct |
3 ms |
25620 KB |
Output is correct |
21 |
Correct |
3 ms |
25620 KB |
Output is correct |
22 |
Correct |
3 ms |
25620 KB |
Output is correct |
23 |
Correct |
3 ms |
25620 KB |
Output is correct |
24 |
Correct |
5 ms |
25620 KB |
Output is correct |
25 |
Correct |
9 ms |
25620 KB |
Output is correct |
26 |
Correct |
6 ms |
25620 KB |
Output is correct |
27 |
Correct |
7 ms |
25620 KB |
Output is correct |
28 |
Correct |
6 ms |
25620 KB |
Output is correct |
29 |
Correct |
3 ms |
25620 KB |
Output is correct |
30 |
Correct |
2 ms |
25620 KB |
Output is correct |
31 |
Correct |
6 ms |
25620 KB |
Output is correct |
32 |
Correct |
10 ms |
25620 KB |
Output is correct |
33 |
Correct |
3 ms |
25752 KB |
Output is correct |
34 |
Correct |
5 ms |
25752 KB |
Output is correct |
35 |
Correct |
3 ms |
25752 KB |
Output is correct |
36 |
Correct |
3 ms |
25752 KB |
Output is correct |
37 |
Correct |
5 ms |
25888 KB |
Output is correct |
38 |
Correct |
6 ms |
25884 KB |
Output is correct |
39 |
Correct |
9 ms |
25884 KB |
Output is correct |
40 |
Correct |
9 ms |
26328 KB |
Output is correct |
41 |
Correct |
5 ms |
26328 KB |
Output is correct |
42 |
Correct |
7 ms |
25752 KB |
Output is correct |
43 |
Correct |
5 ms |
26128 KB |
Output is correct |
44 |
Correct |
9 ms |
26008 KB |
Output is correct |
45 |
Correct |
9 ms |
26012 KB |
Output is correct |
46 |
Correct |
11 ms |
26064 KB |
Output is correct |
47 |
Correct |
0 ms |
26048 KB |
Output is correct |
48 |
Correct |
0 ms |
26052 KB |
Output is correct |
49 |
Correct |
3 ms |
26088 KB |
Output is correct |
50 |
Correct |
6 ms |
25936 KB |
Output is correct |
51 |
Correct |
7 ms |
25936 KB |
Output is correct |
52 |
Correct |
15 ms |
26060 KB |
Output is correct |
53 |
Correct |
3 ms |
26036 KB |
Output is correct |
54 |
Correct |
12 ms |
26036 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
25620 KB |
Output is correct |
2 |
Correct |
6 ms |
25620 KB |
Output is correct |
3 |
Correct |
6 ms |
25620 KB |
Output is correct |
4 |
Correct |
0 ms |
25620 KB |
Output is correct |
5 |
Correct |
4 ms |
25620 KB |
Output is correct |
6 |
Correct |
6 ms |
25620 KB |
Output is correct |
7 |
Correct |
13 ms |
25620 KB |
Output is correct |
8 |
Correct |
7 ms |
25620 KB |
Output is correct |
9 |
Correct |
3 ms |
25620 KB |
Output is correct |
10 |
Correct |
3 ms |
25620 KB |
Output is correct |
11 |
Correct |
4 ms |
25620 KB |
Output is correct |
12 |
Correct |
6 ms |
25620 KB |
Output is correct |
13 |
Correct |
0 ms |
25620 KB |
Output is correct |
14 |
Correct |
7 ms |
25620 KB |
Output is correct |
15 |
Correct |
6 ms |
25620 KB |
Output is correct |
16 |
Correct |
7 ms |
25620 KB |
Output is correct |
17 |
Correct |
3 ms |
25620 KB |
Output is correct |
18 |
Correct |
6 ms |
25620 KB |
Output is correct |
19 |
Correct |
3 ms |
25620 KB |
Output is correct |
20 |
Correct |
3 ms |
25620 KB |
Output is correct |
21 |
Correct |
3 ms |
25620 KB |
Output is correct |
22 |
Correct |
3 ms |
25620 KB |
Output is correct |
23 |
Correct |
3 ms |
25620 KB |
Output is correct |
24 |
Correct |
5 ms |
25620 KB |
Output is correct |
25 |
Correct |
9 ms |
25620 KB |
Output is correct |
26 |
Correct |
6 ms |
25620 KB |
Output is correct |
27 |
Correct |
7 ms |
25620 KB |
Output is correct |
28 |
Correct |
6 ms |
25620 KB |
Output is correct |
29 |
Correct |
3 ms |
25620 KB |
Output is correct |
30 |
Correct |
2 ms |
25620 KB |
Output is correct |
31 |
Correct |
6 ms |
25620 KB |
Output is correct |
32 |
Correct |
10 ms |
25620 KB |
Output is correct |
33 |
Correct |
3 ms |
25752 KB |
Output is correct |
34 |
Correct |
5 ms |
25752 KB |
Output is correct |
35 |
Correct |
3 ms |
25752 KB |
Output is correct |
36 |
Correct |
3 ms |
25752 KB |
Output is correct |
37 |
Correct |
5 ms |
25888 KB |
Output is correct |
38 |
Correct |
6 ms |
25884 KB |
Output is correct |
39 |
Correct |
9 ms |
25884 KB |
Output is correct |
40 |
Correct |
9 ms |
26328 KB |
Output is correct |
41 |
Correct |
5 ms |
26328 KB |
Output is correct |
42 |
Correct |
7 ms |
25752 KB |
Output is correct |
43 |
Correct |
5 ms |
26128 KB |
Output is correct |
44 |
Correct |
9 ms |
26008 KB |
Output is correct |
45 |
Correct |
9 ms |
26012 KB |
Output is correct |
46 |
Correct |
11 ms |
26064 KB |
Output is correct |
47 |
Correct |
0 ms |
26048 KB |
Output is correct |
48 |
Correct |
0 ms |
26052 KB |
Output is correct |
49 |
Correct |
3 ms |
26088 KB |
Output is correct |
50 |
Correct |
6 ms |
25936 KB |
Output is correct |
51 |
Correct |
7 ms |
25936 KB |
Output is correct |
52 |
Correct |
15 ms |
26060 KB |
Output is correct |
53 |
Correct |
3 ms |
26036 KB |
Output is correct |
54 |
Correct |
12 ms |
26036 KB |
Output is correct |
55 |
Correct |
11 ms |
26316 KB |
Output is correct |
56 |
Correct |
46 ms |
28628 KB |
Output is correct |
57 |
Correct |
72 ms |
30180 KB |
Output is correct |
58 |
Correct |
95 ms |
32152 KB |
Output is correct |
59 |
Correct |
136 ms |
33848 KB |
Output is correct |
60 |
Correct |
182 ms |
35396 KB |
Output is correct |
61 |
Correct |
216 ms |
38224 KB |
Output is correct |
62 |
Correct |
236 ms |
39352 KB |
Output is correct |
63 |
Correct |
289 ms |
41436 KB |
Output is correct |
64 |
Correct |
285 ms |
41812 KB |
Output is correct |
65 |
Correct |
104 ms |
77056 KB |
Output is correct |
66 |
Correct |
117 ms |
77056 KB |
Output is correct |
67 |
Correct |
161 ms |
34992 KB |
Output is correct |
68 |
Correct |
189 ms |
62144 KB |
Output is correct |
69 |
Correct |
222 ms |
58740 KB |
Output is correct |
70 |
Correct |
253 ms |
58740 KB |
Output is correct |
71 |
Correct |
236 ms |
58992 KB |
Output is correct |
72 |
Correct |
232 ms |
58800 KB |
Output is correct |
73 |
Correct |
213 ms |
55184 KB |
Output is correct |
74 |
Correct |
228 ms |
55392 KB |
Output is correct |
75 |
Correct |
221 ms |
55856 KB |
Output is correct |
76 |
Correct |
233 ms |
55904 KB |
Output is correct |
77 |
Correct |
241 ms |
55316 KB |
Output is correct |
78 |
Correct |
212 ms |
53688 KB |
Output is correct |
79 |
Correct |
227 ms |
53468 KB |
Output is correct |