#include <bits/stdc++.h>

#define F first
#define S second
#define prev azaza
#define MP make_pair
#define PB push_back

using namespace std;
typedef long long ll;
typedef long double ld;

const int max_n = 200111, inf = 1000111222;

vector<int> a, b;

int main()
{
    //freopen("input.txt", "r", stdin);
    int n;
    cin >> n;
    for (int i = 0; i < n; ++i) {
        int x;
        scanf("%d", &x);
        a.PB(x);
    }
    for (int i = 0; i < n; ++i) {
        int x;
        scanf("%d", &x);
        b.PB(x);
    }
    reverse(b.begin(), b.end());
    multiset<int> ms;
    ll sum = 0;
    for (int i = 0; i < n; ++i) {
        ms.insert(a[i]);
        ms.insert(b[i]);
        auto it = (--ms.end());
        sum += *it;
        ms.erase(it);
    }
    cout << sum << endl;
    return 0;
}



#include<bits/stdc++.h>
using namespace std;
using ll=long long;
const ll inf=998244353;
int main(){
  ll n;cin>>n;
  vector<ll>a(n);
  for(ll i=0;i<n;i++)cin>>a.at(i);
  sort(a.begin(),a.end());
  ll ans=0;
  for(ll i=0;i<n;i++){ans+=a.at(i)%inf*a.at(i)%inf;ans%=inf;}
  ll tmp=a.at(n-1);
  for(ll i=n-2;i>=0;i--){
    ans+=a.at(i)*tmp%inf;
    ans%=inf;
    tmp*=2;
    tmp%=inf;
    tmp+=a.at(i);
    tmp%=inf;
  }
  cout<<ans<<endl;
}