#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
// Time complexity: possibly O(N+M log(N+M))
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];
}
}
vector<int> dv = { 0, (int)v[id[i]].size() };
for (int j : g[i]) {
if (id[j] != id[i]) {
v[id[i]].insert(v[id[i]].end(), v[id[j]].begin(), v[id[j]].end());
dv.push_back(v[id[i]].size());
}
}
int ds = dv.size() - 1;
for (int j = 1; j < ds; j *= 2) {
for (int k = 0; k < ds; k += j * 2) {
if (k + j * 2 <= ds) {
vector<long long> tmp(dv[k + j * 2] - dv[k]);
merge(v[id[i]].begin() + dv[k], v[id[i]].begin() + dv[k + j], v[id[i]].begin() + dv[k + j], v[id[i]].begin() + dv[k + j * 2], tmp.begin());
copy(tmp.begin(), tmp.end(), v[id[i]].begin() + dv[k]);
}
else if (k + j < ds) {
vector<long long> tmp(dv[ds] - dv[k]);
merge(v[id[i]].begin() + dv[k], v[id[i]].begin() + dv[k + j], v[id[i]].begin() + dv[k + j], v[id[i]].end(), tmp.begin());
copy(tmp.begin(), tmp.end(), v[id[i]].begin() + dv[k]);
}
}
}
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:25:11: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (mx < v[id[j]].size()) {
~~~^~~~~~~~~~~~~~~~~
fireworks.cpp:56:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < v[id[0]].size(); i++) {
~~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
14456 KB |
Output is correct |
2 |
Correct |
14 ms |
14456 KB |
Output is correct |
3 |
Correct |
12 ms |
14488 KB |
Output is correct |
4 |
Correct |
12 ms |
14536 KB |
Output is correct |
5 |
Correct |
13 ms |
14664 KB |
Output is correct |
6 |
Correct |
12 ms |
14664 KB |
Output is correct |
7 |
Correct |
13 ms |
14664 KB |
Output is correct |
8 |
Correct |
13 ms |
14664 KB |
Output is correct |
9 |
Correct |
14 ms |
14700 KB |
Output is correct |
10 |
Correct |
13 ms |
14716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
14716 KB |
Output is correct |
2 |
Correct |
13 ms |
14716 KB |
Output is correct |
3 |
Correct |
13 ms |
14716 KB |
Output is correct |
4 |
Correct |
14 ms |
14716 KB |
Output is correct |
5 |
Correct |
13 ms |
14716 KB |
Output is correct |
6 |
Correct |
14 ms |
14716 KB |
Output is correct |
7 |
Correct |
14 ms |
14760 KB |
Output is correct |
8 |
Correct |
12 ms |
14760 KB |
Output is correct |
9 |
Correct |
14 ms |
14760 KB |
Output is correct |
10 |
Correct |
12 ms |
14760 KB |
Output is correct |
11 |
Correct |
13 ms |
14760 KB |
Output is correct |
12 |
Correct |
13 ms |
14760 KB |
Output is correct |
13 |
Correct |
13 ms |
14760 KB |
Output is correct |
14 |
Correct |
15 ms |
14760 KB |
Output is correct |
15 |
Correct |
14 ms |
14760 KB |
Output is correct |
16 |
Correct |
13 ms |
14760 KB |
Output is correct |
17 |
Correct |
13 ms |
14760 KB |
Output is correct |
18 |
Correct |
12 ms |
14760 KB |
Output is correct |
19 |
Correct |
13 ms |
14760 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
14456 KB |
Output is correct |
2 |
Correct |
14 ms |
14456 KB |
Output is correct |
3 |
Correct |
12 ms |
14488 KB |
Output is correct |
4 |
Correct |
12 ms |
14536 KB |
Output is correct |
5 |
Correct |
13 ms |
14664 KB |
Output is correct |
6 |
Correct |
12 ms |
14664 KB |
Output is correct |
7 |
Correct |
13 ms |
14664 KB |
Output is correct |
8 |
Correct |
13 ms |
14664 KB |
Output is correct |
9 |
Correct |
14 ms |
14700 KB |
Output is correct |
10 |
Correct |
13 ms |
14716 KB |
Output is correct |
11 |
Correct |
13 ms |
14716 KB |
Output is correct |
12 |
Correct |
13 ms |
14716 KB |
Output is correct |
13 |
Correct |
13 ms |
14716 KB |
Output is correct |
14 |
Correct |
14 ms |
14716 KB |
Output is correct |
15 |
Correct |
13 ms |
14716 KB |
Output is correct |
16 |
Correct |
14 ms |
14716 KB |
Output is correct |
17 |
Correct |
14 ms |
14760 KB |
Output is correct |
18 |
Correct |
12 ms |
14760 KB |
Output is correct |
19 |
Correct |
14 ms |
14760 KB |
Output is correct |
20 |
Correct |
12 ms |
14760 KB |
Output is correct |
21 |
Correct |
13 ms |
14760 KB |
Output is correct |
22 |
Correct |
13 ms |
14760 KB |
Output is correct |
23 |
Correct |
13 ms |
14760 KB |
Output is correct |
24 |
Correct |
15 ms |
14760 KB |
Output is correct |
25 |
Correct |
14 ms |
14760 KB |
Output is correct |
26 |
Correct |
13 ms |
14760 KB |
Output is correct |
27 |
Correct |
13 ms |
14760 KB |
Output is correct |
28 |
Correct |
12 ms |
14760 KB |
Output is correct |
29 |
Correct |
13 ms |
14760 KB |
Output is correct |
30 |
Correct |
13 ms |
14760 KB |
Output is correct |
31 |
Correct |
13 ms |
14760 KB |
Output is correct |
32 |
Correct |
13 ms |
14844 KB |
Output is correct |
33 |
Correct |
15 ms |
14972 KB |
Output is correct |
34 |
Correct |
15 ms |
14972 KB |
Output is correct |
35 |
Correct |
15 ms |
14972 KB |
Output is correct |
36 |
Correct |
15 ms |
14972 KB |
Output is correct |
37 |
Correct |
17 ms |
14972 KB |
Output is correct |
38 |
Correct |
19 ms |
14972 KB |
Output is correct |
39 |
Correct |
16 ms |
14972 KB |
Output is correct |
40 |
Correct |
16 ms |
14972 KB |
Output is correct |
41 |
Correct |
15 ms |
14972 KB |
Output is correct |
42 |
Correct |
15 ms |
14976 KB |
Output is correct |
43 |
Correct |
26 ms |
14976 KB |
Output is correct |
44 |
Correct |
20 ms |
14976 KB |
Output is correct |
45 |
Correct |
20 ms |
14976 KB |
Output is correct |
46 |
Correct |
16 ms |
15100 KB |
Output is correct |
47 |
Correct |
16 ms |
15232 KB |
Output is correct |
48 |
Correct |
15 ms |
15232 KB |
Output is correct |
49 |
Correct |
15 ms |
15232 KB |
Output is correct |
50 |
Correct |
15 ms |
15232 KB |
Output is correct |
51 |
Correct |
17 ms |
15232 KB |
Output is correct |
52 |
Correct |
15 ms |
15232 KB |
Output is correct |
53 |
Correct |
15 ms |
15232 KB |
Output is correct |
54 |
Correct |
15 ms |
15232 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
14456 KB |
Output is correct |
2 |
Correct |
14 ms |
14456 KB |
Output is correct |
3 |
Correct |
12 ms |
14488 KB |
Output is correct |
4 |
Correct |
12 ms |
14536 KB |
Output is correct |
5 |
Correct |
13 ms |
14664 KB |
Output is correct |
6 |
Correct |
12 ms |
14664 KB |
Output is correct |
7 |
Correct |
13 ms |
14664 KB |
Output is correct |
8 |
Correct |
13 ms |
14664 KB |
Output is correct |
9 |
Correct |
14 ms |
14700 KB |
Output is correct |
10 |
Correct |
13 ms |
14716 KB |
Output is correct |
11 |
Correct |
13 ms |
14716 KB |
Output is correct |
12 |
Correct |
13 ms |
14716 KB |
Output is correct |
13 |
Correct |
13 ms |
14716 KB |
Output is correct |
14 |
Correct |
14 ms |
14716 KB |
Output is correct |
15 |
Correct |
13 ms |
14716 KB |
Output is correct |
16 |
Correct |
14 ms |
14716 KB |
Output is correct |
17 |
Correct |
14 ms |
14760 KB |
Output is correct |
18 |
Correct |
12 ms |
14760 KB |
Output is correct |
19 |
Correct |
14 ms |
14760 KB |
Output is correct |
20 |
Correct |
12 ms |
14760 KB |
Output is correct |
21 |
Correct |
13 ms |
14760 KB |
Output is correct |
22 |
Correct |
13 ms |
14760 KB |
Output is correct |
23 |
Correct |
13 ms |
14760 KB |
Output is correct |
24 |
Correct |
15 ms |
14760 KB |
Output is correct |
25 |
Correct |
14 ms |
14760 KB |
Output is correct |
26 |
Correct |
13 ms |
14760 KB |
Output is correct |
27 |
Correct |
13 ms |
14760 KB |
Output is correct |
28 |
Correct |
12 ms |
14760 KB |
Output is correct |
29 |
Correct |
13 ms |
14760 KB |
Output is correct |
30 |
Correct |
13 ms |
14760 KB |
Output is correct |
31 |
Correct |
13 ms |
14760 KB |
Output is correct |
32 |
Correct |
13 ms |
14844 KB |
Output is correct |
33 |
Correct |
15 ms |
14972 KB |
Output is correct |
34 |
Correct |
15 ms |
14972 KB |
Output is correct |
35 |
Correct |
15 ms |
14972 KB |
Output is correct |
36 |
Correct |
15 ms |
14972 KB |
Output is correct |
37 |
Correct |
17 ms |
14972 KB |
Output is correct |
38 |
Correct |
19 ms |
14972 KB |
Output is correct |
39 |
Correct |
16 ms |
14972 KB |
Output is correct |
40 |
Correct |
16 ms |
14972 KB |
Output is correct |
41 |
Correct |
15 ms |
14972 KB |
Output is correct |
42 |
Correct |
15 ms |
14976 KB |
Output is correct |
43 |
Correct |
26 ms |
14976 KB |
Output is correct |
44 |
Correct |
20 ms |
14976 KB |
Output is correct |
45 |
Correct |
20 ms |
14976 KB |
Output is correct |
46 |
Correct |
16 ms |
15100 KB |
Output is correct |
47 |
Correct |
16 ms |
15232 KB |
Output is correct |
48 |
Correct |
15 ms |
15232 KB |
Output is correct |
49 |
Correct |
15 ms |
15232 KB |
Output is correct |
50 |
Correct |
15 ms |
15232 KB |
Output is correct |
51 |
Correct |
17 ms |
15232 KB |
Output is correct |
52 |
Correct |
15 ms |
15232 KB |
Output is correct |
53 |
Correct |
15 ms |
15232 KB |
Output is correct |
54 |
Correct |
15 ms |
15232 KB |
Output is correct |
55 |
Correct |
20 ms |
15612 KB |
Output is correct |
56 |
Correct |
49 ms |
18088 KB |
Output is correct |
57 |
Correct |
90 ms |
20520 KB |
Output is correct |
58 |
Correct |
115 ms |
22204 KB |
Output is correct |
59 |
Correct |
167 ms |
24668 KB |
Output is correct |
60 |
Correct |
193 ms |
27216 KB |
Output is correct |
61 |
Correct |
240 ms |
29048 KB |
Output is correct |
62 |
Correct |
260 ms |
30356 KB |
Output is correct |
63 |
Correct |
291 ms |
34008 KB |
Output is correct |
64 |
Correct |
403 ms |
34452 KB |
Output is correct |
65 |
Correct |
121 ms |
34452 KB |
Output is correct |
66 |
Correct |
120 ms |
34452 KB |
Output is correct |
67 |
Correct |
121 ms |
34452 KB |
Output is correct |
68 |
Execution timed out |
2070 ms |
34452 KB |
Time limit exceeded |
69 |
Halted |
0 ms |
0 KB |
- |