#ifdef LOCAL
#define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
#ifndef LOCAL
// #pragma GCC optimize("O3")
// #pragma GCC optimize("Ofast")
// #pragma GCC optimize("unroll-loops")
// #pragma GCC target("avx,avx2,bmi,bmi2,popcnt,lzcnt")
#endif
using namespace std;
using ll = long long;
using ld = long double;
#define x first
#define y second
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
mt19937_64 mt(time(0));
void solve();
void init();
int32_t main() {
#ifndef LOCAL
cin.tie(nullptr)->sync_with_stdio(false);
#else
freopen("input.txt", "r", stdin);
#endif
init();
int t = 1;
// cin >> t;
while (t--) {
solve();
}
}
void init() {}
void solve() {
int h, w;
cin >> h >> w;
vector<pair<ll, ll>> a1(h), b1(w), a, b;
for (int i = 0; i < h; i++) {
cin >> a1[i].x;
a1[i].y = 1;
}
for (int i = 0; i < w; i++) {
cin >> b1[i].x;
b1[i].y = 1;
}
for (int i = 0; i < h; i++) {
while (a.size() >= 3) {
auto ff = a.back();
a.pop_back();
if (!(a1[i].x <= ff.x && a.back().x <= ff.x)) {
a.push_back(ff);
break;
} else {
a.back().y += ff.y;
}
}
a.push_back(a1[i]);
}
for (int i = 0; i < w; i++) {
while (b.size() >= 3) {
auto ff = b.back();
b.pop_back();
if (!(b1[i].x <= ff.x && b.back().x <= ff.x)) {
b.push_back(ff);
break;
} else {
b.back().y += ff.y;
}
}
b.push_back(b1[i]);
}
h = a.size();
w = b.size();
// cout << h << ' ' << w << '\n';
vector<vector<ll>> dp(h + 1, vector<ll>(w + 1, INT64_MAX));
// for (auto i : a) {
// cout << i.x << ' ' << i.y << '\n';
// }
// for (auto i : b) {
// cout << i.x << ' ' << i.y << '\n';
// }
dp[0][0] = 0;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
// cout << dp[i][j] << ' ';
dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + b[j].x * a[i].y);
dp[i][j + 1] = min(dp[i][j + 1], dp[i][j] + a[i].x * b[j].y);
}
// cout << '\n';
}
cout << dp[h - 1][w - 1] << '\n';
}
# |
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 |
0 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 |
1 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 |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
7 ms |
8284 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
7 ms |
8284 KB |
Output is correct |
17 |
Correct |
1 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 |
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 |
2 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 |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
344 KB |
Output is correct |
28 |
Correct |
0 ms |
344 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 |
3 ms |
2904 KB |
Output is correct |
4 |
Correct |
6 ms |
2136 KB |
Output is correct |
5 |
Correct |
6 ms |
3932 KB |
Output is correct |
6 |
Correct |
3 ms |
1116 KB |
Output is correct |
7 |
Correct |
13 ms |
4188 KB |
Output is correct |
8 |
Correct |
16 ms |
4184 KB |
Output is correct |
9 |
Correct |
14 ms |
5212 KB |
Output is correct |
10 |
Correct |
15 ms |
5148 KB |
Output is correct |
11 |
Correct |
39 ms |
35728 KB |
Output is correct |
12 |
Correct |
14 ms |
5468 KB |
Output is correct |
13 |
Correct |
17 ms |
5504 KB |
Output is correct |
14 |
Correct |
38 ms |
35232 KB |
Output is correct |
15 |
Correct |
12 ms |
4440 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
464 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
456 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
1 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 |
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 |
0 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 |
1 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 |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
7 ms |
8284 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
7 ms |
8284 KB |
Output is correct |
17 |
Correct |
1 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 |
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 |
2 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 |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
344 KB |
Output is correct |
28 |
Correct |
0 ms |
344 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
3 ms |
2904 KB |
Output is correct |
32 |
Correct |
6 ms |
2136 KB |
Output is correct |
33 |
Correct |
6 ms |
3932 KB |
Output is correct |
34 |
Correct |
3 ms |
1116 KB |
Output is correct |
35 |
Correct |
13 ms |
4188 KB |
Output is correct |
36 |
Correct |
16 ms |
4184 KB |
Output is correct |
37 |
Correct |
14 ms |
5212 KB |
Output is correct |
38 |
Correct |
15 ms |
5148 KB |
Output is correct |
39 |
Correct |
39 ms |
35728 KB |
Output is correct |
40 |
Correct |
14 ms |
5468 KB |
Output is correct |
41 |
Correct |
17 ms |
5504 KB |
Output is correct |
42 |
Correct |
38 ms |
35232 KB |
Output is correct |
43 |
Correct |
12 ms |
4440 KB |
Output is correct |
44 |
Correct |
0 ms |
348 KB |
Output is correct |
45 |
Correct |
0 ms |
464 KB |
Output is correct |
46 |
Correct |
1 ms |
348 KB |
Output is correct |
47 |
Correct |
0 ms |
348 KB |
Output is correct |
48 |
Correct |
1 ms |
348 KB |
Output is correct |
49 |
Correct |
0 ms |
456 KB |
Output is correct |
50 |
Correct |
0 ms |
348 KB |
Output is correct |
51 |
Correct |
1 ms |
348 KB |
Output is correct |
52 |
Correct |
0 ms |
348 KB |
Output is correct |
53 |
Correct |
0 ms |
348 KB |
Output is correct |
54 |
Correct |
0 ms |
348 KB |
Output is correct |
55 |
Correct |
9 ms |
2648 KB |
Output is correct |
56 |
Correct |
1 ms |
344 KB |
Output is correct |
57 |
Correct |
2 ms |
600 KB |
Output is correct |
58 |
Correct |
4 ms |
1368 KB |
Output is correct |
59 |
Correct |
20 ms |
5464 KB |
Output is correct |
60 |
Correct |
18 ms |
5504 KB |
Output is correct |
61 |
Correct |
20 ms |
6908 KB |
Output is correct |
62 |
Correct |
19 ms |
7256 KB |
Output is correct |
63 |
Runtime error |
458 ms |
1048576 KB |
Execution killed with signal 9 |
64 |
Halted |
0 ms |
0 KB |
- |