#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MX=4005;
int H,W;
ll dp[MX][MX];
int main() {
cin.tie(0); ios_base::sync_with_stdio(0);
cin>>H>>W;
vector<ll> A(H),B(W);
for(int i=0;i<H;i++) cin>>A[i];
for(int i=0;i<W;i++) cin>>B[i];
vector<pair<ll,ll>> a, b;
{
vector<bool> C(H);
int mn=2e9;
for(int i=0;i<H;i++) {
if(mn>A[i]) {
mn=A[i];
C[i]=1;
while(i+1<H && A[i+1]==A[i]) i++;
C[i]=1;
}
}
mn=2e9;
for(int i=H-1;i>=0;i--) {
if(mn>A[i]) {
mn=A[i];
C[i]=1;
while(i-1>=0 && A[i-1]==A[i]) i--;
C[i]=1;
}
}
int lst=0;
for(int i=0;i<H;i++)
if(C[i]) {
a.push_back({A[i],i-lst});
lst=i;
}
}
{
vector<bool> C(W);
int mn=2e9;
for(int i=0;i<W;i++) {
if(mn>B[i]) {
mn=B[i];
C[i]=1;
while(i+1<W && B[i+1]==B[i]) i++;
C[i]=1;
}
}
mn=2e9;
for(int i=W-1;i>=0;i--) {
if(mn>B[i]) {
mn=B[i];
C[i]=1;
while(i-1>=0 && B[i-1]==B[i]) i--;
C[i]=1;
}
}
int lst=0;
for(int i=0;i<W;i++)
if(C[i]) {
b.push_back({B[i],i-lst});
lst=i;
}
}
H=a.size();
W=b.size();
assert(H<=4000 && W<=4000);
for(int i=0;i<=H;i++) {
for(int j=0;j<=W;j++) {
dp[i][j]=2e18;
}
}
dp[0][0]=0;
for(int i=0;i<H;i++) {
for(int j=0;j<W;j++) {
if(i+1<H)
dp[i+1][j]=min(dp[i+1][j],dp[i][j]+b[j].first*a[i+1].second);
if(j+1<W)
dp[i][j+1]=min(dp[i][j+1],dp[i][j]+a[i].first*b[j+1].second);
}
}
cout<<dp[H-1][W-1]<<'\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2392 KB |
Output is correct |
11 |
Correct |
1 ms |
2392 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
11 ms |
33424 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
7 ms |
33368 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
344 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
2396 KB |
Output is correct |
24 |
Correct |
0 ms |
2396 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
3 ms |
10844 KB |
Output is correct |
4 |
Correct |
6 ms |
11356 KB |
Output is correct |
5 |
Correct |
7 ms |
5468 KB |
Output is correct |
6 |
Correct |
3 ms |
2908 KB |
Output is correct |
7 |
Correct |
11 ms |
4696 KB |
Output is correct |
8 |
Correct |
11 ms |
4696 KB |
Output is correct |
9 |
Correct |
12 ms |
15192 KB |
Output is correct |
10 |
Correct |
13 ms |
15200 KB |
Output is correct |
11 |
Correct |
100 ms |
128180 KB |
Output is correct |
12 |
Correct |
13 ms |
15196 KB |
Output is correct |
13 |
Correct |
13 ms |
15196 KB |
Output is correct |
14 |
Correct |
47 ms |
97540 KB |
Output is correct |
15 |
Correct |
14 ms |
3040 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
352 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
2392 KB |
Output is correct |
22 |
Correct |
1 ms |
2400 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
352 KB |
Output is correct |
26 |
Correct |
0 ms |
356 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2392 KB |
Output is correct |
11 |
Correct |
1 ms |
2392 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
11 ms |
33424 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
7 ms |
33368 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
344 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
2396 KB |
Output is correct |
24 |
Correct |
0 ms |
2396 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
344 KB |
Output is correct |
29 |
Correct |
0 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
3 ms |
10844 KB |
Output is correct |
32 |
Correct |
6 ms |
11356 KB |
Output is correct |
33 |
Correct |
7 ms |
5468 KB |
Output is correct |
34 |
Correct |
3 ms |
2908 KB |
Output is correct |
35 |
Correct |
11 ms |
4696 KB |
Output is correct |
36 |
Correct |
11 ms |
4696 KB |
Output is correct |
37 |
Correct |
12 ms |
15192 KB |
Output is correct |
38 |
Correct |
13 ms |
15200 KB |
Output is correct |
39 |
Correct |
100 ms |
128180 KB |
Output is correct |
40 |
Correct |
13 ms |
15196 KB |
Output is correct |
41 |
Correct |
13 ms |
15196 KB |
Output is correct |
42 |
Correct |
47 ms |
97540 KB |
Output is correct |
43 |
Correct |
14 ms |
3040 KB |
Output is correct |
44 |
Correct |
1 ms |
348 KB |
Output is correct |
45 |
Correct |
0 ms |
348 KB |
Output is correct |
46 |
Correct |
0 ms |
352 KB |
Output is correct |
47 |
Correct |
0 ms |
348 KB |
Output is correct |
48 |
Correct |
0 ms |
348 KB |
Output is correct |
49 |
Correct |
1 ms |
2392 KB |
Output is correct |
50 |
Correct |
1 ms |
2400 KB |
Output is correct |
51 |
Correct |
0 ms |
348 KB |
Output is correct |
52 |
Correct |
0 ms |
348 KB |
Output is correct |
53 |
Correct |
0 ms |
352 KB |
Output is correct |
54 |
Correct |
0 ms |
356 KB |
Output is correct |
55 |
Correct |
8 ms |
4200 KB |
Output is correct |
56 |
Correct |
1 ms |
2404 KB |
Output is correct |
57 |
Correct |
2 ms |
6760 KB |
Output is correct |
58 |
Correct |
3 ms |
9156 KB |
Output is correct |
59 |
Correct |
16 ms |
5984 KB |
Output is correct |
60 |
Correct |
17 ms |
5992 KB |
Output is correct |
61 |
Correct |
17 ms |
16228 KB |
Output is correct |
62 |
Correct |
19 ms |
18280 KB |
Output is correct |
63 |
Runtime error |
27 ms |
12276 KB |
Execution killed with signal 6 |
64 |
Halted |
0 ms |
0 KB |
- |