Problem p03716 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <assert.h>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <utility>
#include <vector>

#define int long long
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
typedef pair<int, int> pii;
const int INF = 1l << 60;
#define u_b upper_bound
#define l_b lower_bound

int N;
int A[100100];

signed main() {
  cin >> N;
  rep(i, 3 * N) { cin >> A[i]; }
  //[0,b)前半
  //[b,3*N)後半
  map<int, int> left, right, removedRight;
  int ans = -INF;
  int sl = 0, sr = 0; // sum left,right
  int rl = 0, rr = 0; // remove lefr,right
  for (int b = N; b <= 2 * N; ++b) {
    if (b == N) {
      // 初期化
      rep(i, N) {
        left[A[i]]++;
        sl += A[i];
      }
      for (int i = N; i < 3 * N; ++i) {
        right[A[i]]++;
        sr += A[i];
      }
      for (int cnt = 0; cnt < N; ++cnt) {
        int maxRight = right.rbegin()->first;
        rr += maxRight;
        removedRight[maxRight]++;
        right[maxRight]--;
        if (right[maxRight] == 0)
          right.erase(maxRight);
      }
      ans = sl - sr + rr;
    } else {
      sl += A[b - 1];
      sr -= A[b - 1];
      left[A[b - 1]]++;
      right[A[b - 1]]--;

      if (right[A[b - 1]] == -1) {
        rr -= A[b - 1];
        right.erase(A[b - 1]);
        removedRight[A[b - 1]]--;
        if (removedRight[A[b - 1]] == 0)
          removedRight.erase(A[b - 1]);
      } else {
        int minRemoved = removedRight.begin()->first;
        right[minRemoved]++;
        rr -= minRemoved;
        removedRight[minRemoved]--;
        if (removedRight[minRemoved] == 0) {
          removedRight.erase(minRemoved);
        }
      }
      if (right[A[b - 1]] == 0)
        right.erase(A[b - 1]);

      int minLeft = left.begin()->first;
      rl += minLeft;
      left[minLeft]--;
      if (left[minLeft] == 0)
        left.erase(minLeft);

      // cerr << sl << " " << rl << " " << sr << " " << rr << endl;
      ans = max(ans, sl - rl - sr + rr);
    }
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <assert.h>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <utility>
#include <vector>

#define int long long
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
typedef pair<int, int> pii;
const int INF = 1l << 60;
#define u_b upper_bound
#define l_b lower_bound

int N;
int A[300300];

signed main() {
  cin >> N;
  rep(i, 3 * N) { cin >> A[i]; }
  //[0,b)前半
  //[b,3*N)後半
  map<int, int> left, right, removedRight;
  int ans = -INF;
  int sl = 0, sr = 0; // sum left,right
  int rl = 0, rr = 0; // remove lefr,right
  for (int b = N; b <= 2 * N; ++b) {
    if (b == N) {
      // 初期化
      rep(i, N) {
        left[A[i]]++;
        sl += A[i];
      }
      for (int i = N; i < 3 * N; ++i) {
        right[A[i]]++;
        sr += A[i];
      }
      for (int cnt = 0; cnt < N; ++cnt) {
        int maxRight = right.rbegin()->first;
        rr += maxRight;
        removedRight[maxRight]++;
        right[maxRight]--;
        if (right[maxRight] == 0)
          right.erase(maxRight);
      }
      ans = sl - sr + rr;
    } else {
      sl += A[b - 1];
      sr -= A[b - 1];
      left[A[b - 1]]++;
      right[A[b - 1]]--;

      if (right[A[b - 1]] == -1) {
        rr -= A[b - 1];
        right.erase(A[b - 1]);
        removedRight[A[b - 1]]--;
        if (removedRight[A[b - 1]] == 0)
          removedRight.erase(A[b - 1]);
      } else {
        int minRemoved = removedRight.begin()->first;
        right[minRemoved]++;
        rr -= minRemoved;
        removedRight[minRemoved]--;
        if (removedRight[minRemoved] == 0) {
          removedRight.erase(minRemoved);
        }
      }
      if (right[A[b - 1]] == 0)
        right.erase(A[b - 1]);

      int minLeft = left.begin()->first;
      rl += minLeft;
      left[minLeft]--;
      if (left[minLeft] == 0)
        left.erase(minLeft);

      // cerr << sl << " " << rl << " " << sr << " " << rr << endl;
      ans = max(ans, sl - rl - sr + rr);
    }
  }
  cout << ans << endl;
  return 0;
}