Problem:
N: 通販 (Mail Order)
胡桃沢さんは、魔界通販で積み木のおもちゃを買った。
積み木は一辺の長さが1の立方体の形をしており、縦に $H$ 個、横に $W$ 個に分割されたマス目の上に積まれている。
真横から見ると、左から順に、$A_1, A_2, A_3, \dots, A_H$ 個のブロックが積まれて見えた。
正面から見ると、左から順に、$B_1, B_2, B_3, \dots, B_W$ 個のブロックが積まれて見えた。
胡桃沢さんは偉大なので、これらの情報だけからブロックの総数を当てようと考えた。
小さい数を答えて間違えると格好悪いので、考えられる最大の個数を答えたい。
入力
1 行目には整数 $H, W$ が空白区切りで与えられる。
2 行目には、真横から見たときの図を表す $H$ 個の整数 $A_1, A_2, A_3, \dots, A_H$ が空白区切りで与えられる。
3 行目には、正面から見たときの図を表す $W$ 個の整数 $B_1, B_2, B_3, \dots, B_W$ が空白区切りで与えられる。
出力
これらの情報から考えられるブロックの個数の最大値を出力しなさい。
制約
$H, W$ は $1$ 以上 $100 \ 000$ 以下の整数
$A_1, A_2, A_3, \dots, A_H$ は $1$ 以上 $100 \ 000 \ 000$ 以下の整数
$B_1, B_2, B_3, \dots, B_W$ は $1$ 以上 $100 \ 000 \ 000$ 以下の整数
すべての入力に対して、条件を満たすブロックの積み方があることが保証される
入力例1
2 2
1 5
1 5
出力例1
8
縦方向に $X$ 番目、横方向に $Y$ 番目のマスを $(X, Y)$ で表すことにします。
マス $(2, 2)$ に5個、マス $(1, 1)$、$(1, 2)$、$(2, 1)$ に $1$ 個ずつ積まれていると合計 $8$ 個のブロックが積まれることになります。
$9$ 個以上積まれている可能性は無いので、答えは $8$ です。
入力例2
3 3
2 4 5
3 1 5
出力例2
22

Submitted solutions:

code_snippet: #include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
using std::cout;
using std::cin;
using std::endl;
using ll=long long;
using ld=long double;
ll ILL=2167167167167167167;
const int INF=2100000000;
const ll mod=1e9+7;
#define rep(i,a) for (ll i=0;i<a;i++)
#define all(p) p.begin(),p.end()
template<class T> using _pq = priority_queue<T, vector<T>, greater<T>>;
template<class T> ll LB(vector<T> &v,T a){return lower_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> ll UB(vector<T> &v,T a){return upper_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> bool chmin(T &a,const T &b){if(a>b){a=b;return 1;}else return 0;}
template<class T> bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}else return 0;}
template<class T> void So(vector<T> &v) {sort(v.begin(),v.end());}
template<class T> void Sore(vector<T> &v) {sort(v.begin(),v.end(),[](T x,T y){return x>y;});}
void yneos(bool a){if(a) cout<<"Yes\n"; else cout<<"No\n";}
template<class T> void vec_out(vector<T> &p){for(int i=0;i<(int)(p.size());i++){if(i) cout<<" ";cout<<p[i];}cout<<"\n";}
template<class T> T min(vector<T> &a){assert(!a.empty());T ans=a[0];for(auto &x:a) chmin(ans,x);return ans;}
template<class T> T max(vector<T> &a){assert(!a.empty());T ans=a[0];for(auto &x:a) chmax(ans,x);return ans;}
template<class T> T sum(vector<T> &a){assert(!a.empty());T ans=a[0]-a[0];for(auto &x:a) ans+=x;return ans;}

void solve();
// oddloop
int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	
	int t=1;
	//cin>>t;
	rep(i,t) solve();
}

void solve(){
	ll H,W;
	cin>>H>>W;
	vector<ll> p(H+W),order(H+W);
	rep(i,H+W){
		cin>>p[i];
		order[i]=i;
	}
	sort(all(order),[&](int l,int r){
		return p[l]<p[r];
	});
	ll A=H,B=W,ans=0;
	rep(i,H+W){
		int a=order[i];
		if(a<H) ans+=B*p[a],A--;
		else ans+=A*p[a],B--;
	}
	cout<<ans<<"\n";
}
accuracy: 1
time_ms: 30
memory_kb: 6252
final_rank: 6

