Problem p03714 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define repr(i, a, b) for (int i = a; i < b; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define reprrev(i, a, b) for (int i = b - 1; i >= a; i--) // [a, b)
#define reprev(i, n) reprrev(i, 0, n)
typedef long long ll;
typedef unsigned long long ull;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

const ll mod = 1e9 + 7;

void chmod(ll &M) {
  if (M >= mod)
    M %= mod;
  else if (M < 0) {
    M += (abs(M) / mod + 1) * mod;
    M %= mod;
  }
}

ll modpow(ll x, ll n) {
  if (n == 0)
    return 1;
  ll res = modpow(x, n / 2);

  if (n % 2 == 0)
    return res * res % mod;
  else
    return res * res % mod * x % mod;
}

int getl(int i, int N) { return i == 0 ? N - 1 : i - 1; };
int getr(int i, int N) { return i == N - 1 ? 0 : i + 1; };

// 線分 ab の偏角 返り値は[-π, π]
double argument(const pair<double, double> &a, const pair<double, double> &b) {
  double ax = a.first, ay = a.second, bx = b.first, by = b.second;
  return atan2(by - ay, bx - ax);
}

/* <----------------------------------------------------------------------------------->
 */
/* <----------------------------------------------------------------------------------->
 */
/* <----------------------------------------------------------------------------------->
 */
/* <----------------------------------------------------------------------------------->
 */
/* <----------------------------------------------------------------------------------->
 */
/* <----------------------------------------------------------------------------------->
 */
/* <----------------------------------------------------------------------------------->
 */
/* <----------------------------------------------------------------------------------->
 */

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  int N;
  cin >> N;
  vector<ll> a(N);
  rep(i, 3 * N) cin >> a[i];

  vector<ll> left(2 * N, 0), right(2 * N, 0);

  priority_queue<ll, vector<ll>, greater<ll>> lpq;
  ll lmax = 0;
  rep(i, N) {
    lpq.push(a[i]);
    lmax += a[i];
  }
  left[N - 1] = lmax;
  repr(i, N, 2 * N) {
    lmax += a[i];
    lpq.push(a[i]);
    lmax -= lpq.top();
    lpq.pop();
    left[i] = lmax;
  }

  priority_queue<ll> rpq;
  ll rmin = 0;
  repr(i, 2 * N, 3 * N) {
    rpq.push(a[i]);
    rmin += a[i];
  }
  right[2 * N - 1] = rmin;
  reprrev(i, N, 2 * N) {
    rmin += a[i];
    rpq.push(a[i]);
    rmin -= rpq.top();
    rpq.pop();
    right[i - 1] = rmin;
  }

  ll ans = -1e15;
  repr(i, N - 1, 2 * N) chmax(ans, left[i] - right[i]);
  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define repr(i, a, b) for (int i = a; i < b; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define reprrev(i, a, b) for (int i = b - 1; i >= a; i--) // [a, b)
#define reprev(i, n) reprrev(i, 0, n)
typedef long long ll;
typedef unsigned long long ull;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

const ll mod = 1e9 + 7;

void chmod(ll &M) {
  if (M >= mod)
    M %= mod;
  else if (M < 0) {
    M += (abs(M) / mod + 1) * mod;
    M %= mod;
  }
}

ll modpow(ll x, ll n) {
  if (n == 0)
    return 1;
  ll res = modpow(x, n / 2);

  if (n % 2 == 0)
    return res * res % mod;
  else
    return res * res % mod * x % mod;
}

int getl(int i, int N) { return i == 0 ? N - 1 : i - 1; };
int getr(int i, int N) { return i == N - 1 ? 0 : i + 1; };

// 線分 ab の偏角 返り値は[-π, π]
double argument(const pair<double, double> &a, const pair<double, double> &b) {
  double ax = a.first, ay = a.second, bx = b.first, by = b.second;
  return atan2(by - ay, bx - ax);
}

/* <----------------------------------------------------------------------------------->
 */
/* <----------------------------------------------------------------------------------->
 */
/* <----------------------------------------------------------------------------------->
 */
/* <----------------------------------------------------------------------------------->
 */
/* <----------------------------------------------------------------------------------->
 */
/* <----------------------------------------------------------------------------------->
 */
/* <----------------------------------------------------------------------------------->
 */
/* <----------------------------------------------------------------------------------->
 */

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  int N;
  cin >> N;
  vector<ll> a(3 * N);
  rep(i, 3 * N) cin >> a[i];

  vector<ll> left(2 * N, 0), right(2 * N, 0);

  priority_queue<ll, vector<ll>, greater<ll>> lpq;
  ll lmax = 0;
  rep(i, N) {
    lpq.push(a[i]);
    lmax += a[i];
  }
  left[N - 1] = lmax;
  repr(i, N, 2 * N) {
    lmax += a[i];
    lpq.push(a[i]);
    lmax -= lpq.top();
    lpq.pop();
    left[i] = lmax;
  }

  priority_queue<ll> rpq;
  ll rmin = 0;
  repr(i, 2 * N, 3 * N) {
    rpq.push(a[i]);
    rmin += a[i];
  }
  right[2 * N - 1] = rmin;
  reprrev(i, N, 2 * N) {
    rmin += a[i];
    rpq.push(a[i]);
    rmin -= rpq.top();
    rpq.pop();
    right[i - 1] = rmin;
  }

  ll ans = -1e15;
  repr(i, N - 1, 2 * N) chmax(ans, left[i] - right[i]);
  cout << ans << endl;

  return 0;
}