#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define int long long
#define all(x) x.begin() , x.end()
#define ld long double
const long long inf = 1e18 + 1LL;
const int mod = 1e9 + 7LL;
void solve();
void ITO();
int32_t main() {
	ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ITO();
	int t = 1;
	//cin >> t;
	while (t--) solve();
	return 0;
}
const int N = (1e6 + 5);
void solve()
{
	int n; cin >> n;
	int arr[n]; for (auto &x : arr) cin >> x;
	ll res = 0;
	stack<int>s;
	int ind[n]; ind[0] = 0 ; s.push(0);
	for (int i = 1; i < n ; ++i)
	{
		while (!s.empty() and arr[s.top()] >= arr[i]) s.pop();
		ind[i] = (s.empty() ? 0 : s.top() + 1);
		s.push(i);
	}
	while (!s.empty()) s.pop();
	for (int i = n - 1 ; i >= 0 ; --i)
	{
		while (!s.empty() and arr[s.top()] >= arr[i]) s.pop();
		int j = (s.empty() ? n - 1 : s.top() - 1);
		s.push(i);
		res = max(res , (j - ind[i] + 1) * arr[i]);
	}
	cout << res;
	cout << "\n";
	return ;
}
void ITO()
{
#ifndef ONLINE_JUDGE
	freopen("inputf.in", "r", stdin);
	freopen("outputf1.in", "w", stdout);
#endif
}
//                                                                  🙃
#include <bits/stdc++.h>
#define ll long long
#define MODV 1000000007 // 998244353
#define INFLL (1LL<<62)
#define EPS 1e-9
#define rep(i, n) for(ll i=0, i##_len=(ll)(n); i<i##_len; i++)
#define repf(i, n) for(ll i=1, i##_len=(ll)(n+1); i<i##_len; i++)
#define per(i, n) for(ll i=((ll)(n))-1; i>=0; i--)
#define all(v) v.begin(), v.end()
#define endl "\n"
#define vi vector<ll>
#define vvi vector<vector<ll>>
#define Yes() cout << "Yes" << endl
#define YES() cout << "YES" << endl
#define No() cout << "No" << endl
#define NO() cout << "NO" << endl
__attribute__((constructor))
void init(){ std::ios::sync_with_stdio(0); std::cin.tie(0); std::cout<<std::fixed<<std::setprecision(15); }
template<class T>bool chmax(T &a, const T &b){ if(a<b){ a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b){ if(b<a){ a=b; return 1; } return 0; }
using namespace std;

int main(){
    ll n, m;
    cin >> n >> m;
    vi h(n), w(m);
    rep(i, n) cin >> h[i];
    rep(i, m) cin >> w[i];
    sort(all(h));
    sort(all(w));

    // 先頭を含むもの、末尾を含むものの2ペア
    vi sum_s, sum_e;
    rep(i, (n-1)/2){
        sum_s.emplace_back(abs(h[2*i]-h[2*i+1]));
        sum_e.emplace_back(abs(h[2*i+1]-h[2*i+2]));
    }

    // 累積和
    vi imos_s((n-1)/2+1), imos_e((n-1)/2+1);
    imos_s[0] = 0, imos_e[0] = 0;
    rep(i, (n-1)/2){
        imos_s[i+1] = imos_s[i] + sum_s[i];
        imos_e[i+1] = imos_e[i] + sum_e[i];
    }
    imos_s.emplace_back(0);
    imos_e.emplace_back(0);

    // 取り除く対象（i番目）をを全探索
    ll ans = INFLL;
    rep(i, n){
        ll total = 0;
        if(i%2 == 0){
            total = imos_s[(i/2)] + imos_e[(n-1)/2] - imos_e[(i/2)];
            auto it = lower_bound(all(w), h[i]);
            ll num = abs(h[i]-(*it));
            if(it != w.begin()){
                it--;
                chmin(num, abs(h[i]-(*it)));
            }
            chmin(ans, total+num);
        }
        else{
            total += imos_s[(i/2)] + + imos_e[(n-1)/2] - imos_e[(ll)(ceil(i/2.0))] + abs(h[i-1] - h[i+1]);
            auto it = lower_bound(all(w), h[i]);
            ll num = abs(h[i]-(*it));
            if(it != w.begin()){
                it--;
                chmin(num, abs(h[i]-(*it)));
            }
            chmin(ans, total+num);
        }
    }
    cout << ans << endl;
}