Problem p03716 (C++)

Buggy submission (status: Runtime Error):
// author : Himanshu Shukla
#pragma GCC optimize("O2")
#include <bits/stdc++.h>
using namespace std;
#define FASTIO ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define ll long long
#define v32 vector<int>
#define v64 vector<ll>
#define p32 pair<int, int>
#define p64 pair<ll, ll>
#define fi first
#define se second
#define ln "\n"
#define debug(x) cout << (#x) << ": " << (x) << ln
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
ll add(ll a, ll b, ll m) { return (((a % m + b % m) % m + m) % m); }
ll mul(ll a, ll b, ll m) { return (((a % m * b % m) % m + m) % m); }
const double EPS = 1e-6;
const ll MOD = 1e9 + 7;
template <typename T, typename U>
ostream &operator<<(ostream &out, const pair<T, U> &p) {
  out << "[" << p.fi << ", " << p.se << "]";
  return out;
}
template <template <typename, typename...> class ContainerType,
          typename ValueType, typename... Args>
void print_container(const ContainerType<ValueType, Args...> &c) {
  cout << "{ ";
  for (const auto &v : c)
    cout << v << " ";
  cout << "}" << ln;
}

const int N = 1e5 + 5;
int arr[N];
ll fwd[N], bwd[N];
ll sum = 0;

int main() {
  FASTIO;
  int n;
  cin >> n;
  for (int i = 0; i < 3 * n; i++)
    cin >> arr[i];
  priority_queue<int, vector<int>, greater<int>> Q;
  for (int i = 0; i < 3 * n; i++) {
    if (Q.size() < n) {
      Q.push(arr[i]);
      sum += arr[i];
    } else if (arr[i] > Q.top()) {
      sum += (arr[i] - Q.top());
      Q.pop();
      Q.push(arr[i]);
    }
    if (Q.size() == n)
      fwd[i] = sum;
  }

  sum = 0;
  while (!Q.empty())
    Q.pop();
  for (int i = 3 * n - 1; i >= 0; i--) {
    if (Q.size() < n) {
      Q.push(-arr[i]);
      sum += arr[i];
    } else if (Q.top() + arr[i] < 0) {
      sum += (arr[i] + Q.top());
      Q.pop();
      Q.push(-arr[i]);
    }
    if (Q.size() == n)
      bwd[i] = sum;
  }

  ll ans = -1e18;
  for (int i = n - 1; i < 2 * n; i++)
    ans = max(fwd[i] - bwd[i + 1], ans);
  cout << ans << ln;
  return 0;
}


Fixed submission:
// author : Himanshu Shukla
#pragma GCC optimize("O2")
#include <bits/stdc++.h>
using namespace std;
#define FASTIO ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define ll long long
#define v32 vector<int>
#define v64 vector<ll>
#define p32 pair<int, int>
#define p64 pair<ll, ll>
#define fi first
#define se second
#define ln "\n"
#define debug(x) cout << (#x) << ": " << (x) << ln
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
ll add(ll a, ll b, ll m) { return (((a % m + b % m) % m + m) % m); }
ll mul(ll a, ll b, ll m) { return (((a % m * b % m) % m + m) % m); }
const double EPS = 1e-6;
const ll MOD = 1e9 + 7;
template <typename T, typename U>
ostream &operator<<(ostream &out, const pair<T, U> &p) {
  out << "[" << p.fi << ", " << p.se << "]";
  return out;
}
template <template <typename, typename...> class ContainerType,
          typename ValueType, typename... Args>
void print_container(const ContainerType<ValueType, Args...> &c) {
  cout << "{ ";
  for (const auto &v : c)
    cout << v << " ";
  cout << "}" << ln;
}

const int N = 3e5 + 5;
int arr[N];
ll fwd[N], bwd[N];
ll sum = 0;

int main() {
  FASTIO;
  int n;
  cin >> n;
  for (int i = 0; i < 3 * n; i++)
    cin >> arr[i];
  priority_queue<int, vector<int>, greater<int>> Q;
  for (int i = 0; i < 3 * n; i++) {
    if (Q.size() < n) {
      Q.push(arr[i]);
      sum += arr[i];
    } else if (arr[i] > Q.top()) {
      sum += (arr[i] - Q.top());
      Q.pop();
      Q.push(arr[i]);
    }
    if (Q.size() == n)
      fwd[i] = sum;
  }

  sum = 0;
  while (!Q.empty())
    Q.pop();
  for (int i = 3 * n - 1; i >= 0; i--) {
    if (Q.size() < n) {
      Q.push(-arr[i]);
      sum += arr[i];
    } else if (Q.top() + arr[i] < 0) {
      sum += (arr[i] + Q.top());
      Q.pop();
      Q.push(-arr[i]);
    }
    if (Q.size() == n)
      bwd[i] = sum;
  }

  ll ans = -1e18;
  for (int i = n - 1; i < 2 * n; i++)
    ans = max(fwd[i] - bwd[i + 1], ans);
  cout << ans << ln;
  return 0;
}