#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
vector<ll> top;
vector<vector<int>> ch;
vector<multiset<ll>*> s;
// Slope trick, remove the chs until you get something with slope >= -1.
// new 0, new 1
// x(i-1)+6, x(i)+6
void dfs(int x) {
if(ch[x].size() == 0) {
s[x] = new multiset<ll>();
s[x]->insert(top[x]);
s[x]->insert(top[x]);
return;
}
for(int i = 0; i < ch[x].size(); i++) {
dfs(ch[x][i]);
}
sort(ch[x].rbegin(), ch[x].rend(), [](int y1, int y2){
return s[y1]->size() < s[y2]->size();
});
s[x] = s[ch[x][0]];
for(int i = 1; i < ch[x].size(); i++) {
for(ll g : *(s[ch[x][i]])) {
s[x]->insert(g);
}
s[ch[x][i]]->clear();
}
ll y, z;
int k = ch[x].size() + 1;
while(k--) {
auto it = --(s[x]->end());
y = z;
z = *it;
s[x]->erase(it);
}
s[x]->insert(y+top[x]);
s[x]->insert(z+top[x]);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m;
cin >> n >> m;
n += m;
top.resize(n, 0);
ch.resize(n, vector<int>());
s.resize(n, nullptr);
ll sum = 0;
for(int i = 1; i < n; i++) {
int x;
cin >> x >> top[i];
x--;
sum += top[i];
ch[x].push_back(i);
}
dfs(0);
ll pr = 0, sl = s[0]->size() - 1;
for(ll g : *s[0]) {
sum -= sl * (g - pr);
pr = g;
sl--;
}
cout << sum << '\n';
}
Compilation message
fireworks.cpp: In function 'void dfs(int)':
fireworks.cpp:22:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
22 | for(int i = 0; i < ch[x].size(); i++) {
| ~~^~~~~~~~~~~~~~
fireworks.cpp:29:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
29 | for(int i = 1; i < ch[x].size(); i++) {
| ~~^~~~~~~~~~~~~~
fireworks.cpp:44:19: warning: 'z' may be used uninitialized in this function [-Wmaybe-uninitialized]
44 | s[x]->insert(z+top[x]);
fireworks.cpp:43:19: warning: 'y' may be used uninitialized in this function [-Wmaybe-uninitialized]
43 | s[x]->insert(y+top[x]);
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
0 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
468 KB |
Output is correct |
33 |
Correct |
3 ms |
468 KB |
Output is correct |
34 |
Correct |
2 ms |
596 KB |
Output is correct |
35 |
Correct |
3 ms |
596 KB |
Output is correct |
36 |
Correct |
3 ms |
724 KB |
Output is correct |
37 |
Correct |
3 ms |
724 KB |
Output is correct |
38 |
Correct |
4 ms |
812 KB |
Output is correct |
39 |
Correct |
4 ms |
724 KB |
Output is correct |
40 |
Correct |
2 ms |
1364 KB |
Output is correct |
41 |
Correct |
3 ms |
1364 KB |
Output is correct |
42 |
Correct |
4 ms |
596 KB |
Output is correct |
43 |
Correct |
3 ms |
1108 KB |
Output is correct |
44 |
Correct |
4 ms |
1108 KB |
Output is correct |
45 |
Correct |
3 ms |
1108 KB |
Output is correct |
46 |
Correct |
4 ms |
1108 KB |
Output is correct |
47 |
Correct |
4 ms |
980 KB |
Output is correct |
48 |
Correct |
3 ms |
980 KB |
Output is correct |
49 |
Correct |
4 ms |
1108 KB |
Output is correct |
50 |
Correct |
3 ms |
980 KB |
Output is correct |
51 |
Correct |
3 ms |
980 KB |
Output is correct |
52 |
Correct |
3 ms |
1108 KB |
Output is correct |
53 |
Correct |
3 ms |
980 KB |
Output is correct |
54 |
Correct |
4 ms |
1236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
0 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
468 KB |
Output is correct |
33 |
Correct |
3 ms |
468 KB |
Output is correct |
34 |
Correct |
2 ms |
596 KB |
Output is correct |
35 |
Correct |
3 ms |
596 KB |
Output is correct |
36 |
Correct |
3 ms |
724 KB |
Output is correct |
37 |
Correct |
3 ms |
724 KB |
Output is correct |
38 |
Correct |
4 ms |
812 KB |
Output is correct |
39 |
Correct |
4 ms |
724 KB |
Output is correct |
40 |
Correct |
2 ms |
1364 KB |
Output is correct |
41 |
Correct |
3 ms |
1364 KB |
Output is correct |
42 |
Correct |
4 ms |
596 KB |
Output is correct |
43 |
Correct |
3 ms |
1108 KB |
Output is correct |
44 |
Correct |
4 ms |
1108 KB |
Output is correct |
45 |
Correct |
3 ms |
1108 KB |
Output is correct |
46 |
Correct |
4 ms |
1108 KB |
Output is correct |
47 |
Correct |
4 ms |
980 KB |
Output is correct |
48 |
Correct |
3 ms |
980 KB |
Output is correct |
49 |
Correct |
4 ms |
1108 KB |
Output is correct |
50 |
Correct |
3 ms |
980 KB |
Output is correct |
51 |
Correct |
3 ms |
980 KB |
Output is correct |
52 |
Correct |
3 ms |
1108 KB |
Output is correct |
53 |
Correct |
3 ms |
980 KB |
Output is correct |
54 |
Correct |
4 ms |
1236 KB |
Output is correct |
55 |
Correct |
12 ms |
1620 KB |
Output is correct |
56 |
Correct |
39 ms |
5208 KB |
Output is correct |
57 |
Correct |
63 ms |
8764 KB |
Output is correct |
58 |
Correct |
112 ms |
11124 KB |
Output is correct |
59 |
Correct |
126 ms |
14748 KB |
Output is correct |
60 |
Correct |
178 ms |
21336 KB |
Output is correct |
61 |
Correct |
183 ms |
24332 KB |
Output is correct |
62 |
Correct |
211 ms |
27280 KB |
Output is correct |
63 |
Correct |
247 ms |
32460 KB |
Output is correct |
64 |
Correct |
305 ms |
33528 KB |
Output is correct |
65 |
Correct |
129 ms |
68528 KB |
Output is correct |
66 |
Correct |
122 ms |
68672 KB |
Output is correct |
67 |
Correct |
129 ms |
26440 KB |
Output is correct |
68 |
Correct |
219 ms |
59000 KB |
Output is correct |
69 |
Correct |
247 ms |
55712 KB |
Output is correct |
70 |
Correct |
240 ms |
55660 KB |
Output is correct |
71 |
Correct |
314 ms |
51516 KB |
Output is correct |
72 |
Correct |
330 ms |
51368 KB |
Output is correct |
73 |
Correct |
317 ms |
50768 KB |
Output is correct |
74 |
Correct |
321 ms |
50628 KB |
Output is correct |
75 |
Correct |
297 ms |
50664 KB |
Output is correct |
76 |
Correct |
347 ms |
51040 KB |
Output is correct |
77 |
Correct |
304 ms |
52228 KB |
Output is correct |
78 |
Correct |
341 ms |
52492 KB |
Output is correct |
79 |
Correct |
366 ms |
63580 KB |
Output is correct |