Problem p03714 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
using namespace std;

using ll = long long;
using ull = uint64_t;
using P = pair<int, int>;
constexpr double EPS = 1e-9;
constexpr int INF = 1001001001;
constexpr int mod = 1000000007;
// constexpr int mod = 998244353;

template <class T> inline bool chmax(T &x, T y) {
  if (x < y) {
    x = y;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &x, T y) {
  if (x > y) {
    x = y;
    return true;
  }
  return false;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int n;
  cin >> n;
  int N = n * 3;
  vector<int> a(N);
  for (int i = 0; i < N; ++i)
    cin >> a[i];
  ll S1 = 0, S2 = 0;
  priority_queue<int, vector<int>, greater<int>> Q1;
  for (int i = 0; i < n; ++i) {
    S1 += a[i];
    Q1.push(a[i]);
  }
  multiset<int> st, st2;
  for (int i = n; i < N; ++i) {
    S2 += a[i];
    st.insert(a[i]);
  }
  int c = n;
  while (c-- > 0) {
    int val = *(st.rbegin());
    st.erase(*(st.rbegin()));
    S2 -= val;
    st2.insert(val);
  }
  ll ans = S1 - S2;
  for (int i = n; i < N - n; ++i) {
    if (a[i] > Q1.top()) {
      S1 -= Q1.top();
      Q1.pop();
      Q1.push(a[i]);
      S1 += a[i];
    }
    auto it = st.find(a[i]);
    if (it != st.end()) {
      S2 -= a[i];
      st.erase(it);
      int val = *(st2.begin());
      S2 += val;
      st2.erase(st2.begin());
      st.insert(val);
    } else {
      st2.erase(st2.find(a[i]));
    }
    chmax(ans, S1 - S2);
  }
  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
using namespace std;

using ll = long long;
using ull = uint64_t;
using P = pair<int, int>;
constexpr double EPS = 1e-9;
constexpr int INF = 1001001001;
constexpr int mod = 1000000007;
// constexpr int mod = 998244353;

template <class T> inline bool chmax(T &x, T y) {
  if (x < y) {
    x = y;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &x, T y) {
  if (x > y) {
    x = y;
    return true;
  }
  return false;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int n;
  cin >> n;
  int N = n * 3;
  vector<int> a(N);
  for (int i = 0; i < N; ++i)
    cin >> a[i];
  ll S1 = 0, S2 = 0;
  priority_queue<int, vector<int>, greater<int>> Q1;
  for (int i = 0; i < n; ++i) {
    S1 += a[i];
    Q1.push(a[i]);
  }
  multiset<int> st, st2;
  for (int i = n; i < N; ++i) {
    S2 += a[i];
    st.insert(a[i]);
  }
  int c = n;
  while (c-- > 0) {
    int val = *(st.rbegin());
    st.erase(st.find(val));
    S2 -= val;
    st2.insert(val);
  }
  ll ans = S1 - S2;
  for (int i = n; i < N - n; ++i) {
    if (a[i] > Q1.top()) {
      S1 -= Q1.top();
      Q1.pop();
      Q1.push(a[i]);
      S1 += a[i];
    }
    auto it = st.find(a[i]);
    if (it != st.end()) {
      S2 -= a[i];
      st.erase(it);
      int val = *(st2.begin());
      S2 += val;
      st2.erase(st2.begin());
      st.insert(val);
    } else {
      st2.erase(st2.find(a[i]));
    }
    chmax(ans, S1 - S2);
  }
  cout << ans << endl;
}