#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 ans[3005][3005];
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);
};
solve1(solve1, n, m); graph.push_front({1, 1});
solve2(solve2, 1, 1); graph.push_back({n, m});
int k = graph.size();
for(int i = 0; i < k; ++i) for(int j = 0; j < k; ++j) {
if(i == 0) ans[i][j] = 1ll * a[1] * (graph[j].second - 1);
else if(j == 0) ans[i][j] = 1ll * b[1] * (graph[i].first - 1);
else {
ans[i][j] = INF;
ans[i][j] = min(ans[i][j], ans[i][j - 1] + 1ll * a[graph[i].first] * abs(graph[j - 1].second - graph[j].second));
ans[i][j] = min(ans[i][j], ans[i - 1][j] + 1ll * b[graph[j].second] * abs(graph[i - 1].first - graph[i].first));
}
}
cout << ans[k - 1][k - 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 |
2 ms |
2132 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 |
- |