#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const ll INF = 1e18;
int a[100100], b[100100];
ll dp[1010][1010];
ll solve(vector<pair<int, int>> &a, vector<pair<int, int>> &b){
/*printf("A = ");
for (auto &[x, y]:a) printf("(%d, %d) ", x, y);
printf("\n");
printf("B = ");
for (auto &[x, y]:b) printf("(%d, %d) ", x, y);
printf("\n\n");*/
int n = a.size(), m = b.size();
--n, --m;
for (int i=0;i<=n+1;i++) fill(dp[i], dp[i]+m+2, INF);
dp[1][1] = 0;
for (int i=1;i<=n;i++){
for (int j=1;j<=m;j++){
dp[i][j] = min(dp[i][j], min(dp[i-1][j] + (ll)b[j].first * a[i].second, dp[i][j-1] + (ll)a[i].first * b[j].second));
}
}
return dp[n][m];
}
vector<pair<int, int>> get_v(int s, int e, int a[]){
int t = 1, mn = 1e9, cnt = -1;
vector<pair<int, int>> ret = {{0, 0}};
if (s>e) t = -1;
for (int i=s;;i+=t){
cnt++;
if (i==e) {ret.emplace_back(a[i], cnt); break;}
if (mn<=a[i]) continue;
mn = a[i];
ret.emplace_back(a[i], cnt);
cnt = 0;
}
return ret;
}
int main(){
int n, m;
scanf("%d %d", &n, &m);
for (int i=1;i<=n;i++) scanf("%d", a+i);
for (int i=1;i<=m;i++) scanf("%d", b+i);
int idxa = min_element(a+1, a+n+1) - a, idxb = min_element(b+1, b+m+1) - b;
vector<pair<int, int>> A1, A2, B1, B2;
A1 = get_v(1, idxa, a);
A2 = get_v(n, idxa, a);
B1 = get_v(1, idxb, b);
B2 = get_v(m, idxb, b);
printf("%lld\n", solve(A1, B1) + solve(A2, B2));
return 0;
}
Compilation message
kyoto.cpp: In function 'int main()':
kyoto.cpp:49:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
49 | scanf("%d %d", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~~
kyoto.cpp:50:33: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
50 | for (int i=1;i<=n;i++) scanf("%d", a+i);
| ~~~~~^~~~~~~~~~~
kyoto.cpp:51:33: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
51 | for (int i=1;i<=m;i++) scanf("%d", b+i);
| ~~~~~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Incorrect |
4 ms |
4436 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
2 ms |
2132 KB |
Output is correct |
4 |
Correct |
8 ms |
1924 KB |
Output is correct |
5 |
Correct |
9 ms |
1596 KB |
Output is correct |
6 |
Correct |
3 ms |
596 KB |
Output is correct |
7 |
Correct |
23 ms |
1856 KB |
Output is correct |
8 |
Correct |
17 ms |
1876 KB |
Output is correct |
9 |
Correct |
18 ms |
3260 KB |
Output is correct |
10 |
Correct |
18 ms |
2996 KB |
Output is correct |
11 |
Correct |
29 ms |
9732 KB |
Output is correct |
12 |
Correct |
16 ms |
3172 KB |
Output is correct |
13 |
Correct |
17 ms |
3028 KB |
Output is correct |
14 |
Correct |
29 ms |
9676 KB |
Output is correct |
15 |
Correct |
16 ms |
1976 KB |
Output is correct |
16 |
Correct |
1 ms |
304 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
312 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
308 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Incorrect |
4 ms |
4436 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |