#pragma GCC optimize ("O2")
#pragma GCC target ("avx2")
//#include<bits/stdc++.h>
//#include<atcoder/all>
//using namespace atcoder;
#include<iostream>
#include<cstring>

using namespace std;
typedef long long ll;
#define rep(i, n) for(int i = 0; i < (n); i++)
#define rep1(i, n) for(int i = 1; i <= (n); i++)
#define co(x) cout << (x) << "\n"
#define cosp(x) cout << (x) << " "
#define ce(x) cerr << (x) << "\n"
#define cesp(x) cerr << (x) << " "
#define pb push_back
#define mp make_pair
#define chmin(x, y) x = min(x, y)
#define chmax(x, y) x = max(x, y)
#define Would
#define you
#define please

const int IM = 2e9;
int dat[400004];

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


	int N;
	cin >> N;
	dat[0] = -IM;
	ll kotae = -IM * ll(N);
	for (int i = N + 1; i > 1; i--) {
		cin >> dat[i];
		kotae += dat[i];
	}

	for (int i = 2; i < N + 2; i++) {

		int p = 2 + (dat[2] > dat[3]);
		int v = dat[i] - IM;
		while (v > dat[p]) {
			dat[p >> 1] = dat[p];
			p <<= 1;
			p += dat[p] > dat[p + 1];
		}
		dat[p >> 1] = v;

		int a;
		cin >> a;
		kotae += a;
		p = i;
		v = a - IM;
		while (v < dat[p >> 1]) dat[p] = dat[p >> 1], p >>= 1;
		dat[p] = v;

		kotae -= dat[1];
	}

	co(kotae);

	Would you please return 0;
}
#include <iostream>
#include <queue>
using namespace std;
typedef long long ll;

int main()
{
    int n;
    cin >> n;
    ll a[400005];
    for(int i = 0; i < n * 2; i++) cin >> a[i];
    priority_queue<ll> que;
    ll ans = 0;
    for(int i = 0; i < n; i++){
        que.push(a[i]);
        que.push(a[n * 2 - i - 1]);
        ans += que.top();
        que.pop();
    }
    cout << ans << endl;
}
