#include<bits/stdc++.h>
using namespace std;
const int N=4e5+7;
typedef long long LL;
int a[N],id[N];
bool cmp(int x,int y){
	return a[x]>a[y];
}
bool vis[N];
priority_queue<int> Q;
int n;
int tr(int i){
	return i<=n?n-i+1:i-n;
}
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=2*n;i++)scanf("%d",&a[i]),id[i]=i;
	int tot=2*n;
	sort(id+1,id+tot+1,cmp);
	LL ans=0;
	for(int i=1;i<=n;i++)vis[id[i]]=1,ans+=a[id[i]];
	int s=n,j=n;
	for(int i=n;i>=1;i--){
		if(s>i){
			while(tr(id[j])>i)j--;
			vis[id[j]]=0;
			ans-=a[id[j]]; s--;     //printf("-%d\n",a[id[j]]);
			ans+=Q.top();   //printf("%d\n",Q.top());
			Q.pop(); j--;
		}
		if(!vis[i+n])Q.push(a[i+n]);
		if(!vis[n-i+1])Q.push(a[n-i+1]);
		s-=vis[i+n]+vis[n-i+1];
	}
	printf("%lld\n",ans);
	return 0;
}
			

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

using lint = long long int;
using pint = pair<int, int>;
using plint = pair<lint, lint>;
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((lint)(x).size())
#define POW2(n) (1LL << (n))
#define FOR(i, begin, end) for (int i = (begin), i##_end_ = (end); i < i##_end_; i++)
#define IFOR(i, begin, end) for (int i = (end)-1, i##_begin_ = (begin); i >= i##_begin_; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)

int main()
{
    lint n;cin >> n;
    vector<lint> a(n);
    REP(i, n) cin >> a[i];
    map<lint, lint> mp;
    REP(i,n){
        if(mp.count(a[i])==0){
            mp[a[i]] = 1;
        }else{
            mp[a[i]] = mp[a[i]] += 1;
        }
    }
    lint ans = n*(n-1)/2;
    for(auto &e:mp){
        ans -=e.second*(e.second-1)/2;
    }
    cout << ans << "\n";
    return 0;
}