Problem p03716 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f3f3f3f3f
#define MOD 1000000007
#define MAXN 1005
#define MAXM 3005
#define TRACE(x) cout << #x << " = " << x << '\n'
#define TRACE2(x, y) cout << #x << " = " << x << " " << #y << " = " << y << '\n'
#define TRACEA(arr, i)                                                         \
  cout << #arr << "[" << i << "]"                                              \
       << " = " << arr[i] << '\n'
#define PRINT(arr, n)                                                          \
  for (int iDx = 0; iDx < n; iDx++)                                            \
  printf("%d%c", arr[iDx], " \n"[iDx == n - 1])
#define SZ(x) (int)(x).size()
using namespace std;
struct ii {
  int a, b;
  bool operator<(ii o) const { return tie(a, b) < tie(o.a, o.b); }
};
struct iii {
  int a, b, c;
  bool operator<(iii o) const { return tie(a, b, c) < tie(o.a, o.b, o.c); }
};
int TC, N, M, K, Q, x, y, z;

int arr[MAXM];
long long pbsr[MAXM], pkcl[MAXM];
priority_queue<int, vector<int>, greater<int>> bsr;
priority_queue<int> kcl;

int main() {
#ifdef FAIRUZI10
  freopen("/home/fairuzi/Documents/CP/input.in", "r", stdin);
#endif

  scanf("%d", &N);
  M = 3 * N;
  for (int i = 0; i < M; i++) {
    scanf("%d", &arr[i]);
  }
  long long sum = 0;
  for (int i = 0; i < N; i++)
    bsr.push(arr[i]), sum += arr[i];
  pbsr[N - 1] = sum;
  for (int i = N; i < 2 * N; i++) {
    bsr.push(arr[i]);
    sum += arr[i];
    sum -= bsr.top();
    bsr.pop();
    pbsr[i] = sum;
  }
  sum = 0;
  for (int i = M - 1; i >= 2 * N; i--)
    kcl.push(arr[i]), sum += arr[i];
  pkcl[2 * N] = sum;
  for (int i = 2 * N - 1; i > N - 1; i--) {
    kcl.push(arr[i]);
    sum += arr[i];
    sum -= kcl.top();
    kcl.pop();
    pkcl[i] = sum;
  }
  long long ans = -INF;
  for (int i = N - 1; i < 2 * N; i++) {
    ans = max(ans, pbsr[i] - pkcl[i + 1]);
  }
  printf("%lld\n", ans);

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define INF 0x3f3f3f3f3f3f3f3f
#define MOD 1000000007
#define MAXN 100005
#define MAXM 300005
#define TRACE(x) cout << #x << " = " << x << '\n'
#define TRACE2(x, y) cout << #x << " = " << x << " " << #y << " = " << y << '\n'
#define TRACEA(arr, i)                                                         \
  cout << #arr << "[" << i << "]"                                              \
       << " = " << arr[i] << '\n'
#define PRINT(arr, n)                                                          \
  for (int iDx = 0; iDx < n; iDx++)                                            \
  printf("%d%c", arr[iDx], " \n"[iDx == n - 1])
#define SZ(x) (int)(x).size()
using namespace std;
struct ii {
  int a, b;
  bool operator<(ii o) const { return tie(a, b) < tie(o.a, o.b); }
};
struct iii {
  int a, b, c;
  bool operator<(iii o) const { return tie(a, b, c) < tie(o.a, o.b, o.c); }
};
int TC, N, M, K, Q, x, y, z;

int arr[MAXM];
long long pbsr[MAXM], pkcl[MAXM];
priority_queue<int, vector<int>, greater<int>> bsr;
priority_queue<int> kcl;

int main() {
#ifdef FAIRUZI10
  freopen("/home/fairuzi/Documents/CP/input.in", "r", stdin);
#endif

  scanf("%d", &N);
  M = 3 * N;
  for (int i = 0; i < M; i++) {
    scanf("%d", &arr[i]);
  }
  long long sum = 0;
  for (int i = 0; i < N; i++)
    bsr.push(arr[i]), sum += arr[i];
  pbsr[N - 1] = sum;
  for (int i = N; i < 2 * N; i++) {
    bsr.push(arr[i]);
    sum += arr[i];
    sum -= bsr.top();
    bsr.pop();
    pbsr[i] = sum;
  }
  sum = 0;
  for (int i = M - 1; i >= 2 * N; i--)
    kcl.push(arr[i]), sum += arr[i];
  pkcl[2 * N] = sum;
  for (int i = 2 * N - 1; i > N - 1; i--) {
    kcl.push(arr[i]);
    sum += arr[i];
    sum -= kcl.top();
    kcl.pop();
    pkcl[i] = sum;
  }
  long long ans = -INF;
  for (int i = N - 1; i < 2 * N; i++) {
    ans = max(ans, pbsr[i] - pkcl[i + 1]);
  }
  printf("%lld\n", ans);

  return 0;
}