#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif
using T = array<long long, 3>;
bool smaller(const T &a, const T &b) {
long long A = a[0] * b[1], B = a[1] * b[0];
return A == B ? a[2] < b[2] : A < B;
}
struct cmp {
bool operator()(const T &a, const T &b) const {
return smaller(a, b);
}
};
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
int n, m; cin >> n >> m;
array<vector<int>, 2> a;
a[0].resize(n); a[1].resize(m);
auto nxt = a;
array<vector<bool>, 2> del;
array<priority_queue<T, vector<T>, cmp>, 2> pq;
for (auto it : {0, 1}) {
del[it].resize(a[it].size());
iota(nxt[it].begin(), nxt[it].end(), 1);
for (int &x : a[it]) {
cin >> x;
}
for (int i = 0; i + 1 < a[it].size(); ++i) {
pq[it].push({a[it][i + 1] - a[it][i], 1, i});
}
}
long long res = 0;
auto pop = [&](int it, vector<int> &nxt) {
auto [_, cnt, l] = pq[it].top(); pq[it].pop();
int r = nxt[l];
if (r + 1 == a[it].size()) {
while (cnt--) {
a[it].pop_back();
res += a[it ^ 1].back();
}
} else {
int k = nxt[r];
del[it][r] = 1;
nxt[l] = k;
pq[it].push({a[it][k] - a[it][l], k - l, l});
}
};
auto upd = [&](priority_queue<T, vector<T>, cmp> &pq, vector<bool> &del) {
while (pq.size() && del[pq.top()[2]]) {
pq.pop();
}
};
while (pq[0].size() || pq[1].size()) {
if (!pq[0].size()) {
pop(1, nxt[1]);
} else if (!pq[1].size()) {
pop(0, nxt[0]);
} else if (smaller(pq[0].top(), pq[1].top())) {
pop(1, nxt[1]);
} else {
pop(0, nxt[0]);
}
upd(pq[0], del[0]);
upd(pq[1], del[1]);
}
cout << res;
return 0;
}
Compilation message
kyoto.cpp: In function 'int main()':
kyoto.cpp:39:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
39 | for (int i = 0; i + 1 < a[it].size(); ++i) {
| ~~~~~~^~~~~~~~~~~~~~
kyoto.cpp: In lambda function:
kyoto.cpp:47:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
47 | if (r + 1 == a[it].size()) {
| ~~~~~~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
540 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
464 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
452 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
41 ms |
5844 KB |
Output is correct |
5 |
Correct |
22 ms |
4564 KB |
Output is correct |
6 |
Correct |
12 ms |
2508 KB |
Output is correct |
7 |
Correct |
89 ms |
10468 KB |
Output is correct |
8 |
Correct |
88 ms |
9796 KB |
Output is correct |
9 |
Correct |
89 ms |
10664 KB |
Output is correct |
10 |
Correct |
92 ms |
10960 KB |
Output is correct |
11 |
Correct |
82 ms |
9540 KB |
Output is correct |
12 |
Correct |
106 ms |
10364 KB |
Output is correct |
13 |
Correct |
104 ms |
10444 KB |
Output is correct |
14 |
Correct |
67 ms |
10188 KB |
Output is correct |
15 |
Correct |
34 ms |
10700 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
448 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
424 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
540 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
464 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
452 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
41 ms |
5844 KB |
Output is correct |
33 |
Correct |
22 ms |
4564 KB |
Output is correct |
34 |
Correct |
12 ms |
2508 KB |
Output is correct |
35 |
Correct |
89 ms |
10468 KB |
Output is correct |
36 |
Correct |
88 ms |
9796 KB |
Output is correct |
37 |
Correct |
89 ms |
10664 KB |
Output is correct |
38 |
Correct |
92 ms |
10960 KB |
Output is correct |
39 |
Correct |
82 ms |
9540 KB |
Output is correct |
40 |
Correct |
106 ms |
10364 KB |
Output is correct |
41 |
Correct |
104 ms |
10444 KB |
Output is correct |
42 |
Correct |
67 ms |
10188 KB |
Output is correct |
43 |
Correct |
34 ms |
10700 KB |
Output is correct |
44 |
Correct |
0 ms |
348 KB |
Output is correct |
45 |
Correct |
0 ms |
348 KB |
Output is correct |
46 |
Correct |
0 ms |
348 KB |
Output is correct |
47 |
Correct |
0 ms |
348 KB |
Output is correct |
48 |
Correct |
0 ms |
448 KB |
Output is correct |
49 |
Correct |
0 ms |
348 KB |
Output is correct |
50 |
Correct |
0 ms |
348 KB |
Output is correct |
51 |
Correct |
0 ms |
348 KB |
Output is correct |
52 |
Correct |
0 ms |
424 KB |
Output is correct |
53 |
Correct |
0 ms |
348 KB |
Output is correct |
54 |
Correct |
0 ms |
348 KB |
Output is correct |
55 |
Correct |
45 ms |
6608 KB |
Output is correct |
56 |
Correct |
1 ms |
604 KB |
Output is correct |
57 |
Correct |
4 ms |
1052 KB |
Output is correct |
58 |
Correct |
9 ms |
1748 KB |
Output is correct |
59 |
Correct |
93 ms |
12196 KB |
Output is correct |
60 |
Correct |
97 ms |
11588 KB |
Output is correct |
61 |
Correct |
106 ms |
12224 KB |
Output is correct |
62 |
Correct |
96 ms |
11436 KB |
Output is correct |
63 |
Correct |
58 ms |
11936 KB |
Output is correct |
64 |
Correct |
107 ms |
11456 KB |
Output is correct |
65 |
Correct |
92 ms |
11208 KB |
Output is correct |
66 |
Correct |
68 ms |
11980 KB |
Output is correct |
67 |
Correct |
46 ms |
11720 KB |
Output is correct |