#include<bits/stdc++.h>
using namespace std;
using i64 = long long;
 
int main(){
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  #ifndef ONLINE_JUDGE
  freopen("input.txt","r",stdin);
  freopen("output.txt","w",stdout);
  #endif
  int n;
  cin >> n;
  vector<int> v(2*n);
  for(int i = 0; i < 2*n; ++i){
    cin >> v[i]; 
  }
  multiset<int> s;
  int l = 0;
  int r = 2*n -1;
  i64  ans = 0;
  while(l<r){
    s.insert(v[l]);
    s.insert(v[r]);
    auto it = prev(s.end());
    ans += *it;
    s.erase(it);
    l += 1;
    r -= 1;
  }
  cout << ans << endl;
  return 0;
}
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;
typedef vector<pll> vpll;
typedef vector<vpll> vvpll;
typedef vector<bool> vbl;
typedef vector<vector<bool>> vvbl;

void INX(){}
template<typename Head, typename... Tail>
void INX(Head&& head, Tail&&... tail)
{
    cin >> head;
    INX(forward<Tail>(tail)...);
}
void OUTX(){}
template<typename Head, typename... Tail>
void OUTX(Head&& head, Tail&&... tail)
{
    cout << head << endl;
    OUTX(forward<Tail>(tail)...);
}
void OUTX2(){cout << endl;}
template<typename Head, typename... Tail>
void OUTX2(Head&& head, Tail&&... tail)
{
    cout << head << ' ';
    OUTX2(forward<Tail>(tail)...);
}

#define ADD emplace_back
#define MP make_pair
#define __LONG_LONG_MIN__ (-__LONG_LONG_MAX__ - 1)

#define MOD 998244353
//setprecision(10)

class UnionFind
{
    private:
    vll parent;

    public:
    UnionFind(ll N) : parent(N)
    {
        for (ll i = 0; i < N; i++) parent[i] = i;
    }

    ll GetParent(ll x)
    {
        return (parent[x] == x ? x : (parent[x] = GetParent(parent[x])));
    }

    void Unite(ll x, ll y)
    {
        ll rx = GetParent(x);
        ll ry = GetParent(y);
        if(rx != ry) parent[rx] = ry;
    }

    bool IsUnited(ll x, ll y)
    {
        return GetParent(x) == GetParent(y);
    }
};

int main()
{
    ll N;
    INX(N);

    vll f(N);
    UnionFind uf(N);
    for (ll i = 0; i < N; i++)
    {
        INX(f[i]);
        f[i]--;
        uf.Unite(i, f[i]);
    }

    set<ll> s;
    for (ll i = 0; i < N; i++)
    {
        s.emplace(uf.GetParent(i));
    }

    ll result = 1;
    for (ll i = 0; i < s.size(); i++)
    {
        result <<= 1;
        result %= MOD;
    }
    result = (result == 0 ? MOD : result) - 1;
    OUTX(result);


    return 0;
}
