#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll N, M, A[101010], B[101010], D[2020][2020];
ll PA[101010], SA[101010], PB[101010], SB[101010];
int main(){
ios_base::sync_with_stdio(false); cin.tie(nullptr);
cin >> N >> M;
for(int i=1; i<=N; i++) cin >> A[i];
for(int i=1; i<=M; i++) cin >> B[i];
PA[0] = PB[0] = SA[N+1] = SB[M+1] = 0x3f3f3f3f3f3f3f3f;
for(int i=1; i<=N; i++) PA[i] = min(PA[i-1], A[i]);
for(int i=N; i>=1; i--) SA[i] = min(SA[i+1], A[i]);
for(int i=1; i<=M; i++) PB[i] = min(PB[i-1], B[i]);
for(int i=M; i>=1; i--) SB[i] = min(SB[i+1], B[i]);
vector<pair<ll, ll>> U, V;
for(int i=1; i<=N; i++) if(A[i] < PA[i-1] || A[i] < SA[i+1]) U.emplace_back(A[i], i);
for(int i=1; i<=M; i++) if(B[i] < PB[i-1] || B[i] < SB[i+1]) V.emplace_back(B[i], i);
N = U.size(); M = V.size();
memset(D, 0x3f, sizeof D); D[0][0] = 0;
for(int i=0; i<N; i++){
for(int j=0; j<M; j++){
if(i + j == 0) continue;
if(i > 0) D[i][j] = min(D[i][j], D[i-1][j] + V[j].first * (U[i].second - U[i-1].second));
if(j > 0) D[i][j] = min(D[i][j], D[i][j-1] + U[i].first * (V[j].second - V[j-1].second));
}
}
cout << D[N-1][M-1];
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
32212 KB |
Output is correct |
2 |
Correct |
12 ms |
32204 KB |
Output is correct |
3 |
Correct |
13 ms |
32212 KB |
Output is correct |
4 |
Correct |
12 ms |
32212 KB |
Output is correct |
5 |
Correct |
12 ms |
32208 KB |
Output is correct |
6 |
Correct |
13 ms |
32224 KB |
Output is correct |
7 |
Correct |
13 ms |
32220 KB |
Output is correct |
8 |
Correct |
11 ms |
32212 KB |
Output is correct |
9 |
Correct |
13 ms |
32340 KB |
Output is correct |
10 |
Correct |
13 ms |
32256 KB |
Output is correct |
11 |
Correct |
14 ms |
32340 KB |
Output is correct |
12 |
Correct |
13 ms |
32256 KB |
Output is correct |
13 |
Correct |
18 ms |
32356 KB |
Output is correct |
14 |
Correct |
13 ms |
32340 KB |
Output is correct |
15 |
Correct |
13 ms |
32340 KB |
Output is correct |
16 |
Correct |
17 ms |
32384 KB |
Output is correct |
17 |
Correct |
13 ms |
32340 KB |
Output is correct |
18 |
Correct |
12 ms |
32212 KB |
Output is correct |
19 |
Correct |
14 ms |
32212 KB |
Output is correct |
20 |
Correct |
14 ms |
32272 KB |
Output is correct |
21 |
Correct |
12 ms |
32208 KB |
Output is correct |
22 |
Correct |
12 ms |
32212 KB |
Output is correct |
23 |
Correct |
13 ms |
32212 KB |
Output is correct |
24 |
Correct |
12 ms |
32212 KB |
Output is correct |
25 |
Correct |
12 ms |
32220 KB |
Output is correct |
26 |
Correct |
12 ms |
32184 KB |
Output is correct |
27 |
Correct |
13 ms |
32204 KB |
Output is correct |
28 |
Correct |
14 ms |
32212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
32212 KB |
Output is correct |
2 |
Correct |
13 ms |
32196 KB |
Output is correct |
3 |
Correct |
14 ms |
32340 KB |
Output is correct |
4 |
Correct |
17 ms |
34152 KB |
Output is correct |
5 |
Correct |
19 ms |
34320 KB |
Output is correct |
6 |
Correct |
15 ms |
33108 KB |
Output is correct |
7 |
Correct |
27 ms |
37636 KB |
Output is correct |
8 |
Correct |
27 ms |
37684 KB |
Output is correct |
9 |
Correct |
26 ms |
37756 KB |
Output is correct |
10 |
Correct |
30 ms |
37644 KB |
Output is correct |
11 |
Correct |
41 ms |
37748 KB |
Output is correct |
12 |
Correct |
31 ms |
37672 KB |
Output is correct |
13 |
Correct |
31 ms |
37672 KB |
Output is correct |
14 |
Correct |
42 ms |
37728 KB |
Output is correct |
15 |
Correct |
29 ms |
37904 KB |
Output is correct |
16 |
Correct |
13 ms |
32200 KB |
Output is correct |
17 |
Correct |
12 ms |
32228 KB |
Output is correct |
18 |
Correct |
13 ms |
32204 KB |
Output is correct |
19 |
Correct |
13 ms |
32176 KB |
Output is correct |
20 |
Correct |
13 ms |
32212 KB |
Output is correct |
21 |
Correct |
14 ms |
32188 KB |
Output is correct |
22 |
Correct |
12 ms |
32212 KB |
Output is correct |
23 |
Correct |
12 ms |
32244 KB |
Output is correct |
24 |
Correct |
14 ms |
32280 KB |
Output is correct |
25 |
Correct |
13 ms |
32208 KB |
Output is correct |
26 |
Correct |
14 ms |
32212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
32212 KB |
Output is correct |
2 |
Correct |
12 ms |
32204 KB |
Output is correct |
3 |
Correct |
13 ms |
32212 KB |
Output is correct |
4 |
Correct |
12 ms |
32212 KB |
Output is correct |
5 |
Correct |
12 ms |
32208 KB |
Output is correct |
6 |
Correct |
13 ms |
32224 KB |
Output is correct |
7 |
Correct |
13 ms |
32220 KB |
Output is correct |
8 |
Correct |
11 ms |
32212 KB |
Output is correct |
9 |
Correct |
13 ms |
32340 KB |
Output is correct |
10 |
Correct |
13 ms |
32256 KB |
Output is correct |
11 |
Correct |
14 ms |
32340 KB |
Output is correct |
12 |
Correct |
13 ms |
32256 KB |
Output is correct |
13 |
Correct |
18 ms |
32356 KB |
Output is correct |
14 |
Correct |
13 ms |
32340 KB |
Output is correct |
15 |
Correct |
13 ms |
32340 KB |
Output is correct |
16 |
Correct |
17 ms |
32384 KB |
Output is correct |
17 |
Correct |
13 ms |
32340 KB |
Output is correct |
18 |
Correct |
12 ms |
32212 KB |
Output is correct |
19 |
Correct |
14 ms |
32212 KB |
Output is correct |
20 |
Correct |
14 ms |
32272 KB |
Output is correct |
21 |
Correct |
12 ms |
32208 KB |
Output is correct |
22 |
Correct |
12 ms |
32212 KB |
Output is correct |
23 |
Correct |
13 ms |
32212 KB |
Output is correct |
24 |
Correct |
12 ms |
32212 KB |
Output is correct |
25 |
Correct |
12 ms |
32220 KB |
Output is correct |
26 |
Correct |
12 ms |
32184 KB |
Output is correct |
27 |
Correct |
13 ms |
32204 KB |
Output is correct |
28 |
Correct |
14 ms |
32212 KB |
Output is correct |
29 |
Correct |
13 ms |
32212 KB |
Output is correct |
30 |
Correct |
13 ms |
32196 KB |
Output is correct |
31 |
Correct |
14 ms |
32340 KB |
Output is correct |
32 |
Correct |
17 ms |
34152 KB |
Output is correct |
33 |
Correct |
19 ms |
34320 KB |
Output is correct |
34 |
Correct |
15 ms |
33108 KB |
Output is correct |
35 |
Correct |
27 ms |
37636 KB |
Output is correct |
36 |
Correct |
27 ms |
37684 KB |
Output is correct |
37 |
Correct |
26 ms |
37756 KB |
Output is correct |
38 |
Correct |
30 ms |
37644 KB |
Output is correct |
39 |
Correct |
41 ms |
37748 KB |
Output is correct |
40 |
Correct |
31 ms |
37672 KB |
Output is correct |
41 |
Correct |
31 ms |
37672 KB |
Output is correct |
42 |
Correct |
42 ms |
37728 KB |
Output is correct |
43 |
Correct |
29 ms |
37904 KB |
Output is correct |
44 |
Correct |
13 ms |
32200 KB |
Output is correct |
45 |
Correct |
12 ms |
32228 KB |
Output is correct |
46 |
Correct |
13 ms |
32204 KB |
Output is correct |
47 |
Correct |
13 ms |
32176 KB |
Output is correct |
48 |
Correct |
13 ms |
32212 KB |
Output is correct |
49 |
Correct |
14 ms |
32188 KB |
Output is correct |
50 |
Correct |
12 ms |
32212 KB |
Output is correct |
51 |
Correct |
12 ms |
32244 KB |
Output is correct |
52 |
Correct |
14 ms |
32280 KB |
Output is correct |
53 |
Correct |
13 ms |
32208 KB |
Output is correct |
54 |
Correct |
14 ms |
32212 KB |
Output is correct |
55 |
Correct |
22 ms |
35212 KB |
Output is correct |
56 |
Correct |
13 ms |
32236 KB |
Output is correct |
57 |
Correct |
13 ms |
32488 KB |
Output is correct |
58 |
Correct |
17 ms |
33092 KB |
Output is correct |
59 |
Correct |
37 ms |
38880 KB |
Output is correct |
60 |
Correct |
33 ms |
38868 KB |
Output is correct |
61 |
Correct |
34 ms |
38856 KB |
Output is correct |
62 |
Correct |
35 ms |
38872 KB |
Output is correct |
63 |
Runtime error |
841 ms |
83156 KB |
Execution killed with signal 11 |
64 |
Halted |
0 ms |
0 KB |
- |