#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[1] = A[1]; for(int i=2; i<=N; i++) PA[i] = min(PA[i-1], A[i]);
PA[N] = A[N]; for(int i=N-1; i>=1; i--) SA[i] = min(SA[i+1], A[i]);
PB[1] = B[1]; for(int i=2; i<=M; i++) PB[i] = min(PB[i-1], B[i]);
PB[M] = B[M]; for(int i=M-1; 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(!(PA[i-1] >= A[i] && A[i] <= SA[i+1])) U.emplace_back(A[i], i);
for(int i=1; i<=M; i++) if(!(PB[i-1] >= B[i] && 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];
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
32216 KB |
Output is correct |
2 |
Correct |
14 ms |
32292 KB |
Output is correct |
3 |
Correct |
13 ms |
32224 KB |
Output is correct |
4 |
Correct |
13 ms |
32300 KB |
Output is correct |
5 |
Correct |
14 ms |
32284 KB |
Output is correct |
6 |
Correct |
13 ms |
32212 KB |
Output is correct |
7 |
Correct |
13 ms |
32212 KB |
Output is correct |
8 |
Correct |
14 ms |
32264 KB |
Output is correct |
9 |
Correct |
17 ms |
32340 KB |
Output is correct |
10 |
Correct |
17 ms |
32360 KB |
Output is correct |
11 |
Correct |
18 ms |
32336 KB |
Output is correct |
12 |
Correct |
18 ms |
32340 KB |
Output is correct |
13 |
Correct |
20 ms |
32340 KB |
Output is correct |
14 |
Correct |
18 ms |
32328 KB |
Output is correct |
15 |
Correct |
17 ms |
32408 KB |
Output is correct |
16 |
Correct |
19 ms |
32388 KB |
Output is correct |
17 |
Correct |
17 ms |
32340 KB |
Output is correct |
18 |
Correct |
13 ms |
32212 KB |
Output is correct |
19 |
Correct |
12 ms |
32212 KB |
Output is correct |
20 |
Correct |
13 ms |
32228 KB |
Output is correct |
21 |
Correct |
13 ms |
32236 KB |
Output is correct |
22 |
Correct |
13 ms |
32260 KB |
Output is correct |
23 |
Correct |
13 ms |
32220 KB |
Output is correct |
24 |
Correct |
13 ms |
32228 KB |
Output is correct |
25 |
Correct |
13 ms |
32276 KB |
Output is correct |
26 |
Correct |
13 ms |
32212 KB |
Output is correct |
27 |
Correct |
13 ms |
32252 KB |
Output is correct |
28 |
Correct |
13 ms |
32228 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
32212 KB |
Output is correct |
2 |
Correct |
13 ms |
32176 KB |
Output is correct |
3 |
Correct |
16 ms |
32308 KB |
Output is correct |
4 |
Runtime error |
112 ms |
74164 KB |
Execution killed with signal 11 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
32216 KB |
Output is correct |
2 |
Correct |
14 ms |
32292 KB |
Output is correct |
3 |
Correct |
13 ms |
32224 KB |
Output is correct |
4 |
Correct |
13 ms |
32300 KB |
Output is correct |
5 |
Correct |
14 ms |
32284 KB |
Output is correct |
6 |
Correct |
13 ms |
32212 KB |
Output is correct |
7 |
Correct |
13 ms |
32212 KB |
Output is correct |
8 |
Correct |
14 ms |
32264 KB |
Output is correct |
9 |
Correct |
17 ms |
32340 KB |
Output is correct |
10 |
Correct |
17 ms |
32360 KB |
Output is correct |
11 |
Correct |
18 ms |
32336 KB |
Output is correct |
12 |
Correct |
18 ms |
32340 KB |
Output is correct |
13 |
Correct |
20 ms |
32340 KB |
Output is correct |
14 |
Correct |
18 ms |
32328 KB |
Output is correct |
15 |
Correct |
17 ms |
32408 KB |
Output is correct |
16 |
Correct |
19 ms |
32388 KB |
Output is correct |
17 |
Correct |
17 ms |
32340 KB |
Output is correct |
18 |
Correct |
13 ms |
32212 KB |
Output is correct |
19 |
Correct |
12 ms |
32212 KB |
Output is correct |
20 |
Correct |
13 ms |
32228 KB |
Output is correct |
21 |
Correct |
13 ms |
32236 KB |
Output is correct |
22 |
Correct |
13 ms |
32260 KB |
Output is correct |
23 |
Correct |
13 ms |
32220 KB |
Output is correct |
24 |
Correct |
13 ms |
32228 KB |
Output is correct |
25 |
Correct |
13 ms |
32276 KB |
Output is correct |
26 |
Correct |
13 ms |
32212 KB |
Output is correct |
27 |
Correct |
13 ms |
32252 KB |
Output is correct |
28 |
Correct |
13 ms |
32228 KB |
Output is correct |
29 |
Correct |
14 ms |
32212 KB |
Output is correct |
30 |
Correct |
13 ms |
32176 KB |
Output is correct |
31 |
Correct |
16 ms |
32308 KB |
Output is correct |
32 |
Runtime error |
112 ms |
74164 KB |
Execution killed with signal 11 |
33 |
Halted |
0 ms |
0 KB |
- |