#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <bitset>
#include <climits>
#include <cmath>
#include <complex>
#include <functional>
#include <cassert>
#include <stack>
#include <numeric>
#include <iomanip>
#include <limits>
#include <random>
#include <unordered_map>

typedef int64_t ll;
typedef std::pair<int, int> Pii;
typedef std::pair<ll, ll> Pll;
typedef std::pair<double, double> Pdd;

#define rip(_i, _n, _s) for (int _i = (_s); _i < (int)(_n); _i++)
#define all(_l) _l.begin(), _l.end()
#define rall(_l) _l.rbegin(), _l.rend()
#define MM << " " <<

template<typename _T>
using MaxHeap = std::priority_queue<_T>;
template<typename _T>
using MinHeap = std::priority_queue<_T, std::vector<_T>, std::greater<_T>>;

template<typename _T>
inline bool chmax(_T &_l, const _T _b) {
    if (_l < _b) {
        _l = _b;
        return true;
    }
    return false;
}
template<typename _T>
inline bool chmin(_T &_l, const _T _b) {
    if (_l > _b) {
        _l = _b;
        return true;
    }
    return false;
}

template<typename _T>
void vdeb(const std::vector<_T> &bb) {
    for (unsigned int i = 0;i < bb.size();i++) {
        if (i == bb.size() - 1) std::cout << bb[i];
        else std::cout << bb[i] << ' ';
    }
    std::cout << '\n';
}
template<typename _T>
void vdeb(const std::vector<std::vector<_T>> &bb) {
    for (unsigned int i = 0;i < bb.size();i++) {
        std::cout << i << ' ';
        vdeb(bb[i]);
    }
    std::cout << '\n';
}

using namespace std;

int main() {
    int n; cin >> n;
    vector<ll> da1(n), da2(n);
    ll ans = 0;
    rip(i,n,0) {
        cin >> da1[i];
    }
    rip(i,n,0) cin >> da2[i];
    reverse(all(da1));
    MinHeap<ll> pq;
    rip(i,n,0) {
        pq.push(da1[i]);
        pq.push(da2[i]);
        ans += da1[i] + da2[i];
        ans -= pq.top();
        pq.pop();
    }
    cout << ans << endl;
}
#include<bits/stdc++.h>
#define cs const
#define pb push_back
using namespace std;
typedef long long ll;
cs int N = 4e5 + 5; 
int n, a[N], p[N], c[N];
ll ans; 
#define mid ((l + r) >> 1)
cs int M = N << 2; 
int mn[M], ta[M];
void up(int x) {
	mn[x] = min(mn[x << 1], mn[x << 1 | 1]) + ta[x];
}
void build(int x, int l, int r) {
	if(l == r) {
		mn[x] = l;
		return;
	}
	build(x << 1, l, mid);
	build(x << 1 | 1, mid + 1, r);
	up(x);
}
void mdf(int x, int l, int r, int L, int R) {
	if(L <= l && r <= R) 
		return mn[x] --, ta[x] --, void();
	if(L <= mid) mdf(x << 1, l, mid, L, R);
	if(R > mid) mdf(x << 1 | 1, mid + 1, r, L, R); 
	up(x);
}
int qry(int x, int l, int r, int L, int R) {
	if(L <= l && r <= R) 
		return mn[x];
	int ans = 1e9;
	if(L <= mid) ans = min(ans, qry(x << 1, l, mid, L, R));
	if(R > mid) ans = min(ans, qry(x << 1 | 1, mid + 1, r, L, R));
	return ans + ta[x];
}
#undef mid

void ins(int x, int c) {
	int z = qry(1, 1, n, x, n);
	if(z) {
		ans += c;
		mdf(1, 1, n, x, n);
	}
}
int main() {
	#ifdef FSYo
	freopen("1.in", "r", stdin);
	#endif
	cin >> n; 
	for(int i = 1; i <= n + n; i++)
		scanf("%d", &a[i]), p[i] = i;
	sort(p + 1, p + n + n + 1, 
	[](cs int &i, cs int &j) {
		return a[i] > a[j];
	});
	build(1, 1, n);
	for(int i = 1; i <= n + n; i++) {
		int x = p[i];
		if(x <= n) {
			ins(n - x + 1, a[x]);
		}
		else {
			ins(x - n, a[x]);
		}
	}
	cout << ans; 
	return 0; 
}