#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].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);
}
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 |
1 ms |
320 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
316 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 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 |
1 ms |
324 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 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 |
316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
320 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
316 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 |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
324 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 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 |
316 KB |
Output is correct |
30 |
Correct |
1 ms |
328 KB |
Output is correct |
31 |
Correct |
3 ms |
468 KB |
Output is correct |
32 |
Correct |
2 ms |
468 KB |
Output is correct |
33 |
Correct |
4 ms |
608 KB |
Output is correct |
34 |
Correct |
6 ms |
596 KB |
Output is correct |
35 |
Correct |
9 ms |
724 KB |
Output is correct |
36 |
Correct |
9 ms |
852 KB |
Output is correct |
37 |
Correct |
12 ms |
852 KB |
Output is correct |
38 |
Correct |
20 ms |
980 KB |
Output is correct |
39 |
Correct |
14 ms |
980 KB |
Output is correct |
40 |
Correct |
3 ms |
1364 KB |
Output is correct |
41 |
Correct |
4 ms |
1364 KB |
Output is correct |
42 |
Correct |
2 ms |
724 KB |
Output is correct |
43 |
Correct |
310 ms |
1240 KB |
Output is correct |
44 |
Correct |
248 ms |
1184 KB |
Output is correct |
45 |
Correct |
251 ms |
1192 KB |
Output is correct |
46 |
Correct |
4 ms |
1108 KB |
Output is correct |
47 |
Correct |
4 ms |
1236 KB |
Output is correct |
48 |
Correct |
4 ms |
1108 KB |
Output is correct |
49 |
Correct |
4 ms |
1108 KB |
Output is correct |
50 |
Correct |
4 ms |
1236 KB |
Output is correct |
51 |
Correct |
4 ms |
1100 KB |
Output is correct |
52 |
Correct |
10 ms |
1236 KB |
Output is correct |
53 |
Correct |
5 ms |
1236 KB |
Output is correct |
54 |
Correct |
6 ms |
1364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
320 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
316 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 |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
324 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 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 |
316 KB |
Output is correct |
30 |
Correct |
1 ms |
328 KB |
Output is correct |
31 |
Correct |
3 ms |
468 KB |
Output is correct |
32 |
Correct |
2 ms |
468 KB |
Output is correct |
33 |
Correct |
4 ms |
608 KB |
Output is correct |
34 |
Correct |
6 ms |
596 KB |
Output is correct |
35 |
Correct |
9 ms |
724 KB |
Output is correct |
36 |
Correct |
9 ms |
852 KB |
Output is correct |
37 |
Correct |
12 ms |
852 KB |
Output is correct |
38 |
Correct |
20 ms |
980 KB |
Output is correct |
39 |
Correct |
14 ms |
980 KB |
Output is correct |
40 |
Correct |
3 ms |
1364 KB |
Output is correct |
41 |
Correct |
4 ms |
1364 KB |
Output is correct |
42 |
Correct |
2 ms |
724 KB |
Output is correct |
43 |
Correct |
310 ms |
1240 KB |
Output is correct |
44 |
Correct |
248 ms |
1184 KB |
Output is correct |
45 |
Correct |
251 ms |
1192 KB |
Output is correct |
46 |
Correct |
4 ms |
1108 KB |
Output is correct |
47 |
Correct |
4 ms |
1236 KB |
Output is correct |
48 |
Correct |
4 ms |
1108 KB |
Output is correct |
49 |
Correct |
4 ms |
1108 KB |
Output is correct |
50 |
Correct |
4 ms |
1236 KB |
Output is correct |
51 |
Correct |
4 ms |
1100 KB |
Output is correct |
52 |
Correct |
10 ms |
1236 KB |
Output is correct |
53 |
Correct |
5 ms |
1236 KB |
Output is correct |
54 |
Correct |
6 ms |
1364 KB |
Output is correct |
55 |
Correct |
45 ms |
1996 KB |
Output is correct |
56 |
Correct |
424 ms |
6880 KB |
Output is correct |
57 |
Correct |
935 ms |
11040 KB |
Output is correct |
58 |
Correct |
1045 ms |
13376 KB |
Output is correct |
59 |
Execution timed out |
2059 ms |
18948 KB |
Time limit exceeded |
60 |
Halted |
0 ms |
0 KB |
- |