#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
vector<int> 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].begin(), ch[x].end(), [](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);
}
}
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:43:19: warning: 'z' may be used uninitialized in this function [-Wmaybe-uninitialized]
43 | s[x]->insert(z+top[x]);
fireworks.cpp:42:19: warning: 'y' may be used uninitialized in this function [-Wmaybe-uninitialized]
42 | s[x]->insert(y+top[x]);
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 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 |
1 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 |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 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 |
1 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 |
468 KB |
Output is correct |
31 |
Correct |
2 ms |
980 KB |
Output is correct |
32 |
Correct |
2 ms |
976 KB |
Output is correct |
33 |
Correct |
4 ms |
1520 KB |
Output is correct |
34 |
Correct |
6 ms |
2112 KB |
Output is correct |
35 |
Correct |
10 ms |
3924 KB |
Output is correct |
36 |
Correct |
10 ms |
3788 KB |
Output is correct |
37 |
Correct |
8 ms |
3380 KB |
Output is correct |
38 |
Correct |
21 ms |
7892 KB |
Output is correct |
39 |
Correct |
10 ms |
4144 KB |
Output is correct |
40 |
Correct |
3 ms |
1236 KB |
Output is correct |
41 |
Correct |
3 ms |
1228 KB |
Output is correct |
42 |
Correct |
3 ms |
724 KB |
Output is correct |
43 |
Correct |
336 ms |
147960 KB |
Output is correct |
44 |
Correct |
251 ms |
109804 KB |
Output is correct |
45 |
Correct |
259 ms |
107996 KB |
Output is correct |
46 |
Correct |
4 ms |
1868 KB |
Output is correct |
47 |
Correct |
5 ms |
1876 KB |
Output is correct |
48 |
Correct |
4 ms |
1620 KB |
Output is correct |
49 |
Correct |
4 ms |
1748 KB |
Output is correct |
50 |
Correct |
3 ms |
1612 KB |
Output is correct |
51 |
Correct |
3 ms |
1492 KB |
Output is correct |
52 |
Correct |
9 ms |
3556 KB |
Output is correct |
53 |
Correct |
4 ms |
1996 KB |
Output is correct |
54 |
Correct |
3 ms |
1712 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 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 |
1 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 |
468 KB |
Output is correct |
31 |
Correct |
2 ms |
980 KB |
Output is correct |
32 |
Correct |
2 ms |
976 KB |
Output is correct |
33 |
Correct |
4 ms |
1520 KB |
Output is correct |
34 |
Correct |
6 ms |
2112 KB |
Output is correct |
35 |
Correct |
10 ms |
3924 KB |
Output is correct |
36 |
Correct |
10 ms |
3788 KB |
Output is correct |
37 |
Correct |
8 ms |
3380 KB |
Output is correct |
38 |
Correct |
21 ms |
7892 KB |
Output is correct |
39 |
Correct |
10 ms |
4144 KB |
Output is correct |
40 |
Correct |
3 ms |
1236 KB |
Output is correct |
41 |
Correct |
3 ms |
1228 KB |
Output is correct |
42 |
Correct |
3 ms |
724 KB |
Output is correct |
43 |
Correct |
336 ms |
147960 KB |
Output is correct |
44 |
Correct |
251 ms |
109804 KB |
Output is correct |
45 |
Correct |
259 ms |
107996 KB |
Output is correct |
46 |
Correct |
4 ms |
1868 KB |
Output is correct |
47 |
Correct |
5 ms |
1876 KB |
Output is correct |
48 |
Correct |
4 ms |
1620 KB |
Output is correct |
49 |
Correct |
4 ms |
1748 KB |
Output is correct |
50 |
Correct |
3 ms |
1612 KB |
Output is correct |
51 |
Correct |
3 ms |
1492 KB |
Output is correct |
52 |
Correct |
9 ms |
3556 KB |
Output is correct |
53 |
Correct |
4 ms |
1996 KB |
Output is correct |
54 |
Correct |
3 ms |
1712 KB |
Output is correct |
55 |
Correct |
51 ms |
19008 KB |
Output is correct |
56 |
Correct |
485 ms |
172540 KB |
Output is correct |
57 |
Correct |
1052 ms |
357872 KB |
Output is correct |
58 |
Correct |
1210 ms |
399480 KB |
Output is correct |
59 |
Runtime error |
1562 ms |
524288 KB |
Execution killed with signal 9 |
60 |
Halted |
0 ms |
0 KB |
- |