#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);
}
kx.pb(n-1);
// mn=1e9+1;
// for(int i=n-1;i>=0;i--){
// if(umin(mn,a[i])) kx.pb(i);
// }
// mn=1e9+1;
ky.pb(m-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());
int i=0,j=0;
ll ans=0;
while(i+1<sz(kx)&&j+1<sz(ky)){
ll cst1=1ll*(kx[i+1]-kx[i])*b[ky[j]]+1ll*(ky[j+1]-ky[j])*(a[kx[i+1]]);
ll cst2=1ll*(kx[i+1]-kx[i])*b[ky[j+1]]+1ll*(ky[j+1]-ky[j])*(a[kx[i]]);
if(cst1<=cst2) ans+=1ll*(kx[i+1]-kx[i])*b[ky[j]],++i;
else ans+=1ll*(ky[j+1]-ky[j])*(a[kx[i]]),++j;
}
if(j<sz(ky)-1) ans+=1ll*(ky[j+1]-ky[j])*(a[kx[i]]);
if(i<sz(kx)-1) ans+=1ll*(kx[i+1]-kx[i])*b[ky[j]];
//// 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<<ans;
return 0;
}
/*
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |