Problem p03716 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int ll
#define FOR(i, a, b) for (int i = (a); i < int(b); ++i)
#define RFOR(i, a, b) for (int i = (b)-1; i >= int(a); --i)
#define rep(i, n) FOR(i, 0, n)
#define rep1(i, n) FOR(i, 1, int(n) + 1)
#define rrep(i, n) RFOR(i, 0, n)
#define rrep1(i, n) RFOR(i, 1, int(n) + 1)
#define all(c) begin(c), end(c)
const int MOD = 1000000007;

template <typename T> void __dump__(std::ostream &os, const T &first) {
  os << first;
}
template <typename First, typename... Rest>
void __dump__(std::ostream &os, const First &first, const Rest &...rest) {
  os << first << ", ";
  __dump__(os, rest...);
}
#define dump(...)                                                              \
  do {                                                                         \
    std::ostringstream os;                                                     \
    os << __LINE__ << ":\t" << #__VA_ARGS__ << " = ";                          \
    __dump__(os, __VA_ARGS__);                                                 \
    std::cerr << os.str() << std::endl;                                        \
  } while (0)

const ll INF = 1e18;

int a[100010];
int L[100010], R[100010];

signed main() {
  int n;
  while (cin >> n && n) {
    rep(i, n + n + n) { cin >> a[i]; }

    {
      priority_queue<int> q;
      int s = 0;
      rep(i, n * 3) {
        s += a[i];
        q.push(-a[i]);
        if ((int)q.size() > n) {
          int x = -q.top();
          q.pop();
          s -= x;
        }
        L[i] = s;
      }
    }
    {
      priority_queue<int> q;
      int s = 0;
      rrep(i, n * 3) {
        s += a[i];
        q.push(a[i]);
        if ((int)q.size() > n) {
          int x = q.top();
          q.pop();
          s -= x;
        }
        R[i] = s;
      }
    }

    int ans = -INF;
    rep(i, n * 3) {
      if (n - 1 <= i && i < n + n) {
        dump(L[i], R[i + 1]);
        ans = max(ans, L[i] - R[i + 1]);
      }
    }
    cout << ans << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int ll
#define FOR(i, a, b) for (int i = (a); i < int(b); ++i)
#define RFOR(i, a, b) for (int i = (b)-1; i >= int(a); --i)
#define rep(i, n) FOR(i, 0, n)
#define rep1(i, n) FOR(i, 1, int(n) + 1)
#define rrep(i, n) RFOR(i, 0, n)
#define rrep1(i, n) RFOR(i, 1, int(n) + 1)
#define all(c) begin(c), end(c)
const int MOD = 1000000007;

template <typename T> void __dump__(std::ostream &os, const T &first) {
  os << first;
}
template <typename First, typename... Rest>
void __dump__(std::ostream &os, const First &first, const Rest &...rest) {
  os << first << ", ";
  __dump__(os, rest...);
}
#define dump(...)                                                              \
  do {                                                                         \
    std::ostringstream os;                                                     \
    os << __LINE__ << ":\t" << #__VA_ARGS__ << " = ";                          \
    __dump__(os, __VA_ARGS__);                                                 \
    std::cerr << os.str() << std::endl;                                        \
  } while (0)

const ll INF = 1e18;

int a[300010];
int L[300010], R[300010];

signed main() {
  int n;
  while (cin >> n && n) {
    rep(i, n + n + n) { cin >> a[i]; }

    {
      priority_queue<int> q;
      int s = 0;
      rep(i, n * 3) {
        s += a[i];
        q.push(-a[i]);
        if ((int)q.size() > n) {
          int x = -q.top();
          q.pop();
          s -= x;
        }
        L[i] = s;
      }
    }
    {
      priority_queue<int> q;
      int s = 0;
      rrep(i, n * 3) {
        s += a[i];
        q.push(a[i]);
        if ((int)q.size() > n) {
          int x = q.top();
          q.pop();
          s -= x;
        }
        R[i] = s;
      }
    }

    int ans = -INF;
    rep(i, n * 3) {
      if (n - 1 <= i && i < n + n) {
        dump(L[i], R[i + 1]);
        ans = max(ans, L[i] - R[i + 1]);
      }
    }
    cout << ans << endl;
  }
}