Problem:
Score : 500 points

Problem Statement

Let N be a positive integer.

There is a numerical sequence of length 3N, a = (a_1, a_2, ..., a_{3N}).
Snuke is constructing a new sequence of length 2N, a', by removing exactly N elements from a without changing the order of the remaining elements.
Here, the score of a' is defined as follows: (the sum of the elements in the first half of a') - (the sum of the elements in the second half of a').

Find the maximum possible score of a'.

Constraints

1 ≤ N ≤ 10^5

a_i is an integer.

1 ≤ a_i ≤ 10^9

Partial Score

In the test set worth 300 points, N ≤ 1000.

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 ... a_{3N}

Output

Print the maximum possible score of a'.

Sample Input 1

2
3 1 4 1 5 9

Sample Output 1

1

When a_2 and a_6 are removed, a' will be (3, 4, 1, 5), which has a score of (3 + 4) - (1 + 5) = 1.

Sample Input 2

1
1 2 3

Sample Output 2

-1

For example, when a_1 are removed, a' will be (2, 3), which has a score of 2 - 3 = -1.

Sample Input 3

3
8 2 2 7 4 6 5 3 8

Sample Output 3

5

For example, when a_2, a_3 and a_9 are removed, a' will be (8, 7, 4, 6, 5, 3), which has a score of (8 + 7 + 4) - (6 + 5 + 3) = 5.

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <cmath>
#include <cstdio>
#include <limits>
#include <tuple>
#include <stack>
#include <queue>
#include <algorithm>
#include <iostream>
#include <vector>
#include <map>
#include <numeric>
#include <string>
#include <array>
#include <iomanip>

#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rrep(i, n) for (int i = n-1; i >= 0; --i)
#define REP(i, s, n) for (int i = s; i< (int)(n); ++i)
#define all(x) x.begin(), x.end()

using namespace std;
using ll = long long;
ll inf = ll(1e18);
using ll1d = vector<ll>;
using ll2d = vector<ll1d>;
using ll3d = vector<ll2d>;

void solve(istream &ccin = cin, ostream &ccout = cout) {
  ll n;
  ccin >> n;
  vector<ll> a(3 * n);
  rep(i, 3 * n) {
    ccin >> a[i];
  }

  vector<ll> a_f, a_s;
  ll c = 3 * n / 2;

  rep(i, 3 * n) {
    if (i < c)
      a_f.push_back(a[i]);
    else
      a_s.push_back(a[i]);
  }
  sort(all(a_f));
  sort(a_s.rbegin(), a_s.rend());

  vector<ll> c_f, c_s;
  rep(i, n) {
    c_f.push_back(a[c - i - 1]);
    c_s.push_back(a[c + i]);
  };

  vector<ll> sum_cf(n + 1, 0), sum_cs(n + 1, 0);
  rep(i, n) {
    sum_cf[i + 1] = sum_cf[i] + c_f[i];
    sum_cs[i + 1] = sum_cs[i] + c_s[i];
  }

  vector<ll> sum_af(a_f.size() + 1, 0), sum_as(a_s.size() + 1, 0);
  rep(i, a_f.size()) {
    sum_af[i + 1] = sum_af[i] + a_f[i];
  }
  rep(i, a_s.size()) {
    sum_as[i + 1] = sum_as[i] + a_s[i];
  }

  ll score = 0;
  rep(i, n + 1) {
    ll j = n - i;
    ll nuki = sum_as[j] - sum_af[i];
    ll ido;
    if (i >= j)
      ido = 2 * sum_cs[(i - j) / 2];
    else
      ido = -2 * sum_cf[(j - i) / 2];
    score = max(score, nuki + ido);
  }
  ll ans = accumulate(all(a_f), 0) - accumulate(all(a_s), 0) + score;

  ccout << ans << endl;

}

int main() {
  ios::sync_with_stdio(false);
  solve();
  return 0;
}