#include <bits/stdc++.h>
#define MAX 500005
#define MOD (ll)(1e9+7)
#define INF (ll)(1e18)
#define inf (1987654321)
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
int n, m, q;
int a[100005], b[100005];
ll ans1[100005], ans2[100005];
int prea[100005], preb[100005];
int posta[100005], postb[100005];
deque<pii> graph;
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m;
for(int i = 1; i <= n; ++i) cin >> a[i];
for(int i = 1; i <= m; ++i) cin >> b[i];
prea[1] = 1, preb[1] = 1;
for(int i = 2; i <= n; ++i) prea[i] = a[prea[i - 1]] > a[i] ? i : prea[i - 1];
for(int i = 2; i <= m; ++i) preb[i] = b[preb[i - 1]] > b[i] ? i : preb[i - 1];
posta[n] = n, postb[m] = m;
for(int i = n - 1; i >= 1; --i) posta[i] = a[posta[i + 1]] > a[i] ? i : posta[i + 1];
for(int i = m - 1; i >= 1; --i) postb[i] = b[postb[i + 1]] > b[i] ? i : postb[i + 1];
auto solve1 = [&](auto&& self, int r, int c) -> void {
if(r == 0 || c == 0) return;
int i = prea[r], j = preb[c];
graph.push_front({i, j});
self(self, i - 1, j - 1);
};
auto solve2 = [&](auto&& self, int r, int c) -> void {
if(r == n + 1 || c == m + 1) return;
int i = posta[r], j = postb[c];
graph.push_back({i, j});
self(self, i + 1, j + 1);
};
int i = prea[n], j = preb[m];
graph.push_back({i, j});
solve1(solve1, i - 1, j - 1);
graph.push_front({1, 1});
for(int i = 1; i < (int)graph.size(); ++i) {
ans1[i] = INF;
for(int j = 0; j < i; ++j) {
ll r = graph[i].first - graph[j].first, c = graph[i].second - graph[j].second;
ll cost = min(ans1[j] + 1ll * a[graph[j].first] * c + 1ll * b[graph[i].second] * r,
ans1[j] + 1ll * b[graph[j].second] * r + 1ll * a[graph[i].first] * c);
ans1[i] = min(ans1[i], cost);
}
}
ll tmp = ans1[graph.size() - 1];
graph.clear();
graph.push_back({i, j});
solve2(solve2, i + 1, j + 1);
graph.push_back({n, m});
for(int i = 1; i < (int)graph.size(); ++i) {
ans2[i] = INF;
for(int j = 0; j < i; ++j) {
ll r = graph[i].first - graph[j].first, c = graph[i].second - graph[j].second;
ll cost = min(ans2[j] + 1ll * a[graph[j].first] * c + 1ll * b[graph[i].second] * r,
ans2[j] + 1ll * b[graph[j].second] * r + 1ll * a[graph[i].first] * c);
ans2[i] = min(ans2[i], cost);
}
}
cout << tmp + ans2[graph.size() - 1];
return 0;
}
# |
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 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
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 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
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 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |