#include "iostream"
#include "climits"
#include "list"
#include "queue"
#include "stack"
#include "set"
#include "functional"
#include "algorithm"
#include "string"
#include "map"
#include "unordered_map"
#include "unordered_set"
#include "iomanip"
#include "cmath"
#include "random"
#include "bitset"
#include "cstdio"
#include "numeric"
#include "cassert"
#include "ctime"

using namespace std;

constexpr long long int MOD = 1000000007;
//constexpr int MOD = 1000000007;
//constexpr int MOD = 998244353;
//constexpr long long int MOD = 998244353;
constexpr double EPS = 1e-8;

//int N, M, K, T, H, W, L, R;
long long int N, M, K, T, H, W, L, R;

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

	cin >> N;
	vector<int>v(N * 2);
	for (auto& i : v)cin >> i;
	priority_queue<int, vector<int>, greater<int>>PQ;
	for (int i = N, j = N - 1; i < 2 * N; i++, j--) {
		PQ.push(v[i]);
		PQ.push(v[j]);
		PQ.pop();
	}
	long long int ans = 0;
	while (!PQ.empty()) {
		ans += PQ.top();
		PQ.pop();
	}
	cout << ans << endl;
}
#include <bits/stdc++.h>
using namespace std;
void read (int &x) {
    char ch = getchar(); x = 0; while (!isdigit(ch)) ch = getchar();
    while (isdigit(ch)) x = x * 10 + ch - 48, ch = getchar();
} const int N = 2e5 + 5;
int n; pair<int, int> a[N * 2];
struct tree {
    int c[N << 2], tag[N << 2];
    #define ls (p << 1)
    #define rs (p << 1 | 1)
    void push_down (int p) {
        c[ls] += tag[p], c[rs] += tag[p];
        tag[ls] += tag[p], tag[rs] += tag[p]; tag[p] = 0;
    }
    void modify (int p, int l, int r, int ql, int qr, int v) {
         if (ql <= l && r <= qr) {
             c[p] += v, tag[p] += v; return;
         }
         int mid (l + r >> 1); push_down (p);
         if (ql <= mid) modify (ls, l, mid, ql, qr, v);
         if (qr > mid) modify (rs, mid + 1, r, ql, qr, v);
         c[p] = min (c[ls], c[rs]);
    }
    int query (int p, int l, int r, int ql, int qr) {
        if (ql <= l && r <= qr) return c[p];
        int mid (l + r >> 1), s = 1e9; push_down (p);
        if (ql <= mid) s = query (ls, l, mid, ql, qr);
        if (qr > mid) s = min (s, query (rs, mid + 1, r, ql, qr));
        return s;
    }
} qwq;
signed main() {
    read (n); int c = 0;
    for (int i = n, x; i >= 1; --i)
        read (x), a[++c] = {x, i};
    for (int i = 1, x; i <= n; ++i)
        read (x), a[++c] = {x, i};
    sort (a + 1, a + c + 1);
    for (int i = 1; i <= n; ++i) qwq.modify (1, 1, n, i, i, i);
    long long res = 0;
    for (int i = c, s = n; i >= 1 && s; --i) {
        int x = a[i].first, y = a[i].second;
        if (qwq.query (1, 1, n, y, n) >= 1) {
            --s, res += x, qwq.modify (1, 1, n, y, n, -1);
        }
    }
    return printf ("%lld\n", res), 0;
}