code_snippet: #pragma GCC optimize("Ofast")
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <cstdio>
#include <ctime>
#include <assert.h>
#include <chrono>
#include <random>
#include <numeric>
#include <set>
#include <deque>
#include <stack>
#include <sstream>
#include <utility>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
#include <tuple>
#include <array>
#include <bitset>
using namespace std;
typedef long long int ll;
typedef unsigned long long ull;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) {
    return (ull)rng() % B;
}
inline ll time() {
    return static_cast<long double>(chrono::duration_cast<chrono::nanoseconds>(chrono::steady_clock::now().time_since_epoch()).count()) * 1e-9;
}

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int h,w; 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];
    }
    sort(a.begin(), a.end());
    sort(b.begin(), b.end());
    a.push_back(2e9);
    b.push_back(2e9);
    ll res = 0;
    for(int i=0,j=0;i<h and j<w;){
        if(a[i] <= b[j]){
            res += a[i]*(w-j);
            i++;
        }
        else{
            res += b[j]*(h-i);
            j++;
        }
    }
    cout << res << endl;
}
accuracy: 1
time_ms: 20
memory_kb: 5484
final_rank: 4

code_snippet: #include <bits/stdc++.h>
using namespace std;

using ll = long long;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll h, w;
  cin >> h >> w;

  vector<ll> a(h), b(w);
  for (int i = 0; i < h; i++) cin >> a.at(i);
  for (int i = 0; i < w; i++) cin >> b.at(i);

  sort(b.begin(), b.end());

  vector<ll> accb = b;
  for (int i = 1; i < w; i++) accb.at(i) += accb.at(i - 1);

  ll ans = 0;
  for (int i = 0; i < h; i++) {
    auto it = upper_bound(b.begin(), b.end(), a.at(i));
    ll dist = distance(b.begin(), it);
    if (dist > 0) ans += accb.at(dist - 1);
    ans += a.at(i) * (w - dist);
  }

  cout << ans << '\n';
}
accuracy: 1
time_ms: 20
memory_kb: 5448
final_rank: 2

code_snippet: #include <bits/stdc++.h>
//#include <atcoder/all>
//using namespace atcoder;
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
using namespace std;
 
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<int, int> pii;
typedef pair<long long, long long> PLL;
typedef pair<int, PII> TIII;
 
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
 
 
#define FOR(i, s, n) for (int i = s; i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define trav(a, x) for (auto &a : x)
#define all(x) x.begin(), x.end()
 
#define MOD 1000000007
 
template<class T1, class T2> inline bool chmax(T1 &a, T2 b) {if (a < b) {a = b; return true;} return false;}
template<class T1, class T2> inline bool chmin(T1 &a, T2 b) {if (a > b) {a = b; return true;} return false;}
const double EPS = 1e-8, PI = acos(-1);
const double pi = 3.141592653589793238462643383279;
//ここから編集    
typedef string::const_iterator State;
ll GCD(ll a, ll b){
  return (b==0)?a:GCD(b, a%b);
}
ll LCM(ll a, ll b){
  return a/GCD(a, b) * b;
}
template< int mod >
struct ModInt {
  int x;
 
  ModInt() : x(0) {}
 
  ModInt(int64_t y) : x(y >= 0 ? y % mod : (mod - (-y) % mod) % mod) {}
 
  ModInt &operator+=(const ModInt &p) {
    if((x += p.x) >= mod) x -= mod;
    return *this;
  }
 
  ModInt &operator-=(const ModInt &p) {
    if((x += mod - p.x) >= mod) x -= mod;
    return *this;
  }
 
  ModInt &operator*=(const ModInt &p) {
    x = (int) (1LL * x * p.x % mod);
    return *this;
  }
 
  ModInt &operator/=(const ModInt &p) {
    *this *= p.inverse();
    return *this;
  }
 
  ModInt operator-() const { return ModInt(-x); }
 
  ModInt operator+(const ModInt &p) const { return ModInt(*this) += p; }
 
  ModInt operator-(const ModInt &p) const { return ModInt(*this) -= p; }
 
  ModInt operator*(const ModInt &p) const { return ModInt(*this) *= p; }
 
  ModInt operator/(const ModInt &p) const { return ModInt(*this) /= p; }
 
  bool operator==(const ModInt &p) const { return x == p.x; }
 
  bool operator!=(const ModInt &p) const { return x != p.x; }
 
  ModInt inverse() const {
    int a = x, b = mod, u = 1, v = 0, t;
    while(b > 0) {
      t = a / b;
      swap(a -= t * b, b);
      swap(u -= t * v, v);
    }
    return ModInt(u);
  }
 
