Problem p03716 (C++)

Buggy submission (status: Runtime Error):

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <vector>
using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, b, a) for (int i = (b)-1; i >= (a); i--)
#define ITER(it, a)                                                            \
  for (__typeof(a.begin()) it = a.begin(); it != a.end(); it++)
#define FILL(a, value) memset(a, value, sizeof(a))

#define SZ(a) (int)a.size()
#define ALL(a) a.begin(), a.end()
#define PB push_back
#define MP make_pair

typedef long long LL;
typedef vector<int> VI;
typedef pair<int, int> PII;

const double PI = acos(-1.0);
const int INF = 1000 * 1000 * 1000 + 7;
const int MOD = 1000 * 1000 * 1000 + 7;
const LL LINF = INF * (LL)INF;
const int MAX = 100070;

int c[MAX];

int A[MAX];
int m[MAX];

int main() {
  int n;
  scanf("%d", &n);
  FOR(i, 0, 3 * n) { scanf("%d", A + i); }
  multiset<int> a;
  multiset<int> b;
  multiset<int> bb;

  LL s = 0;
  FOR(i, 0, 3 * n) {
    if (b.size() < n) {
      s += A[i];
      b.insert(A[i]);

    } else if (A[i] < *--b.end()) {
      s -= *--b.end();
      s += A[i];
      bb.insert(*--b.end());
      b.erase(--b.end());
      b.insert(A[i]);
    } else {
      bb.insert(A[i]);
    }
  }
  // cout << s << endl;
  LL S = 0;
  LL ans = -LINF;
  FOR(i, 0, 3 * n) {
    if (a.size() == n && b.size() == n) {
      ans = max(ans, S - s);
    }
    if (a.size() < n) {
      S += A[i];
      a.insert(A[i]);
    } else if (A[i] > *a.begin()) {
      S -= *a.begin();
      S += A[i];
      a.erase(a.begin());
      a.insert(A[i]);
    }

    multiset<int>::iterator ib = b.find(A[i]);
    if (ib != b.end()) {
      s -= A[i];
      b.erase(ib);
      if (!bb.empty()) {
        s += *bb.begin();
        b.insert(*bb.begin());
        bb.erase(bb.begin());
      }
    } else {
      bb.erase(bb.find(A[i]));
    }
  }

  cout << ans << endl;

  return 0;
}


Fixed submission:

#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <vector>
using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, b, a) for (int i = (b)-1; i >= (a); i--)
#define ITER(it, a)                                                            \
  for (__typeof(a.begin()) it = a.begin(); it != a.end(); it++)
#define FILL(a, value) memset(a, value, sizeof(a))

#define SZ(a) (int)a.size()
#define ALL(a) a.begin(), a.end()
#define PB push_back
#define MP make_pair

typedef long long LL;
typedef vector<int> VI;
typedef pair<int, int> PII;

const double PI = acos(-1.0);
const int INF = 1000 * 1000 * 1000 + 7;
const int MOD = 1000 * 1000 * 1000 + 7;
const LL LINF = INF * (LL)INF;
const int MAX = 300070;

int c[MAX];

int A[MAX];
int m[MAX];

int main() {
  int n;
  scanf("%d", &n);
  FOR(i, 0, 3 * n) { scanf("%d", A + i); }
  multiset<int> a;
  multiset<int> b;
  multiset<int> bb;

  LL s = 0;
  FOR(i, 0, 3 * n) {
    if (b.size() < n) {
      s += A[i];
      b.insert(A[i]);

    } else if (A[i] < *--b.end()) {
      s -= *--b.end();
      s += A[i];
      bb.insert(*--b.end());
      b.erase(--b.end());
      b.insert(A[i]);
    } else {
      bb.insert(A[i]);
    }
  }
  // cout << s << endl;
  LL S = 0;
  LL ans = -LINF;
  FOR(i, 0, 3 * n) {
    if (a.size() == n && b.size() == n) {
      ans = max(ans, S - s);
    }
    if (a.size() < n) {
      S += A[i];
      a.insert(A[i]);
    } else if (A[i] > *a.begin()) {
      S -= *a.begin();
      S += A[i];
      a.erase(a.begin());
      a.insert(A[i]);
    }

    multiset<int>::iterator ib = b.find(A[i]);
    if (ib != b.end()) {
      s -= A[i];
      b.erase(ib);
      if (!bb.empty()) {
        s += *bb.begin();
        b.insert(*bb.begin());
        bb.erase(bb.begin());
      }
    } else {
      bb.erase(bb.find(A[i]));
    }
  }

  cout << ans << endl;

  return 0;
}