Problem p03714 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define pb(a) push_back(a)

typedef long long ll;
const ll inf = 1e9 + 7;
const int dsize = 1e5 + 5;
int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
vector<ll> prime;
priority_queue<ll, vector<ll>, greater<ll>> ql;
multiset<pair<ll, ll>> qr;

ll inv[dsize], fac[dsize];

inline ll GCD(ll a, ll b) {
  ll c;
  while (b != 0) {
    c = a % b;
    a = b;
    b = c;
  }
  return a;
}
inline ll LCM(ll a, ll b) { return a * b / GCD(a, b); }
inline ll POW(ll a, ll b) {
  ll c = 1;
  while (b > 0) {
    if (b & 1) {
      c = a * c % inf;
    }
    a = a * a % inf;
    b >>= 1;
  }
  return c;
}
inline void _nCr() {
  fac[0] = 1;
  for (int i = 1; i < dsize; i++) {
    fac[i] = fac[i - 1] * i % inf;
  }
  for (int i = 0; i < dsize; i++) {
    inv[i] = POW(fac[i], inf - 2);
  }
}
inline ll nCr(ll n, ll r) { return (fac[n] * inv[r] % inf) * inv[n - r] % inf; }
inline void PRI(ll n) {
  bool a[n + 1];
  for (int i = 0; i < n + 1; i++) {
    a[i] = 1;
  }
  for (int i = 2; i < n + 1; i++) {
    if (a[i]) {
      prime.pb(i);
      ll b = i;
      while (b <= n) {
        a[b] = 0;
        b += i;
      }
    }
  }
}

bool comp(pair<ll, ll> &a, pair<ll, ll> &b) {
  if (a.first == b.first) {
    return a.second >= b.second;
  } else {
    return a.first > b.second;
  }
}

int main() {
  int n;
  cin >> n;
  ll sum = 0;
  ll suml = 0, disl = 0, sumr = 0, disr = 0, maxl = 0, maxr = 0, minl = inf,
     minr = inf;
  ll taimahensyu = -inf;
  ll a[3 * n];
  pair<ll, ll> back[2 * n];
  for (int i = 0; i < 3 * n; i++) {
    cin >> a[i];
    sum += a[i];
    if (i < n) {
      suml += a[i];
      ql.push(a[i]);
    } else if (n <= i) {
      sumr += a[i];
      back[i - n] = {a[i], i};
    }
  }
  sort(back, back + 2 * n, comp);
  for (int i = 0; i < n; i++) {
    qr.insert(back[i]);
    disr += back[i].first;
  }
  taimahensyu = (suml - disl) - (sumr - disr);
  for (int i = 0; i <= n; i++) {
    taimahensyu = max(taimahensyu, (suml - disl) - (sumr - disr));
    if (i == n)
      break;
    ll now = ql.top();
    ql.pop();
    ql.push(a[n + i]);
    if (qr.find({a[n + i], n + i}) != qr.end()) {
      qr.erase({a[n + i], n + i});
      disr -= a[n + i];
    } else {
      disr -= qr.begin()->first;
      qr.erase(*qr.begin());
    }
    suml += a[n + i];
    sumr -= a[n + i];
    disl += now;
  }
  cout << taimahensyu << endl;
}

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

#define pb(a) push_back(a)

typedef long long ll;
const ll inf = 1e9 + 7;
const int dsize = 1e5 + 5;
int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
vector<ll> prime;
priority_queue<ll, vector<ll>, greater<ll>> ql;
multiset<pair<ll, ll>> qr;

ll inv[dsize], fac[dsize];

inline ll GCD(ll a, ll b) {
  ll c;
  while (b != 0) {
    c = a % b;
    a = b;
    b = c;
  }
  return a;
}
inline ll LCM(ll a, ll b) { return a * b / GCD(a, b); }
inline ll POW(ll a, ll b) {
  ll c = 1;
  while (b > 0) {
    if (b & 1) {
      c = a * c % inf;
    }
    a = a * a % inf;
    b >>= 1;
  }
  return c;
}
inline void _nCr() {
  fac[0] = 1;
  for (int i = 1; i < dsize; i++) {
    fac[i] = fac[i - 1] * i % inf;
  }
  for (int i = 0; i < dsize; i++) {
    inv[i] = POW(fac[i], inf - 2);
  }
}
inline ll nCr(ll n, ll r) { return (fac[n] * inv[r] % inf) * inv[n - r] % inf; }
inline void PRI(ll n) {
  bool a[n + 1];
  for (int i = 0; i < n + 1; i++) {
    a[i] = 1;
  }
  for (int i = 2; i < n + 1; i++) {
    if (a[i]) {
      prime.pb(i);
      ll b = i;
      while (b <= n) {
        a[b] = 0;
        b += i;
      }
    }
  }
}

bool comp(pair<ll, ll> &a, pair<ll, ll> &b) {
  if (a.first == b.first) {
    return a.second >= b.second;
  } else {
    return a.first > b.first;
  }
}

int main() {
  int n;
  cin >> n;
  ll sum = 0;
  ll suml = 0, disl = 0, sumr = 0, disr = 0, maxl = 0, maxr = 0, minl = inf,
     minr = inf;
  ll taimahensyu = -inf;
  ll a[3 * n];
  pair<ll, ll> back[2 * n];
  for (int i = 0; i < 3 * n; i++) {
    cin >> a[i];
    sum += a[i];
    if (i < n) {
      suml += a[i];
      ql.push(a[i]);
    } else if (n <= i) {
      sumr += a[i];
      back[i - n] = {a[i], i};
    }
  }
  sort(back, back + 2 * n, comp);
  for (int i = 0; i < n; i++) {
    qr.insert(back[i]);
    disr += back[i].first;
  }
  taimahensyu = (suml - disl) - (sumr - disr);
  for (int i = 0; i <= n; i++) {
    taimahensyu = max(taimahensyu, (suml - disl) - (sumr - disr));
    if (i == n)
      break;
    ll now = ql.top();
    ql.pop();
    ql.push(a[n + i]);
    if (qr.find({a[n + i], n + i}) != qr.end()) {
      qr.erase({a[n + i], n + i});
      disr -= a[n + i];
    } else {
      disr -= qr.begin()->first;
      qr.erase(*qr.begin());
    }
    suml += a[n + i];
    sumr -= a[n + i];
    disl += now;
  }
  cout << taimahensyu << endl;
}