  ModInt pow(int64_t n) const {
    ModInt ret(1), mul(x);
    while(n > 0) {
      if(n & 1) ret *= mul;
      mul *= mul;
      n >>= 1;
    }
    return ret;
  }
 
  friend ostream &operator<<(ostream &os, const ModInt &p) {
    return os << p.x;
  }
 
  friend istream &operator>>(istream &is, ModInt &a) {
    int64_t t;
    is >> t;
    a = ModInt< mod >(t);
    return (is);
  }
 
  static int get_mod() { return mod; }
};
 
using modint = ModInt< 998244353 >;
template< typename T >
struct Combination {
  vector< T > _fact, _rfact, _inv;
 
  Combination(int sz) : _fact(sz + 1), _rfact(sz + 1), _inv(sz + 1) {
    _fact[0] = _rfact[sz] = _inv[0] = 1;
    for(int i = 1; i <= sz; i++) _fact[i] = _fact[i - 1] * i;
    _rfact[sz] /= _fact[sz];
    for(int i = sz - 1; i >= 0; i--) _rfact[i] = _rfact[i + 1] * (i + 1);
    for(int i = 1; i <= sz; i++) _inv[i] = _rfact[i] * _fact[i - 1];
  }
 
  inline T fact(int k) const { return _fact[k]; }
 
  inline T rfact(int k) const { return _rfact[k]; }
 
  inline T inv(int k) const { return _inv[k]; }
 
  T P(int n, int r) const {
    if(r < 0 || n < r) return 0;
    return fact(n) * rfact(n - r);
  }
 
  T C(int p, int q) const {
    if(q < 0 || p < q) return 0;
    return fact(p) * rfact(q) * rfact(p - q);
  }
 
  T H(int n, int r) const {
    if(n < 0 || r < 0) return (0);
    return r == 0 ? 1 : C(n + r - 1, r);
  }
};
 
ll modpow(ll x, ll n, ll mod) {
  ll res = 1;
  x %= mod;
  if(x == 0) return 0;
  while(n) {
    if(n&1) res = (res * x) % mod;
    x = (x * x) % mod;
    n >>= 1;
  }
  return res;
} 
inline long long mod(long long a, long long m) {
    return (a % m + m) % m;
}
template<typename T> 
struct BIT{
  int N;
  std::vector<T> node;
  BIT(){}
  BIT(int n){
    N = n;
    node.resize(N+10);
  }
  void build(int n) {
    N = n;
    node.resize(N+10);
  }
  /* a: 1-indexed */
  void add(int a, T x){
    for(int i=a; i<(int)node.size(); i += i & -i) node[i] += x;
  }

  /* [1, a] */
  T sum(int a){
    T res = 0;
    for(int x=a; x>0; x -= x & -x) res += node[x];
    return res;
  }

  /* [l, r] */
  T rangesum(int l, int r){
    return sum(r) - sum(l-1);
  }

  /* 
    a1+a2+...+aw >= valとなるような最小のwを返す
    @verify https://codeforces.com/contest/992/problem/E
  */
  int lower_bound(T val) {
    if(val < 0) return 0;

    int res = 0;
    int n = 1; 
    while (n < N) n *= 2;

    T tv=0;
    for (int k = n; k > 0; k /= 2) {
      if(res + k < N && node[res + k] < val){
        val -= node[res+k];
        res += k;
      }
    }
    return res+1; 
  }
};
struct UnionFind{
  std::vector<int> par;
  std::vector<int> siz;

  UnionFind(int sz_): par(sz_), siz(sz_) {
    for(int i=0; i<sz_; ++i) par[i] = i, siz[i] = 1;
  }

  void init(int sz_){
    par.resize(sz_);
    siz.resize(sz_);
    for(int i=0; i<sz_; ++i) par[i] = i, siz[i] = 1;
  }

  int root(int x){
    if(x == par[x]) return x;
    return par[x] = root(par[x]);
  }

  bool merge(int x, int y){
    x = root(x), y = root(y);
    if(x == y) return false;
    if(siz[x] < siz[y]) std::swap(x, y);
    siz[x] += siz[y];
    par[y] = x;
    return true;
  }

  bool issame(int x, int y){
    return root(x) == root(y);
  }

  int size(int x){
    return siz[root(x)];
  }
};
struct RollingHash{

    using ull = unsigned long long;
    const ull mod = (1ULL << 61) - 1;
    const ull MASK30 = (1ULL << 30) - 1;
    const ull MASK31 = (1ULL << 31) - 1;

    const ull MASK61 = mod;

    ull base;
    int n;
    vector<ull> hash, pow;

    RollingHash(const string &s)
    {
        random_device rnd;
        mt19937_64 mt(rnd());
        base = 1001;
        
        n = (int)s.size();
        hash.assign(n+1, 0);
        pow.assign(n+1, 1);
        
        for(int i=0; i<n; i++){
            hash[i+1] = calc_mod(mul(hash[i], base) + s[i]);
            pow[i+1] = calc_mod(mul(pow[i], base));
        }
    }

    ull calc_mod(ull x){
        ull xu = x >> 61;
        ull xd = x & MASK61;
        ull res = xu + xd;
        if(res >= mod) res -= mod;
        return res;
    }

    ull mul(ull a, ull b){
        ull au = a >> 31;
        ull ad = a & MASK31;
        ull bu = b >> 31;
        ull bd = b & MASK31;
        ull mid = ad * bu + au * bd;
        ull midu = mid >> 30;
        ull midd = mid & MASK30;
        return calc_mod(au * bu * 2 + midu + (midd << 31) + ad * bd);
    }

    //[l,r)のハッシュ値
    inline ull get(int l, int r){
        ull res = calc_mod(hash[r] + mod*3-mul(hash[l], pow[r-l]));
        return res;
    }
    //string tのハッシュ値
    inline ull get(string t){
        ull res = 0;
        for(int i=0; i<t.size(); i++){
            res = calc_mod(mul(res, base)+t[i]);
        }
        return res;
    }
    //string s中のtの数をカウント
    inline int count(string t) {
        if(t.size() > n) return 0;
        auto hs = get(t);
        int res = 0;
        for(int i=0; i<n-t.size()+1; i++){
            if(get(i, i+t.size()) == hs) res++; 
        }
        return res;
    }

    /* 
        concat 
        @verify https://codeforces.com/problemset/problem/514/C
    */
    inline ull concat(ull h1, ull h2, int h2len){
      return calc_mod(h2 + mul(h1, pow[h2len]));
    }

    // LCPを求める S[a:] T[b:]
    inline int LCP(int a, int b){
        int len = min((int)hash.size()-a, (int)hash.size()-b);
        
        int lb = -1, ub = len;
        while(ub-lb>1){
            int mid = (lb+ub)/2;

            if(get(a, a+mid) == get(b, b+mid)) lb = mid;
            else ub = mid;
        }
        return lb;
    }
};

