Problem p03716 (C++)

Buggy submission (status: Runtime Error):
// let's do it
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
#define ll long long int
#define inf 100000000000000000
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define all(v) v.begin(), v.end()
#define S second
#define F first
#define boost1 ios::sync_with_stdio(false);
#define boost2 cin.tie(0);
#define mem(a, val) memset(a, val, sizeof a)
#define endl "\n"
#define maxn 100001

ll pref[maxn], suff[maxn], arr[maxn];
multiset<ll> s;

int main() {
  boost1;
  boost2;
  ll i, j, n, x, y, ans = -inf, sum;
  cin >> n;
  for (i = 1; i <= 3 * n; i++)
    cin >> arr[i];
  sum = 0;
  for (i = 1; i <= 3 * n; i++) {
    if (i <= n || !s.size()) {
      s.insert(arr[i]);
      sum += arr[i];
      pref[i] = sum;
    } else {
      if (arr[i] > *s.begin()) {
        sum -= *s.begin();
        s.erase(s.begin());
        sum += arr[i];
        s.insert(arr[i]);
        pref[i] = sum;
      } else
        pref[i] = sum;
    }
  }
  /*for(i=1;i<=3*n;i++)
  cout<<pref[i]<<" ";
  cout<<endl;*/
  sum = 0;
  s.clear();
  for (i = 3 * n; i >= 1; i--) {
    if (i > 2 * n || !s.size()) {
      s.insert(arr[i]);
      sum += arr[i];
      suff[i] = sum;
    } else {
      if (arr[i] < *(--s.end())) {
        sum -= *(--s.end());
        s.erase(s.find(*(--s.end())));
        sum += arr[i];
        s.insert(arr[i]);
        suff[i] = sum;
      } else
        suff[i] = sum;
    }
  }
  /*for(i=1;i<=3*n;i++)
  cout<<suff[i]<<" ";
  cout<<endl;*/
  // ans=0;
  for (i = n; i <= 2 * n; i++)
    ans = max(ans, pref[i] - suff[i + 1]);
  cout << ans;
  return 0;
}


Fixed submission:
// let's do it
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
#define ll long long int
#define inf 100000000000000000
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define all(v) v.begin(), v.end()
#define S second
#define F first
#define boost1 ios::sync_with_stdio(false);
#define boost2 cin.tie(0);
#define mem(a, val) memset(a, val, sizeof a)
#define endl "\n"
#define maxn 300005

ll pref[maxn], suff[maxn], arr[maxn];
multiset<ll> s;

int main() {
  boost1;
  boost2;
  ll i, j, n, x, y, ans = -inf, sum;
  cin >> n;
  for (i = 1; i <= 3 * n; i++)
    cin >> arr[i];
  sum = 0;
  for (i = 1; i <= 3 * n; i++) {
    if (i <= n || !s.size()) {
      s.insert(arr[i]);
      sum += arr[i];
      pref[i] = sum;
    } else {
      if (arr[i] > *s.begin()) {
        sum -= *s.begin();
        s.erase(s.begin());
        sum += arr[i];
        s.insert(arr[i]);
        pref[i] = sum;
      } else
        pref[i] = sum;
    }
  }
  /*for(i=1;i<=3*n;i++)
  cout<<pref[i]<<" ";
  cout<<endl;*/
  sum = 0;
  s.clear();
  for (i = 3 * n; i >= 1; i--) {
    if (i > 2 * n || !s.size()) {
      s.insert(arr[i]);
      sum += arr[i];
      suff[i] = sum;
    } else {
      if (arr[i] < *(--s.end())) {
        sum -= *(--s.end());
        s.erase(s.find(*(--s.end())));
        sum += arr[i];
        s.insert(arr[i]);
        suff[i] = sum;
      } else
        suff[i] = sum;
    }
  }
  /*for(i=1;i<=3*n;i++)
  cout<<suff[i]<<" ";
  cout<<endl;*/
  // ans=0;
  for (i = n; i <= 2 * n; i++)
    ans = max(ans, pref[i] - suff[i + 1]);
  cout << ans;
  return 0;
}