#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <string>
using namespace std;
#define ll long long
#define pii pair<int, int>
const int A = 1002, N = 1e5 + 2;
const ll inf = 1e18 + 49;
int h, w;
int a[N], b[N];
int fto[A], lto[A];
int fsu[A], lsu[A];
vector<int> toxs, syuns;
ll dp[2 * A][2 * A];
int main() {
cin >> h >> w;
for (int i = 1; i <= h; i++) {
cin >> a[i];
}
for (int i = 1; i <= w; i++) {
cin >> b[i];
}
if (h <= 1000 && w <= 1000) {
for (int i = 1; i <= h; i++) {
for (int j = 1; j <= w; j++) {
dp[i][j] = inf;
dp[1][1] = 0;
if (i > 1)
dp[i][j] = min(dp[i][j], dp[i - 1][j] + b[j]);
if (j > 1)
dp[i][j] = min(dp[i][j], dp[i][j - 1] + a[i]);
}
}
cout << dp[h][w] << endl;
}
else {
for (int i = 1; i <= h; i++) {
if (!fto[a[i]])
fto[a[i]] = i;
}
for (int i = 1; i <= w; i++) {
if (!fsu[b[i]])
fsu[b[i]] = i;
}
for (int i = h; i >= 1; i--) {
if (!lto[a[i]])
lto[a[i]] = i;
}
for (int i = w; i >= 1; i--) {
if (!lsu[b[i]])
lsu[b[i]] = i;
}
for (int i = 1; i <= h; i++) {
if (fto[a[i]] == i)
toxs.push_back(i);
else if (lto[a[i]] == i)
toxs.push_back(i);
}
for (int i = 1; i <= w; i++) {
if (fsu[b[i]] == i)
syuns.push_back(i);
else if (lsu[b[i]] == i)
syuns.push_back(i);
}
int n = toxs.size(), m = syuns.size();
dp[0][0] = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
dp[i][j] = inf;
dp[0][0] = 0;
if (i) {
dp[i][j] = min(dp[i][j], dp[i - 1][j] + b[syuns[j]] * (toxs[i] - toxs[i - 1]));
}
if (j) {
dp[i][j] = min(dp[i][j], dp[i][j - 1] + a[toxs[i]] * (syuns[j] - syuns[j - 1]));
}
}
}
cout << dp[n - 1][m - 1] << endl;
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 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 |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
2232 KB |
Output is correct |
6 |
Correct |
1 ms |
1588 KB |
Output is correct |
7 |
Correct |
1 ms |
1108 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
9 ms |
12116 KB |
Output is correct |
10 |
Correct |
10 ms |
12116 KB |
Output is correct |
11 |
Correct |
10 ms |
12116 KB |
Output is correct |
12 |
Correct |
12 ms |
12104 KB |
Output is correct |
13 |
Correct |
10 ms |
12104 KB |
Output is correct |
14 |
Correct |
10 ms |
12116 KB |
Output is correct |
15 |
Correct |
9 ms |
12116 KB |
Output is correct |
16 |
Correct |
9 ms |
12112 KB |
Output is correct |
17 |
Correct |
9 ms |
12096 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
308 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
0 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
0 ms |
340 KB |
Output is correct |
27 |
Correct |
0 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
308 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
3 ms |
3924 KB |
Output is correct |
4 |
Correct |
47 ms |
32332 KB |
Output is correct |
5 |
Correct |
16 ms |
2788 KB |
Output is correct |
6 |
Correct |
12 ms |
8716 KB |
Output is correct |
7 |
Correct |
72 ms |
33256 KB |
Output is correct |
8 |
Correct |
65 ms |
33168 KB |
Output is correct |
9 |
Correct |
68 ms |
33172 KB |
Output is correct |
10 |
Correct |
68 ms |
33172 KB |
Output is correct |
11 |
Correct |
65 ms |
33076 KB |
Output is correct |
12 |
Correct |
68 ms |
32960 KB |
Output is correct |
13 |
Correct |
71 ms |
33100 KB |
Output is correct |
14 |
Correct |
67 ms |
33184 KB |
Output is correct |
15 |
Correct |
48 ms |
2040 KB |
Output is correct |
16 |
Correct |
0 ms |
312 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
312 KB |
Output is correct |
20 |
Correct |
0 ms |
316 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
0 ms |
340 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 |
1 ms |
212 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 |
980 KB |
Output is correct |
5 |
Correct |
1 ms |
2232 KB |
Output is correct |
6 |
Correct |
1 ms |
1588 KB |
Output is correct |
7 |
Correct |
1 ms |
1108 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
9 ms |
12116 KB |
Output is correct |
10 |
Correct |
10 ms |
12116 KB |
Output is correct |
11 |
Correct |
10 ms |
12116 KB |
Output is correct |
12 |
Correct |
12 ms |
12104 KB |
Output is correct |
13 |
Correct |
10 ms |
12104 KB |
Output is correct |
14 |
Correct |
10 ms |
12116 KB |
Output is correct |
15 |
Correct |
9 ms |
12116 KB |
Output is correct |
16 |
Correct |
9 ms |
12112 KB |
Output is correct |
17 |
Correct |
9 ms |
12096 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
308 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
0 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
0 ms |
340 KB |
Output is correct |
27 |
Correct |
0 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
308 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
3 ms |
3924 KB |
Output is correct |
32 |
Correct |
47 ms |
32332 KB |
Output is correct |
33 |
Correct |
16 ms |
2788 KB |
Output is correct |
34 |
Correct |
12 ms |
8716 KB |
Output is correct |
35 |
Correct |
72 ms |
33256 KB |
Output is correct |
36 |
Correct |
65 ms |
33168 KB |
Output is correct |
37 |
Correct |
68 ms |
33172 KB |
Output is correct |
38 |
Correct |
68 ms |
33172 KB |
Output is correct |
39 |
Correct |
65 ms |
33076 KB |
Output is correct |
40 |
Correct |
68 ms |
32960 KB |
Output is correct |
41 |
Correct |
71 ms |
33100 KB |
Output is correct |
42 |
Correct |
67 ms |
33184 KB |
Output is correct |
43 |
Correct |
48 ms |
2040 KB |
Output is correct |
44 |
Correct |
0 ms |
312 KB |
Output is correct |
45 |
Correct |
1 ms |
212 KB |
Output is correct |
46 |
Correct |
1 ms |
212 KB |
Output is correct |
47 |
Correct |
0 ms |
312 KB |
Output is correct |
48 |
Correct |
0 ms |
316 KB |
Output is correct |
49 |
Correct |
1 ms |
340 KB |
Output is correct |
50 |
Correct |
1 ms |
340 KB |
Output is correct |
51 |
Correct |
1 ms |
340 KB |
Output is correct |
52 |
Correct |
0 ms |
340 KB |
Output is correct |
53 |
Correct |
1 ms |
340 KB |
Output is correct |
54 |
Correct |
1 ms |
340 KB |
Output is correct |
55 |
Runtime error |
33 ms |
1876 KB |
Execution killed with signal 11 |
56 |
Halted |
0 ms |
0 KB |
- |