Problem p03714 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <assert.h>
#include <bitset>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
typedef long long ll;
typedef unsigned long long ull;

#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define DEBUG(vec)                                                             \
  for (auto v : vec) {                                                         \
    cout << v << " ";                                                          \
  }                                                                            \
  cout << endl;

using namespace std;

ll a[100010] = {};
ll l[100010] = {};
ll r[100010] = {};

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

  int N;
  cin >> N;
  REP(i, 3 * N) { cin >> a[i]; }

  ll left = 0;
  priority_queue<ll, vector<ll>, greater<ll>> leftq;
  REP(i, N) {
    leftq.push(a[i]);
    left += a[i];
  }
  l[0] = left;
  for (int i = 1; i <= N; i++) {
    leftq.push(a[N + i - 1]);
    left += a[N + i - 1];
    left -= leftq.top();
    leftq.pop();
    l[i] = left;
  }

  ll right = 0;
  priority_queue<ll> rightq;
  REP(i, N) {
    rightq.push(a[3 * N - 1 - i]);
    right += a[3 * N - 1 - i];
  }
  r[N] = right;
  for (int i = 1; i <= N; i++) {
    rightq.push(a[2 * N - i]);
    right += a[2 * N - i];
    right -= rightq.top();
    rightq.pop();
    r[N - i] = right;
  }

  ll mx = l[0] - r[0];
  REP(i, N + 1) {
    // cout << l[i] << " " << r[N - i] << endl;
    mx = max(mx, l[i] - r[i]);
  }
  cout << mx << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <array>
#include <assert.h>
#include <bitset>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
typedef long long ll;
typedef unsigned long long ull;

#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define DEBUG(vec)                                                             \
  for (auto v : vec) {                                                         \
    cout << v << " ";                                                          \
  }                                                                            \
  cout << endl;

using namespace std;

ll a[300030] = {};
ll l[100010] = {};
ll r[100010] = {};

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

  int N;
  cin >> N;
  REP(i, 3 * N) { cin >> a[i]; }

  ll left = 0;
  priority_queue<ll, vector<ll>, greater<ll>> leftq;
  REP(i, N) {
    leftq.push(a[i]);
    left += a[i];
  }
  l[0] = left;
  for (int i = 1; i <= N; i++) {
    leftq.push(a[N + i - 1]);
    left += a[N + i - 1];
    left -= leftq.top();
    leftq.pop();
    l[i] = left;
  }

  ll right = 0;
  priority_queue<ll> rightq;
  REP(i, N) {
    rightq.push(a[3 * N - 1 - i]);
    right += a[3 * N - 1 - i];
  }
  r[N] = right;
  for (int i = 1; i <= N; i++) {
    rightq.push(a[2 * N - i]);
    right += a[2 * N - i];
    right -= rightq.top();
    rightq.pop();
    r[N - i] = right;
  }

  ll mx = l[0] - r[0];
  REP(i, N + 1) {
    // cout << l[i] << " " << r[N - i] << endl;
    mx = max(mx, l[i] - r[i]);
  }
  cout << mx << endl;
  return 0;
}