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

int main(){
    ll N;cin>>N;
    vector<ll> A(N);
    for(ll i=0;i<N;i++){
        cin>>A[i];
    }
    ll ans=0;
    sort(A.rbegin(), A.rend());
    for(ll i=0; i<N; i++){
        ans += A[i]*(N-1-i) - A[i]*i;
    }
    cout << ans << endl;

}

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

int main(){
  	
    int N;
  
  	cin >> N;
  
  	vector<int> A(N),B(N),C(N);
  
    for (int i = 1; i <= N; ++i) cin >> A[i];
   	for (int i = 1; i <= N; ++i) cin >> B[i];
  	for (int i = 1; i <= N; ++i) cin >> C[i];
  	
  	vector<int> count(N);
  	for(int j = 1; j <= N; ++j) count[B[C[j]]] += 1;  
  
  	long long num = 0;
  	for(int i = 1; i <= N; ++i) num += count[A[i]];
 	 
    cout << num << endl;
}