//bs:sanitizers
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
namespace x = __gnu_pbds;
template <typename T>
using ordered_set = x::tree<T, x::null_type, less<T>, x::rb_tree_tag, x::tree_order_statistics_node_update>;
template <typename T>
using normal_queue = priority_queue<T, vector<T>, greater<>>;
#define all(x) begin(x), end(x)
#define sz(x) ((int) (x).size())
#define x first
#define y second
using ll = long long;
using ld = long double;
#define int ll
const int N = 1e5 + 10, M = 2010, INF = 0x3f3f3f3f3f3f3f3f;
int a[2][N], where[2][N];
list<int> c[2];
int version[2][N];
struct Entry {
ld fitness;
int x, y, ver;
list<int>::iterator it;
bool operator<(const Entry &other) const {
return fitness < other.fitness;
}
};
priority_queue<Entry> q;
signed main() {
cin.tie(0)->sync_with_stdio(0);
int n[2];
cin >> n[0] >> n[1];
for (int j = 0; j < 2; ++j) {
for (int i = 0; i < n[j]; ++i) {
cin >> a[j][i];
}
for (int i = 0; i < n[j] - 1; ++i) {
c[j].push_back(i);
q.push({ld(a[j][i] - a[j][i + 1]), j, i, 0, prev(c[j].end())});
}
c[j].push_back(n[j] - 1);
}
for (int iter = 1; sz(q); ++iter) {
auto [fitness, x, y, ver, it] = q.top();
// cout << "using " << fitness << " " << x << " " << y << endl;
q.pop();
if (version[x][y] != ver) {
continue;
}
where[x][y] = iter;
// cout << sz(c[x]) << endl;
if (it != c[x].begin() && sz(c[x]) > 2) {
// cout << "recount delta" << endl;
auto prv = prev(it), nxt = next(it);
version[x][*prv] = iter;
q.push({(a[x][*prv] - a[x][*nxt]) / ld(*nxt - *prv), x, *prv, iter, prv});
}
// cout << "removing " << x << " " << *it << endl;
c[x].erase(it);
}
ll ans = 0;
int cx = 0, cy = 0;
for (int i = 0; i < n[0] + n[1] - 2; ++i) {
// cout << cx << " " << cy << endl;
if (cx != n[0] - 1 && (cy == n[1] - 1 || where[0][cx] < where[1][cy])) {
ans += a[1][cy];
++cx;
} else {
ans += a[0][cx];
++cy;
}
}
// cout << cx << " " << cy << endl;
cout << ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
2 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
2 ms |
468 KB |
Output is correct |
17 |
Correct |
2 ms |
468 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
0 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 |
0 ms |
340 KB |
Output is correct |
23 |
Correct |
0 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
0 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 |
0 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
97 ms |
9092 KB |
Output is correct |
5 |
Correct |
42 ms |
9056 KB |
Output is correct |
6 |
Correct |
27 ms |
4616 KB |
Output is correct |
7 |
Correct |
252 ms |
20588 KB |
Output is correct |
8 |
Correct |
286 ms |
20604 KB |
Output is correct |
9 |
Correct |
254 ms |
20652 KB |
Output is correct |
10 |
Correct |
292 ms |
20652 KB |
Output is correct |
11 |
Correct |
134 ms |
20632 KB |
Output is correct |
12 |
Correct |
260 ms |
20592 KB |
Output is correct |
13 |
Correct |
281 ms |
20604 KB |
Output is correct |
14 |
Correct |
105 ms |
20632 KB |
Output is correct |
15 |
Correct |
128 ms |
20572 KB |
Output is correct |
16 |
Correct |
0 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 |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
0 ms |
340 KB |
Output is correct |
23 |
Correct |
0 ms |
340 KB |
Output is correct |
24 |
Correct |
0 ms |
340 KB |
Output is correct |
25 |
Correct |
0 ms |
340 KB |
Output is correct |
26 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
2 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
2 ms |
468 KB |
Output is correct |
16 |
Correct |
2 ms |
468 KB |
Output is correct |
17 |
Correct |
2 ms |
468 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
0 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 |
0 ms |
340 KB |
Output is correct |
23 |
Correct |
0 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
0 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 |
0 ms |
340 KB |
Output is correct |
29 |
Correct |
0 ms |
340 KB |
Output is correct |
30 |
Correct |
0 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
468 KB |
Output is correct |
32 |
Correct |
97 ms |
9092 KB |
Output is correct |
33 |
Correct |
42 ms |
9056 KB |
Output is correct |
34 |
Correct |
27 ms |
4616 KB |
Output is correct |
35 |
Correct |
252 ms |
20588 KB |
Output is correct |
36 |
Correct |
286 ms |
20604 KB |
Output is correct |
37 |
Correct |
254 ms |
20652 KB |
Output is correct |
38 |
Correct |
292 ms |
20652 KB |
Output is correct |
39 |
Correct |
134 ms |
20632 KB |
Output is correct |
40 |
Correct |
260 ms |
20592 KB |
Output is correct |
41 |
Correct |
281 ms |
20604 KB |
Output is correct |
42 |
Correct |
105 ms |
20632 KB |
Output is correct |
43 |
Correct |
128 ms |
20572 KB |
Output is correct |
44 |
Correct |
0 ms |
340 KB |
Output is correct |
45 |
Correct |
1 ms |
340 KB |
Output is correct |
46 |
Correct |
1 ms |
340 KB |
Output is correct |
47 |
Correct |
0 ms |
340 KB |
Output is correct |
48 |
Correct |
0 ms |
340 KB |
Output is correct |
49 |
Correct |
1 ms |
340 KB |
Output is correct |
50 |
Correct |
0 ms |
340 KB |
Output is correct |
51 |
Correct |
0 ms |
340 KB |
Output is correct |
52 |
Correct |
0 ms |
340 KB |
Output is correct |
53 |
Correct |
0 ms |
340 KB |
Output is correct |
54 |
Correct |
0 ms |
340 KB |
Output is correct |
55 |
Correct |
106 ms |
9264 KB |
Output is correct |
56 |
Correct |
1 ms |
468 KB |
Output is correct |
57 |
Correct |
8 ms |
1424 KB |
Output is correct |
58 |
Correct |
20 ms |
3080 KB |
Output is correct |
59 |
Correct |
273 ms |
20576 KB |
Output is correct |
60 |
Correct |
301 ms |
20592 KB |
Output is correct |
61 |
Correct |
266 ms |
20648 KB |
Output is correct |
62 |
Correct |
278 ms |
20644 KB |
Output is correct |
63 |
Correct |
127 ms |
20440 KB |
Output is correct |
64 |
Correct |
302 ms |
22316 KB |
Output is correct |
65 |
Correct |
282 ms |
22288 KB |
Output is correct |
66 |
Correct |
146 ms |
22336 KB |
Output is correct |
67 |
Correct |
139 ms |
21340 KB |
Output is correct |