#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int N, M, p[300009], c[300009], id[300009]; vector<int> g[300009]; vector<long long> v[300009];
int main() {
cin.tie(0);
ios_base::sync_with_stdio(false);
cin >> N >> M;
long long sum = 0;
for (int i = 1; i < N + M; i++) {
cin >> p[i] >> c[i];
g[--p[i]].push_back(i);
sum += c[i];
if (i >= N) {
v[i - N].push_back(c[i]);
v[i - N].push_back(c[i]);
id[i] = i - N;
}
}
for (int i = N - 1; i >= 0; i--) {
int mx = 0;
for (int j : g[i]) {
if (mx < v[id[j]].size()) {
mx = v[id[j]].size();
id[i] = id[j];
}
}
for (int j : g[i]) {
if (id[j] != id[i]) {
int sz = v[id[i]].size();
v[id[i]].resize(v[id[i]].size() + v[id[j]].size());
copy(v[id[j]].begin(), v[id[j]].end(), v[id[i]].begin() + sz);
}
}
sort(v[id[i]].begin(), v[id[i]].end());
v[id[i]].resize(v[id[i]].size() - g[i].size() + 1);
v[id[i]][v[id[i]].size() - 1] += c[i];
v[id[i]][v[id[i]].size() - 2] += c[i];
}
for (int i = 0; i < v[id[0]].size(); i++) {
sum -= (v[id[0]][i] - (i == 0 ? 0 : v[id[0]][i - 1])) * (v[id[0]].size() - i - 1);
}
cout << sum << '\n';
return 0;
}
Compilation message
fireworks.cpp: In function 'int main()':
fireworks.cpp:24:11: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (mx < v[id[j]].size()) {
~~~^~~~~~~~~~~~~~~~~
fireworks.cpp:41:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < v[id[0]].size(); i++) {
~~^~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
14456 KB |
Output is correct |
2 |
Correct |
13 ms |
14456 KB |
Output is correct |
3 |
Correct |
14 ms |
14488 KB |
Output is correct |
4 |
Correct |
13 ms |
14548 KB |
Output is correct |
5 |
Correct |
12 ms |
14548 KB |
Output is correct |
6 |
Correct |
13 ms |
14624 KB |
Output is correct |
7 |
Correct |
13 ms |
14624 KB |
Output is correct |
8 |
Correct |
13 ms |
14768 KB |
Output is correct |
9 |
Correct |
13 ms |
14768 KB |
Output is correct |
10 |
Correct |
12 ms |
14768 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
14824 KB |
Output is correct |
2 |
Correct |
13 ms |
14824 KB |
Output is correct |
3 |
Correct |
12 ms |
14824 KB |
Output is correct |
4 |
Correct |
13 ms |
14824 KB |
Output is correct |
5 |
Correct |
13 ms |
14824 KB |
Output is correct |
6 |
Correct |
13 ms |
14824 KB |
Output is correct |
7 |
Correct |
12 ms |
14824 KB |
Output is correct |
8 |
Correct |
13 ms |
14824 KB |
Output is correct |
9 |
Correct |
13 ms |
14824 KB |
Output is correct |
10 |
Correct |
13 ms |
14844 KB |
Output is correct |
11 |
Correct |
13 ms |
14844 KB |
Output is correct |
12 |
Correct |
13 ms |
14844 KB |
Output is correct |
13 |
Correct |
13 ms |
14844 KB |
Output is correct |
14 |
Correct |
12 ms |
14844 KB |
Output is correct |
15 |
Correct |
13 ms |
14844 KB |
Output is correct |
16 |
Correct |
12 ms |
14844 KB |
Output is correct |
17 |
Correct |
13 ms |
14844 KB |
Output is correct |
18 |
Correct |
13 ms |
14844 KB |
Output is correct |
19 |
Correct |
12 ms |
14844 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
14456 KB |
Output is correct |
2 |
Correct |
13 ms |
14456 KB |
Output is correct |
3 |
Correct |
14 ms |
14488 KB |
Output is correct |
4 |
Correct |
13 ms |
14548 KB |
Output is correct |
5 |
Correct |
12 ms |
14548 KB |
Output is correct |
6 |
Correct |
13 ms |
14624 KB |
Output is correct |
7 |
Correct |
13 ms |
14624 KB |
Output is correct |
8 |
Correct |
13 ms |
14768 KB |
Output is correct |
9 |
Correct |
13 ms |
14768 KB |
Output is correct |
10 |
Correct |
12 ms |
14768 KB |
Output is correct |
11 |
Correct |
13 ms |
14824 KB |
Output is correct |
12 |
Correct |
13 ms |
14824 KB |
Output is correct |
13 |
Correct |
12 ms |
14824 KB |
Output is correct |
14 |
Correct |
13 ms |
14824 KB |
Output is correct |
15 |
Correct |
13 ms |
14824 KB |
Output is correct |
16 |
Correct |
13 ms |
14824 KB |
Output is correct |
17 |
Correct |
12 ms |
14824 KB |
Output is correct |
18 |
Correct |
13 ms |
14824 KB |
Output is correct |
19 |
Correct |
13 ms |
14824 KB |
Output is correct |
20 |
Correct |
13 ms |
14844 KB |
Output is correct |
21 |
Correct |
13 ms |
14844 KB |
Output is correct |
22 |
Correct |
13 ms |
14844 KB |
Output is correct |
23 |
Correct |
13 ms |
14844 KB |
Output is correct |
24 |
Correct |
12 ms |
14844 KB |
Output is correct |
25 |
Correct |
13 ms |
14844 KB |
Output is correct |
26 |
Correct |
12 ms |
14844 KB |
Output is correct |
27 |
Correct |
13 ms |
14844 KB |
Output is correct |
28 |
Correct |
13 ms |
14844 KB |
Output is correct |
29 |
Correct |
12 ms |
14844 KB |
Output is correct |
30 |
Correct |
13 ms |
14844 KB |
Output is correct |
31 |
Correct |
14 ms |
14844 KB |
Output is correct |
32 |
Correct |
14 ms |
14848 KB |
Output is correct |
33 |
Correct |
14 ms |
14848 KB |
Output is correct |
34 |
Correct |
15 ms |
14848 KB |
Output is correct |
35 |
Correct |
16 ms |
14996 KB |
Output is correct |
36 |
Correct |
16 ms |
14996 KB |
Output is correct |
37 |
Correct |
16 ms |
14996 KB |
Output is correct |
38 |
Correct |
20 ms |
14996 KB |
Output is correct |
39 |
Correct |
18 ms |
14996 KB |
Output is correct |
40 |
Correct |
15 ms |
14996 KB |
Output is correct |
41 |
Correct |
14 ms |
14996 KB |
Output is correct |
42 |
Correct |
14 ms |
14996 KB |
Output is correct |
43 |
Correct |
87 ms |
15124 KB |
Output is correct |
44 |
Correct |
78 ms |
15124 KB |
Output is correct |
45 |
Correct |
74 ms |
15124 KB |
Output is correct |
46 |
Correct |
15 ms |
15124 KB |
Output is correct |
47 |
Correct |
15 ms |
15124 KB |
Output is correct |
48 |
Correct |
15 ms |
15124 KB |
Output is correct |
49 |
Correct |
15 ms |
15124 KB |
Output is correct |
50 |
Correct |
15 ms |
15124 KB |
Output is correct |
51 |
Correct |
16 ms |
15124 KB |
Output is correct |
52 |
Correct |
17 ms |
15124 KB |
Output is correct |
53 |
Correct |
15 ms |
15124 KB |
Output is correct |
54 |
Correct |
15 ms |
15124 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
14456 KB |
Output is correct |
2 |
Correct |
13 ms |
14456 KB |
Output is correct |
3 |
Correct |
14 ms |
14488 KB |
Output is correct |
4 |
Correct |
13 ms |
14548 KB |
Output is correct |
5 |
Correct |
12 ms |
14548 KB |
Output is correct |
6 |
Correct |
13 ms |
14624 KB |
Output is correct |
7 |
Correct |
13 ms |
14624 KB |
Output is correct |
8 |
Correct |
13 ms |
14768 KB |
Output is correct |
9 |
Correct |
13 ms |
14768 KB |
Output is correct |
10 |
Correct |
12 ms |
14768 KB |
Output is correct |
11 |
Correct |
13 ms |
14824 KB |
Output is correct |
12 |
Correct |
13 ms |
14824 KB |
Output is correct |
13 |
Correct |
12 ms |
14824 KB |
Output is correct |
14 |
Correct |
13 ms |
14824 KB |
Output is correct |
15 |
Correct |
13 ms |
14824 KB |
Output is correct |
16 |
Correct |
13 ms |
14824 KB |
Output is correct |
17 |
Correct |
12 ms |
14824 KB |
Output is correct |
18 |
Correct |
13 ms |
14824 KB |
Output is correct |
19 |
Correct |
13 ms |
14824 KB |
Output is correct |
20 |
Correct |
13 ms |
14844 KB |
Output is correct |
21 |
Correct |
13 ms |
14844 KB |
Output is correct |
22 |
Correct |
13 ms |
14844 KB |
Output is correct |
23 |
Correct |
13 ms |
14844 KB |
Output is correct |
24 |
Correct |
12 ms |
14844 KB |
Output is correct |
25 |
Correct |
13 ms |
14844 KB |
Output is correct |
26 |
Correct |
12 ms |
14844 KB |
Output is correct |
27 |
Correct |
13 ms |
14844 KB |
Output is correct |
28 |
Correct |
13 ms |
14844 KB |
Output is correct |
29 |
Correct |
12 ms |
14844 KB |
Output is correct |
30 |
Correct |
13 ms |
14844 KB |
Output is correct |
31 |
Correct |
14 ms |
14844 KB |
Output is correct |
32 |
Correct |
14 ms |
14848 KB |
Output is correct |
33 |
Correct |
14 ms |
14848 KB |
Output is correct |
34 |
Correct |
15 ms |
14848 KB |
Output is correct |
35 |
Correct |
16 ms |
14996 KB |
Output is correct |
36 |
Correct |
16 ms |
14996 KB |
Output is correct |
37 |
Correct |
16 ms |
14996 KB |
Output is correct |
38 |
Correct |
20 ms |
14996 KB |
Output is correct |
39 |
Correct |
18 ms |
14996 KB |
Output is correct |
40 |
Correct |
15 ms |
14996 KB |
Output is correct |
41 |
Correct |
14 ms |
14996 KB |
Output is correct |
42 |
Correct |
14 ms |
14996 KB |
Output is correct |
43 |
Correct |
87 ms |
15124 KB |
Output is correct |
44 |
Correct |
78 ms |
15124 KB |
Output is correct |
45 |
Correct |
74 ms |
15124 KB |
Output is correct |
46 |
Correct |
15 ms |
15124 KB |
Output is correct |
47 |
Correct |
15 ms |
15124 KB |
Output is correct |
48 |
Correct |
15 ms |
15124 KB |
Output is correct |
49 |
Correct |
15 ms |
15124 KB |
Output is correct |
50 |
Correct |
15 ms |
15124 KB |
Output is correct |
51 |
Correct |
16 ms |
15124 KB |
Output is correct |
52 |
Correct |
17 ms |
15124 KB |
Output is correct |
53 |
Correct |
15 ms |
15124 KB |
Output is correct |
54 |
Correct |
15 ms |
15124 KB |
Output is correct |
55 |
Correct |
40 ms |
15484 KB |
Output is correct |
56 |
Correct |
189 ms |
18204 KB |
Output is correct |
57 |
Correct |
390 ms |
20428 KB |
Output is correct |
58 |
Correct |
435 ms |
22184 KB |
Output is correct |
59 |
Correct |
1008 ms |
24512 KB |
Output is correct |
60 |
Correct |
980 ms |
27100 KB |
Output is correct |
61 |
Correct |
1330 ms |
28152 KB |
Output is correct |
62 |
Correct |
1472 ms |
29884 KB |
Output is correct |
63 |
Correct |
1406 ms |
33364 KB |
Output is correct |
64 |
Execution timed out |
2070 ms |
33364 KB |
Time limit exceeded |
65 |
Halted |
0 ms |
0 KB |
- |