int main() {  
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(12);
  
  int h, w; cin >> h >> w;
  vector<ll> a(h), b(w);
  REP(i,h) cin >> a[i];
  REP(i,w) cin >> b[i];

  sort(all(b));
  ll ans = 0;
  vector<ll> cums(w+1);
  REP(i,w) cums[i+1] = cums[i] + b[i];
  REP(i,h) {
    ll num = b.end() - lower_bound(all(b), a[i]);
    ans += num * a[i] + cums[w-num];
  }
  cout << ans << endl;
  return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 5452
final_rank: 3

code_snippet: #include <bits/stdc++.h>
//#include <atcoder/all>
#define LOCAL
using namespace std;
//using namespace atcoder;
template <typename A, typename B>
ostream& operator <<(ostream& out, const pair<A, B>& a) {
out << "(" << a.first << "," << a.second << ")";
return out;
}
template <typename T, size_t N>
ostream& operator <<(ostream& out, const array<T, N>& a) {
out << "["; bool first = true;
for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "]";
return out;
}
template <typename T>
ostream& operator <<(ostream& out, const vector<T>& a) {
out << "["; bool first = true;
for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "]";
return out;
}
template <typename T, class Cmp>
ostream& operator <<(ostream& out, const set<T, Cmp>& a) {
out << "{"; bool first = true;
for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "}";
return out;
}
template <typename U, typename T, class Cmp>
ostream& operator <<(ostream& out, const map<U, T, Cmp>& a) {
out << "{"; bool first = true;
for (auto& p : a) { out << (first ? "" : ", "); out << p.first << ":" << p.second; first = 0;} out << "}";
return out;
}
#ifdef LOCAL
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
#else
#define trace(...) 42
#endif
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
cerr << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
const char* comma = strchr(names + 1, ',');
cerr.write(names, comma - names) << ": " << arg1 << " |";
__f(comma + 1, args...);
}
#define rep(i,n) for(int i=0; i<(n); i++)
using ll = long long;
//#define int long long
using P = pair<int,int>;
//#########################################
int main(){
    int h,w;cin >>h >> w;
    vector<int> yoko(h),tate(w);
    rep(i,h)cin >> yoko[i];
    rep(i,w)cin >> tate[i];
    sort(yoko.begin(),yoko.end());
    sort(tate.begin(),tate.end());
    vector<ll>wsum(w+1);
    rep(i,w)wsum[i+1] = wsum[i] + tate[i];
    ll ret = 0;
    rep(i,h){
        ll x = yoko[i];
        ll num = lower_bound(tate.begin(),tate.end(),x)-tate.begin();
        ret += wsum[num];
        ret += ((ll)w-num)*x;
    }
    cout << ret << endl;
    return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 4740
final_rank: 8

code_snippet: #include<iostream>
#include<vector>
#include<algorithm>
#include<map>
using namespace std;

using lint = long long;

int main(){
    int h, w;
    cin >> h >> w;

    vector<int> a(h);
    map<int, lint> cnta;
    for(int i = 0 ; i < h ; ++i){
        cin >> a[i];
        ++cnta[a[i]];
    }
    sort(a.begin(), a.end());
    a.erase(unique(a.begin(), a.end()), a.end());
    reverse(a.begin(), a.end());
    a.emplace_back(-1);
    h = (int)a.size();

    vector<int> b(w);
    map<int, lint> cntb;
    for(int i = 0 ; i < w ; ++i){
        cin >> b[i];
        ++cntb[b[i]];
    }
    sort(b.begin(), b.end());
    b.erase(unique(b.begin(), b.end()), b.end());
    reverse(b.begin(), b.end());
    b.emplace_back(-1);
    w = (int)b.size();

    lint ans = 0;
    int j = 0, k = 0;
    lint suma = 0, sumb = 0;
    while(j < h || k < w){
        if(a[j] == b[k]){
            suma += cnta[a[j]];
            sumb += cntb[b[k]];
            ans += (cnta[a[j]] * sumb + cntb[b[k]] * suma - cnta[a[j]] * cntb[b[k]]) * a[j];
            ++j;
            ++k;
        }else if(a[j] < b[k]){
            sumb += cntb[b[k]];
            ans += cntb[b[k]] * suma * b[k];
            ++k;
        }else{
            suma += cnta[a[j]];
            ans += cnta[a[j]] * sumb * a[j];
            ++j;
        }
    }

    cout << ans << endl;

    return 0;
}
accuracy: 1
time_ms: 110
memory_kb: 16320
final_rank: 19

code_snippet: #include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
#include<iomanip>
#include <string>
#include <math.h>
using namespace std;

int main(){
    long long int h,w,q=0,e=0,ans=0;
    cin>>h>>w;
    vector<pair<long long int,int>>a(200000);
    for(int i=0;i<h;i++){
        cin>>a[i].first;
        a[i].second=1;
    }
    for(int i=h;i<w+h;i++){
        cin>>a[i].first;
        a[i].second=2;
    }
    sort(a.begin(),a.begin()+h+w,greater<>());
    for(int i=0;i<h+w;i++){
        if(a[i].second==1){
            e++;
            ans+=a[i].first*q;
        }
        else{
            q++;
            ans+=a[i].first*e;
        }
    }
    cout<<ans<<endl;
    return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 5996
final_rank: 13

code_snippet: #include<iostream>
#include<vector>
#include<queue>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;

int main(){
    int h, w;
    cin >> h >> w;
    priority_queue<pii> pq;
    for(int i = 0; i < h; i++){
        int x;  cin >> x;
        pq.push({-x, 0});
    }
    for(int i = 0; i < w; i++){
        int x;  cin >> x;
        pq.push({-x, 1});
    }
    ll ans = 0;
    while(!pq.empty()){
        pii p = pq.top();   pq.pop();
        ll v = -p.first;
        int stat = p.second;
        if(stat == 0){
            ans += v*w;
            h--;
        }else if(stat == 1){
            ans += v*h;
            w--;
        }
    }
    cout << ans << endl;
    return 0;
}
accuracy: 1
time_ms: 70
memory_kb: 4940
final_rank: 14

code_snippet: #include<bits/stdc++.h>

#define REP(i,s,n) for(int i=s;i<n;++i)
#define rep(i,n) REP(i,0,n)
#define EPS (1e-8)
#define equals(a,b) (fabs((a)-(b))<EPS)
#define ALL(x) x.begin(),x.end()

using namespace std;

typedef long long ll;

bool LT(double a,double b) { return !equals(a,b) && a < b; }
bool LTE(double a,double b) { return equals(a,b) || a < b; }

int H,W;
vector<ll> A, B;

struct Data {
  bool type;
  ll len;
  bool operator < ( const Data &data ) const {
    if( len != data.len ) return len < data.len;
    return type < data.type;
  }
};

void compute() {
  vector<Data> vec;
  rep(i,H) vec.push_back((Data){false,A[i]});
  rep(i,W) vec.push_back((Data){true,B[i]});
  sort(ALL(vec));
  ll ans = 0;
  rep(i,(int)vec.size()) {
    if( vec[i].type == false ) { // H
      ans += ( vec[i].len * W );
      --H;
    } else { // W
      ans += ( vec[i].len * H );
      --W;
    }
  }
  cout << ans << endl;
}

int main() {
  cin >> H >> W;
  A.resize(H), B.resize(W);
  rep(i,H) cin >> A[i];
  rep(i,W) cin >> B[i];
  compute();
  return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 8648
final_rank: 17

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define lp(i,n) for(int i=0;i<n;i++)
#define lps(i,j,n) for(int i=j;i<n;i++)
#define fordebug int hoge;cin>>hoge;
#define DEKAI 1000000007;
#define INF (1<<28)
#define int long long
#define double long double
#define floot10 cout<<fixed<<setprecision(10)


signed main(){
  int h,w;
  cin>>h>>w;
  vector<int> a,b;
  lp(i,h){
    int x;
    cin>>x;
    a.push_back(x);
  }
  lp(i,w){
    int x;
    cin>>x;
    b.push_back(x);
  }
  sort(a.begin(),a.end());
  sort(b.begin(),b.end());
  int bit=0,bsum=0,ans=0;
  lp(i,a.size()){
    int now=a[i];
    while(1){
      if(bit==b.size()) break;
      if(b[bit]<now){
	bsum+=b[bit];
	bit++;
      }
      else{
	break;
      }
    }
    ans+=now*(w-bit)+bsum;
  }
  cout<<ans<<endl;
  return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 5244
final_rank: 11

code_snippet: #include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<queue>
#include<functional>
#define REP(i,n) for(int i=0;i<(n);++i)
#define REPA(i,n) for(int i=1;i<(n);++i)
#define PII pair<int,int>
#define PLI pair<long long, int>
#define MOD ((int)1e9 + 7)
#define INF ((int)1e9)
#define ALL(x) (x).begin(),(x).end()
#define ctoi(x) (x - 'a') 
#define CTOI(x) (x - 'A')
using namespace std;
int count(int x){
    int i = 1;
    while(x){
        ++i;
        x /= 2;
    }
    return i;
}
int main(){
    int H,W;
    cin >> H >> W;
    vector<long long> a(H),b(W);
    REP(i,H){
        cin >> a[i];
    }
    REP(i,W){
        cin>>b[i];
    }
    long long ans = 0;
    sort(ALL(a));
    sort(ALL(b));
    vector<long long> sum(W+1);
    sum[0]=0;
    REP(i,W){
        sum[i+1] = sum[i] + b[i];
    }
    long long p = 1;
    REP(i,H){
        while(p<W&&b[p] < a[i])++p;
        ans += sum[p];
        ans += a[i] * (W - p);
    }
    cout << ans << endl;
    return 0;
}
accuracy: 0.2
time_ms: 60
memory_kb: 5236
final_rank: 20

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define MOD 1000000007
#define BIG 1000000010
#define repi(i,x,n) for(int i=x;i<n;i++)
#define rep(i,n) repi(i,0,n)
#define repn(i,n) for(int i=n;i>=0;i--)
typedef long long int ll;


int main(){
  ll  h,w;
  ll a[100000],b[100000];
  ll ans=0;
  cin>>h>>w;
  rep(i,h) cin>>a[i];
  rep(i,w) cin>>b[i];
  sort(a,a+h);sort(b,b+w);
  ll aa=0,bb=0;
  while(1){
    if(h==0) break;
    else{
      if(a[aa]<=b[bb]){
	ans+=a[aa]*w;
	h--;
	aa++;
      }else{
	ans+=b[bb]*h;
	w--;
	bb++;
      }
    }
  }
  cout<<ans<<endl;

  return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 4724
final_rank: 10

code_snippet: #include <bits/stdc++.h>
using namespace std;

#define int long long

signed main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(12);

  int H, W;
  cin >> H >> W;

  vector<int> A(H);
  for ( int i = 0; i < H; i++ ) {
    cin >> A[i];    
  }

  sort(A.begin(), A.end());
  vector<int> cum(H, 0);  
  cum[0] = A[0];
  for ( int i = 1; i < H; i++ ) {
    cum[i] += cum[i-1]+A[i];    
  }

  int ans = 0;
  for ( int i = 0; i < W; i++ ) {
    int b;
    cin >> b;
    int p = upper_bound(A.begin(), A.end(), b) - A.begin();
    p--;
    if ( p >= 0 ) ans += cum[p];
    if ( H-p-1 >= 0 ) ans += b*(H-p-1);    
  }

  cout << ans << endl;
  
  return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 4488
final_rank: 1

code_snippet: #include "bits/stdc++.h"

#pragma warning(disable:4996)
using namespace std;

const int X=100000;
vector<long long int> divisor(long long int n) {
	vector<long long int> res;
	for (long long int i = 1; i * i <= n; ++i) {
		if (n % i == 0) {
			res.push_back(i);
			if (i * i != n) res.push_back(n / i);
		}
	}
	sort(begin(res), end(res));
	return res;
}
int main() {
	int H,W;cin>>H>>W;
	vector<long long int>ys(H),xs(W);
	vector<pair<long long int,int>>ps;
	for (int i = 0; i < H; ++i) {
		int a;cin>>a;
		ps.push_back(make_pair(a,0));
	}
	for (int i = 0; i < W; ++i) {
		int b;cin>>b;
		ps.push_back(make_pair(b,1));
	}

	sort(ps.begin(),ps.end());
	long long int answer=0;
	long long int use_y=0;
	long long int use_x=0;
	for (auto p : ps) {
		if (p.second) {
			answer+=(H-use_y)*p.first;
			use_x++;
		}
		else {
			answer+=(W-use_x)*p.first;
			use_y++;
		}
	}
	cout<<answer<<endl;
	return 0;
}
accuracy: 1
time_ms: 70
memory_kb: 8584
final_rank: 18

code_snippet: /*  -*- coding: utf-8 -*-
 *
 * 2928.cc: Mail Order
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_H = 100000;
const int MAX_W = 100000;

/* typedef */

typedef long long ll;

template <typename T, const int MAX_N>
struct BIT {
  int n;
  T bits[MAX_N + 1];
  BIT() {}

  void init(int _n) {
    n = _n;
    memset(bits, 0, sizeof(bits));
  }

  T sum(int x) {
    T s = 0;
    while (x > 0) {
      s += bits[x];
      x -= (x & -x);
    }
    return s;
  }

  void add(int x, T v) {
    while (x <= n) {
      bits[x] += v;
      x += (x & -x);
    }
  }
};

/* global variables */

int as[MAX_H], uas[MAX_H];
BIT<int,MAX_H> abit;
BIT<ll,MAX_H> sbit;

/* subroutines */

/* main */

int main() {
  int h, w;
  scanf("%d%d", &h, &w);

  for (int i = 0; i < h; i++)
    scanf("%d", as + i), uas[i] = as[i];

  sort(uas, uas + h);
  int un = unique(uas, uas + h) - uas;

  abit.init(un), sbit.init(un);
  for (int i = 0; i < h; i++) {
    int k = lower_bound(uas, uas + un, as[i]) - uas;
    abit.add(k + 1, 1);
    sbit.add(k + 1, as[i]);
  }

  ll sum = 0;
  for (int i = 0; i < w; i++) {
    int bi;
    scanf("%d", &bi);

    int k = upper_bound(uas, uas + un, bi) - uas;
    int j = abit.sum(k);
    sum += sbit.sum(k) + (ll)bi * (h - j);
  }

  printf("%lld\n", sum);
  return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 5240
final_rank: 7

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define fi first
#define se second
#define dbg(x) cout<<#x" = "<<((x))<<endl
template<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;}
template<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;}

const ll INF = LLONG_MAX/3;

int main(){
    int h,w;
    cin >>h >>w;
    vector<ll> a(h),b(w);
    rep(i,h) cin >>a[i];
    rep(i,w) cin >>b[i];
    sort(all(a));
    sort(all(b));

    ll ans = 0;
    ll A=0, B=0;
    int ai=0, bi=0;
    while(ai<h && bi<w){
        ll av = INF, bv = INF;
        if(ai<h) av = a[ai];
        if(bi<w) bv = b[bi];

        if(av<=bv){
            ans += av*(w-B);
            ++A;
            ++ai;
        }
        else{
            ans += bv*(h-A);
            ++B;
            ++bi;
        }
    }
    cout << ans << endl;
    return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 4424
final_rank: 9

code_snippet: #include <bits/stdc++.h>
#define REP(i, a, n) for(ll i=a; i<n; i++)
#define RREP(i, a, n) for(ll i=n-1; i>=a; i--)
typedef long long ll;
const ll mod =1e9+7;
const ll inf =1e18;
using namespace std;

ll h, w;
ll a[101010], b[101010];
vector<pair<ll, bool>> vp; // <a[i]or[b[i], is a>
int main(){
    cin>>h>>w;
    REP(i, 0, h){
        cin>>a[i];
        vp.emplace_back(a[i], true);
    }
    REP(j, 0, w){
        cin>>b[j];
        vp.emplace_back(b[j], false);
    }

    sort(vp.begin(), vp.end());

    ll ans=0;
    ll cnth=w, cntw=h;
    for(auto itr : vp){
        if(itr.second){
            ans+=(itr.first*cnth);
            cntw--;
        }
        else{
            ans+=(itr.first*cntw);
            cnth--;
        }

        if(cnth<=0 || cntw<=0) break;
    }

    cout<<ans<<endl;
    return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 8076
final_rank: 16

code_snippet: #include<iostream>
#include<algorithm>
using namespace std;
#define rep(i,s,n) for(int i=s;i<n;i++)
#define lol(i,n) for(int i=0;i<n;i++)
typedef long long ll;
#define N 100010
ll a[N],b[N],h,w,cs[N];
int main(){
    cin>>h>>w;
    lol(i,h)cin>>a[i];
    lol(j,w)cin>>b[j];
    sort(a,a+h); sort(b,b+w);
    cs[0]=0; lol(i,w)cs[i+1]=cs[i]+b[i];
    ll ans=0;
    lol(i,h){
	ll t=lower_bound(b,b+w,a[i])-b;
	ans+=cs[t]+a[i]*(w-t);
    }
    cout<<ans<<endl;
    return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 5512
final_rank: 12

code_snippet: #include<bits/stdc++.h>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define HUGE_NUM 99999999999999999
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;


int H,W;
vector<ll> A,B;

int main(){

	scanf("%d %d",&H,&W);

	ll tmp;

	for(int loop = 0; loop < H; loop++){
		scanf("%lld",&tmp);
		A.push_back(tmp);
	}

	for(int loop = 0; loop < W; loop++){
		scanf("%lld",&tmp);
		B.push_back(tmp);
	}

	sort(A.begin(),A.end());
	sort(B.begin(),B.end());

	ll ans = 0;

	int left,right,mid;
	int min_index;

	for(int i = 0; i < A.size(); i++){

		left = 0,right = B.size()-1,mid = (left+right)/2;
		min_index = B.size();

		while(left <= right){

			if(A[i] <= B[mid]){

				min_index = mid;
				right = mid-1;

			}else{

				left = mid+1;
			}
			mid = (left+right)/2;
		}
		ans += A[i]*(B.size()-min_index);
	}

	for(int i = 0; i < B.size(); i++){

		left = 0,right = A.size()-1,mid = (left+right)/2;
		min_index = A.size();

		while(left <= right){

			if(B[i] < A[mid]){

				min_index = mid;
				right = mid-1;

			}else{

				left = mid+1;
			}
			mid = (left+right)/2;
		}
		ans += B[i]*(A.size()-min_index);
	}

	printf("%lld\n",ans);

	return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 5260
final_rank: 5

code_snippet: #include <bits/stdc++.h>

using namespace std;

typedef long long ll;

signed main()
{
    ll n, m;
    cin >> n >> m;
    vector<ll> a(n);
    vector<ll> b(m);

    for(auto& x : a)
        cin >> x;
    for(auto& x : b)
        cin >> x;

    vector<ll> rui(n + 1, 0);
    sort(a.begin(), a.end());
    sort(b.begin(), b.end());
    for(ll i = 0; i < n; ++i)
        rui[i + 1] = rui[i] + a[i];

    ll ans = 0;

    for(ll i = 0; i < m; ++i){
        auto it = lower_bound(a.begin(), a.end(), b[i]);
        ll dist = distance(a.begin(), it);

        ans += rui[dist];
        ans += b[i] * (n - dist);
    }
    cout << ans << endl;
}
accuracy: 1
time_ms: 70
memory_kb: 5208
final_rank: 15