#include<cstdio>
#include<iostream>
#include<vector>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<map>
using namespace std;

// #define NDEBUG
#include<cassert>

namespace Elaina{
    #define rep(i, l, r) for(int i=(l), i##_end_=(r); i<=i##_end_; ++i)
    #define drep(i, l, r) for(int i=(l), i##_end_=(r); i>=i##_end_; --i)
    #define fi first
    #define se second
    #define mp(a, b) make_pair(a, b)
    #define Endl putchar('\n')
    #define mmset(a, b) memset(a, b, sizeof a)
    typedef long long ll;
    typedef pair<int, int> pii;
    typedef pair<ll, ll> pll;
    template<class T>inline T fab(T x){ return x<0? -x: x; }
    template<class T>inline void getmin(T& x, const T rhs){ x=min(x, rhs); }
    template<class T>inline void getmax(T& x, const T rhs){ x=max(x, rhs); }
    template<class T>inline T readin(T x){
        x=0; int f=0; char c;
        while((c=getchar())<'0' || '9'<c) if(c=='-') f=1;
        for(x=(c^48); '0'<=(c=getchar()) && c<='9'; x=(x<<1)+(x<<3)+(c^48));
        return f? -x: x;
    }
    template<class T>inline void writc(T x, char s='\n'){
        static int fwri_sta[1005], fwri_ed=0;
        if(x<0) putchar('-'), x=-x;
        do fwri_sta[++fwri_ed]=x%10, x/=10; while(x);
        while(putchar(fwri_sta[fwri_ed--]^48), fwri_ed);
        putchar(s);
    }
}
using namespace Elaina;

const int maxn=3e5;

int a[maxn+5], n;
map<int, int>m;

signed main(){
    n=readin(1);
    rep(i, 1, n){
        a[i]=readin(1);
        ++m[a[i]];
    }
    ll ans=0;
    rep(i, 1, n){
        ans+=(n-m[a[i]]);
    }
    writc(ans>>1);
    return 0;
}
#include <bits/stdc++.h>
using namespace std;
 
int main(){
    long n, g = 0;
    cin >> n;
    long a[n], b[n];
    for(int i = 0; i < n; i++){
        cin >> a[i];
        g += a[i];
    }
    for(int i = 0; i < n; i++){
        cin >> b[i];
        g += b[i];
    }
    priority_queue<long, vector<long>, greater<long>> q;
    for(int i = 0; i < n; i++){
        q.push(a[n - 1 - i]);
        q.push(b[i]);
        g -= q.top();
        q.pop();
    }
    cout << g << endl;
}