#include "bits/stdc++.h"
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...)
#endif
using ll = long long;
using pii = pair<int, int>;
#define F first
#define S second
#define sz(x) (int)((x).size())
#define all(x) (x).begin(), (x).end()
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll get_rand(ll l, ll r) {
    assert(l <= r);
    return uniform_int_distribution<ll> (l, r)(rng);
}
using Point = pair<ll, ll>;
#define X first
#define Y second
Point operator - (const Point &A, const Point &B){
    return make_pair(A.X - B.X, A.Y - B.Y);
}
ll cross(Point A, Point B){
    return A.X * B.Y - A.Y * B.X;
}
ll cross(Point &A, Point &B, Point &C){
    return cross(B - A, C - A);
}
vector<Point> lower_hull(vector<Point> &a){
    vector<Point> lower;
    for(int i = 0; i < sz(a); ++i){
        while(sz(lower) > 1 and cross(lower[sz(lower) - 2], lower.back(), a[i]) <= 0)
            lower.pop_back();
        lower.push_back(a[i]);
    }
    return lower;
}
int n, m;
vector<Point> a, b;
void solve(){
    cin >> n >> m;
    for(int i = 1; i <= n; ++i){
        ll x;
        cin >> x;
        a.push_back(make_pair(i, x));
    }
    for(int i = 1; i <= m; ++i){
        ll x;
        cin >> x;
        b.push_back(make_pair(i, x));
    }
    vector<Point> ha = lower_hull(a), hb = lower_hull(b);
    debug(ha, hb);
    int i = 0, j = 0;
    ll res = 0;
    while(i + 1 < sz(ha) and j + 1 < sz(hb)){
        if(cross(ha[i + 1] - ha[i], hb[j + 1] - hb[j]) < 0){
            res += (hb[j + 1].X - hb[j].X) * ha[i].Y;
            ++j;
        }
        else{
            res += (ha[i + 1].X - ha[i].X) * hb[j].Y;
            ++i;
        }
    }
    while(i + 1 < sz(ha)){
        res += (ha[i + 1].X - ha[i].X) * hb[j].Y;
        ++i;
    }
    while(j + 1 < sz(hb)){
        res += (hb[j + 1].X - hb[j].X) * ha[i].Y;
        ++j;
    }
    cout << res;
}
int32_t main() {
    cin.tie(nullptr)->sync_with_stdio(0);
    #define task "troll"
    if(fopen(task".inp", "r")){
        freopen(task".inp", "r", stdin);
        freopen(task".out", "w", stdout);
    }
    int test = 1;
//    cin >> test;
    for(int i = 1; i <= test; ++i){
//        cout << "Case #" << i << ": ";
        solve();
    }
    #ifdef LOCAL
        cerr << "\n[Time]: " << 1000.0 * clock() / CLOCKS_PER_SEC << " ms.\n";
    #endif
    return 0;
}
Compilation message (stderr)
kyoto.cpp: In function 'int32_t main()':
kyoto.cpp:97:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   97 |         freopen(task".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
kyoto.cpp:98:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   98 |         freopen(task".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |