Problem p03716 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#define ll long long
#define db long double
#define ii pair<int, int>
#define vi vector<int>
#define fi first
#define se second
#define sz(a) (int)(a).size()
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define mp make_pair
#define FN(i, n) for (int i = 0; i < (int)(n); ++i)
#define FEN(i, n) for (int i = 1; i <= (int)(n); ++i)
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repv(i, a, b) for (int i = b - 1; i >= a; i--)
#define SET(A, val) memset(A, val, sizeof(A))
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
// order_of_key (val): returns the no. of values less than val
// find_by_order (k): returns the kth largest element.(0-based)
#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define trace(...)
#endif
const int N = 100005;
ll a[N];
multiset<ll> sa1, sa2, sb1, sb2;
int main() {
  std::ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n;
  ll sum1 = 0, sum2 = 0, ans = -LLONG_MAX;
  cin >> n;
  rep(i, 1, 3 * n + 1) cin >> a[i];
  rep(i, 1, n + 1) sum1 += a[i], sa1.insert(a[i]);
  rep(i, n + 1, 3 * n + 1) {
    if (sz(sb1) < n)
      sb1.insert(a[i]), sum2 += a[i];
    else {
      auto it = sb1.end();
      it = prev(it);
      if (*it <= a[i])
        sb2.insert(a[i]);
      else {
        sb2.insert(*it);
        sum2 -= *it;
        sb1.erase(it);
        sb1.insert(a[i]);
        sum2 += a[i];
      }
    }
  }
  ans = sum1 - sum2;
  rep(i, n + 1, 2 * n + 1) {
    ll x = *sa1.begin();
    if (x >= a[i])
      sa2.insert(a[i]);
    else {
      sa1.erase(sa1.begin());
      sum1 -= x;
      sa2.insert(x);
      sa1.insert(a[i]);
      sum1 += a[i];
    }
    if (sb1.find(a[i]) != sb1.end()) {
      sb1.erase(sb1.find(a[i]));
      sum2 -= a[i];
      sb1.insert(*sb2.begin());
      sum2 += *sb2.begin();
      sb2.erase(sb2.begin());
    } else
      sb2.erase(sb2.find(a[i]));
    ans = max(ans, sum1 - sum2);
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#define ll long long
#define db long double
#define ii pair<int, int>
#define vi vector<int>
#define fi first
#define se second
#define sz(a) (int)(a).size()
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define mp make_pair
#define FN(i, n) for (int i = 0; i < (int)(n); ++i)
#define FEN(i, n) for (int i = 1; i <= (int)(n); ++i)
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repv(i, a, b) for (int i = b - 1; i >= a; i--)
#define SET(A, val) memset(A, val, sizeof(A))
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
// order_of_key (val): returns the no. of values less than val
// find_by_order (k): returns the kth largest element.(0-based)
#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define trace(...)
#endif
const int N = 300005;
ll a[N];
multiset<ll> sa1, sa2, sb1, sb2;
int main() {
  std::ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n;
  ll sum1 = 0, sum2 = 0, ans = -LLONG_MAX;
  cin >> n;
  rep(i, 1, 3 * n + 1) cin >> a[i];
  rep(i, 1, n + 1) sum1 += a[i], sa1.insert(a[i]);
  rep(i, n + 1, 3 * n + 1) {
    if (sz(sb1) < n)
      sb1.insert(a[i]), sum2 += a[i];
    else {
      auto it = sb1.end();
      it = prev(it);
      if (*it <= a[i])
        sb2.insert(a[i]);
      else {
        sb2.insert(*it);
        sum2 -= *it;
        sb1.erase(it);
        sb1.insert(a[i]);
        sum2 += a[i];
      }
    }
  }
  ans = sum1 - sum2;
  rep(i, n + 1, 2 * n + 1) {
    ll x = *sa1.begin();
    if (x >= a[i])
      sa2.insert(a[i]);
    else {
      sa1.erase(sa1.begin());
      sum1 -= x;
      sa2.insert(x);
      sa1.insert(a[i]);
      sum1 += a[i];
    }
    if (sb1.find(a[i]) != sb1.end()) {
      sb1.erase(sb1.find(a[i]));
      sum2 -= a[i];
      sb1.insert(*sb2.begin());
      sum2 += *sb2.begin();
      sb2.erase(sb2.begin());
    } else
      sb2.erase(sb2.find(a[i]));
    ans = max(ans, sum1 - sum2);
  }
  cout << ans << endl;
  return 0;
}