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

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int n;
    cin >> n;
    vector<int> x(n);
    vector<int> y(n);
    vector<int> posx(n, 0);
    vector<int> posy(n, 0);
    iota(posx.begin(), posx.end(), 0);
    iota(posy.begin(), posy.end(), 0);
    for (int i = 0; i < n; i++) {
        cin >> x[i] >> y[i];
    }
    sort(posx.begin(), posx.end(), [&](int i, int j) {
        return x[i] < x[j];
    });
    sort(posy.begin(), posy.end(), [&](int i, int j) {
        return y[i] < y[j];
    });
    set<int> candidates;
    candidates.insert(posx[0]);
    candidates.insert(posx[1]);
    candidates.insert(posx[n - 2]);
    candidates.insert(posx[n - 1]);
    candidates.insert(posy[0]);
    candidates.insert(posy[1]);
    candidates.insert(posy[n - 2]);
    candidates.insert(posy[n - 1]);
    vector<int> dis;
    for (auto i = candidates.begin(); i != candidates.end(); i++) {
        for (auto j = i; j != candidates.end(); j++) {
            if (i == j) continue;
            dis.push_back(max(abs(x[*i] - x[*j]), abs(y[*i] - y[*j])));
        }
    }
    sort(dis.begin(), dis.end(), greater<int>());
    cout << dis[1];
    return 0;
}
#include <iostream>
#include <algorithm>
#include <utility>
using namespace std;

pair<int, int> p[400009];
int st[800009], lz[800009];
	
void lazy(int tn, int s, int e)
{
	if (lz[tn] != 0) {
		st[tn] += lz[tn];
		if (s != e) {
			lz[tn * 2] += lz[tn];
			lz[tn * 2 + 1] += lz[tn];
		}
		lz[tn] = 0;
	}
}

void update(int tn, int s, int e, int ts, int te, int w)
{
	lazy(tn, s, e);
	if (e < ts || s > te) return;
	if (ts <= s && e <= te) {
		lz[tn] += w;
		lazy(tn, s, e);
		return;
	}
	int md = (s + e) / 2;
	update(tn * 2, s, md, ts, te, w); update(tn * 2 + 1, md + 1, e, ts, te, w);
	if (s != e) st[tn] = min(st[tn * 2], st[tn * 2 + 1]);
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	int n; cin >> n;
	for (int i = 1; i <= 2 * n; i++) {
		cin >> p[i].first;
		if (i <= n) p[i].second = n - i + 1;
		else p[i].second = i - n;
	}
	sort(p + 1, p + 2 * n + 1); reverse(p + 1, p + 2 * n + 1);
	long long ans = 0;
	for (int i = 1; i <= n; i++)
		update(1, 1, n, i, n, 1);
	for (int i = 1; i <= 2 * n; i++) {
		update(1, 1, n, p[i].second, n, -1);
		if (st[1] < 0) {
			update(1, 1, n, p[i].second, n, 1);
			continue;
		}
		ans += p[i].first;
	}
	cout << ans << '\n';
	return 0;
}