#include <bits/stdc++.h>
#define f first
#define s second
#define m_p make_pair
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define vec vector
#define pb push_back
#define sz(x) (int)(x).size()
#define pw(x) (1LL<<(x))
#define fast_resp ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef long double ld;
typedef pair<int,ll> pil;
template<class T> bool umin(T &a,const T &b){return (a>b?a=b,1:0);}
template<class T> bool umax(T &a,const T &b){return (a<b?a=b,1:0);}
const int M=1e9+7;
//const int N=5e5+1;
void add(int &a,int b){
a+=b;
if(a>=M) a-=M;
else if(a<0) a+=M;
}
int mult(int a,int b){
return 1ll*a*b%M;
}
//int dp[N][26];
const int N=4e3+1;
ll dp[N][N];
signed main(){
fast_resp;
int n,m;
cin>>n>>m;
vec<int>a(n),b(m);
// int i=0,j=0;
for(auto &z : a) cin>>z;
for(auto &z : b) cin>>z;
int mn=1e9+1;
vec<int> p1(n),p2(m);
vec<int> kx,ky;
for(int i=0;i<n;i++){
if(umin(mn,a[i])) kx.pb(i);
}
mn=1e9+1;
for(int i=n-1;i>=0;i--){
if(umin(mn,a[i])) kx.pb(i);
}
mn=1e9+1;
for(int i=m-1;i>=0;i--){
if(umin(mn,b[i])) ky.pb(i);
}
mn=1e9+1;
for(int i=0;i<m;i++){
if(umin(mn,b[i])) ky.pb(i);
}
sort(all(kx));kx.erase(unique(all(kx)),kx.end());
sort(all(ky));ky.erase(unique(all(ky)),ky.end());
// cout<<endl;
// cout<<0<<' ';
for(int i=0;i<sz(kx);i++){
for(int j=0;j<sz(ky);j++){
if(!i && !j) continue;
dp[i][j]=1e18;
if(i) umin(dp[i][j],dp[i-1][j]+1ll*(kx[i]-kx[i-1])*b[ky[j]]);
if(j) umin(dp[i][j],dp[i][j-1]+1ll*(ky[j]-ky[j-1])*a[kx[i]]);
// cout<<dp[i][j]-((i&&j)?dp[i-1][j-1]:0)<<' ';
}
// cout<<endl;
}
cout<<dp[sz(kx)-1][sz(ky)-1];
return 0;
}
/*
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
212 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 |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
9 ms |
12116 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
596 KB |
Output is correct |
16 |
Correct |
10 ms |
12136 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 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 |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
328 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
3 ms |
3924 KB |
Output is correct |
4 |
Correct |
7 ms |
2516 KB |
Output is correct |
5 |
Correct |
9 ms |
3284 KB |
Output is correct |
6 |
Correct |
4 ms |
596 KB |
Output is correct |
7 |
Correct |
14 ms |
1892 KB |
Output is correct |
8 |
Correct |
16 ms |
1876 KB |
Output is correct |
9 |
Correct |
16 ms |
4196 KB |
Output is correct |
10 |
Correct |
15 ms |
4204 KB |
Output is correct |
11 |
Correct |
38 ms |
41188 KB |
Output is correct |
12 |
Correct |
15 ms |
4584 KB |
Output is correct |
13 |
Correct |
16 ms |
4692 KB |
Output is correct |
14 |
Correct |
45 ms |
40540 KB |
Output is correct |
15 |
Correct |
15 ms |
1900 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
0 ms |
340 KB |
Output is correct |
23 |
Correct |
0 ms |
340 KB |
Output is correct |
24 |
Correct |
0 ms |
340 KB |
Output is correct |
25 |
Correct |
0 ms |
340 KB |
Output is correct |
26 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
212 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 |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
9 ms |
12116 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
1 ms |
596 KB |
Output is correct |
16 |
Correct |
10 ms |
12136 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 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 |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
328 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
0 ms |
340 KB |
Output is correct |
31 |
Correct |
3 ms |
3924 KB |
Output is correct |
32 |
Correct |
7 ms |
2516 KB |
Output is correct |
33 |
Correct |
9 ms |
3284 KB |
Output is correct |
34 |
Correct |
4 ms |
596 KB |
Output is correct |
35 |
Correct |
14 ms |
1892 KB |
Output is correct |
36 |
Correct |
16 ms |
1876 KB |
Output is correct |
37 |
Correct |
16 ms |
4196 KB |
Output is correct |
38 |
Correct |
15 ms |
4204 KB |
Output is correct |
39 |
Correct |
38 ms |
41188 KB |
Output is correct |
40 |
Correct |
15 ms |
4584 KB |
Output is correct |
41 |
Correct |
16 ms |
4692 KB |
Output is correct |
42 |
Correct |
45 ms |
40540 KB |
Output is correct |
43 |
Correct |
15 ms |
1900 KB |
Output is correct |
44 |
Correct |
0 ms |
212 KB |
Output is correct |
45 |
Correct |
0 ms |
212 KB |
Output is correct |
46 |
Correct |
0 ms |
212 KB |
Output is correct |
47 |
Correct |
0 ms |
340 KB |
Output is correct |
48 |
Correct |
0 ms |
212 KB |
Output is correct |
49 |
Correct |
0 ms |
340 KB |
Output is correct |
50 |
Correct |
0 ms |
340 KB |
Output is correct |
51 |
Correct |
0 ms |
340 KB |
Output is correct |
52 |
Correct |
0 ms |
340 KB |
Output is correct |
53 |
Correct |
0 ms |
340 KB |
Output is correct |
54 |
Correct |
0 ms |
340 KB |
Output is correct |
55 |
Correct |
10 ms |
2004 KB |
Output is correct |
56 |
Correct |
1 ms |
336 KB |
Output is correct |
57 |
Correct |
2 ms |
980 KB |
Output is correct |
58 |
Correct |
5 ms |
1824 KB |
Output is correct |
59 |
Correct |
25 ms |
3924 KB |
Output is correct |
60 |
Correct |
21 ms |
2280 KB |
Output is correct |
61 |
Correct |
28 ms |
5488 KB |
Output is correct |
62 |
Correct |
22 ms |
5068 KB |
Output is correct |
63 |
Runtime error |
1388 ms |
260748 KB |
Execution killed with signal 11 |
64 |
Halted |
0 ms |
0 KB |
- |