prithivMLmods/Coder-Stat huggingface dataset:
Problem p03714 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int N;
int a[100001];
long long int sum[300030], sum2[300030];
priority_queue<int, vector<int>, greater<int>> que;
priority_queue<int> que2;
int main() {
  cin >> N;
  for (int i = 1; i <= 3 * N; i++)
    cin >> a[i];

  for (int i = 1; i <= N; i++) {
    que.push(a[i]);
    sum[i] = sum[i - 1] + a[i];
  }

  for (int i = N + 1; i <= 2 * N; i++) {
    que.push(a[i]);
    long long int x = que.top();
    que.pop();
    sum[i] = sum[i - 1] + a[i] - x;
  }

  for (int i = 3 * N; i > 2 * N; i--) {
    que2.push(a[i]);
    sum2[i] = sum2[i + 1] + a[i];
  }
  for (int i = 2 * N; i > N; i--) {
    que2.push(a[i]);
    long long int x = que2.top();
    que2.pop();
    sum2[i] = sum2[i + 1] + a[i] - x;
  }
  long long int ans = sum[N] - sum2[N + 1];
  for (int i = N + 1; i <= 2 * N; i++) {
    ans = max(ans, sum[i] - sum2[i + 1]);
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int N;
long long int a[300030];
long long int sum[300030], sum2[300030];
priority_queue<int, vector<int>, greater<int>> que;
priority_queue<int> que2;
int main() {
  cin >> N;
  for (int i = 1; i <= 3 * N; i++)
    cin >> a[i];

  for (int i = 1; i <= N; i++) {
    que.push(a[i]);
    sum[i] = sum[i - 1] + a[i];
  }

  for (int i = N + 1; i <= 2 * N; i++) {
    que.push(a[i]);
    long long int x = que.top();
    que.pop();
    sum[i] = sum[i - 1] + a[i] - x;
  }

  for (int i = 3 * N; i > 2 * N; i--) {
    que2.push(a[i]);
    sum2[i] = sum2[i + 1] + a[i];
  }
  for (int i = 2 * N; i > N; i--) {
    que2.push(a[i]);
    long long int x = que2.top();
    que2.pop();
    sum2[i] = sum2[i + 1] + a[i] - x;
  }
  long long int ans = sum[N] - sum2[N + 1];
  for (int i = N + 1; i <= 2 * N; i++) {
    ans = max(ans, sum[i] - sum2[i + 1]);
  }
  cout << ans << endl;
  return 0;
}
Problem p03714 (C++)

Buggy submission (status: Runtime Error):

#include <algorithm>
#include <iostream>
#include <queue>
#include <set>
#define rep(i, j, n) for (int i = j; i < n; i++)
using namespace std;
#define int long long
#define inf 1000000007
signed main() {
  int n;
  cin >> n;
  priority_queue<int, vector<int>, greater<int>> pu;
  int sum = 0;
  int num[100005];
  int res[100005];
  rep(i, 0, 3 * n) { cin >> num[i]; }
  rep(i, 0, n) {
    pu.push(num[i]);
    sum += num[i];
  }
  res[n - 1] = sum;
  rep(i, n, 2 * n) {
    pu.push(num[i]);
    sum += num[i];
    sum -= pu.top();
    pu.pop();
    res[i] = sum;
  }
  priority_queue<int> Q1;
  int sum1 = 0;
  for (int i = 3 * n - 1; i >= 2 * n; i--) {
    Q1.push(num[i]);
    sum1 += num[i];
  }
  res[2 * n - 1] -= sum1;
  for (int i = 2 * n - 1; i >= n; i--) {
    Q1.push(num[i]);
    sum1 += num[i];
    sum1 -= Q1.top();
    Q1.pop();
    res[i - 1] -= sum1;
  }
  int ans = -1e15;
  for (int i = n - 1; i < 2 * n; i++) {
    ans = max(ans, res[i]);
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <set>
#define rep(i, j, n) for (int i = j; i < n; i++)
using namespace std;
#define int long long
#define inf 1000000007
signed main() {
  int n;
  cin >> n;
  priority_queue<int, vector<int>, greater<int>> pu;
  int sum = 0;
  int num[300005];
  int res[300005];
  rep(i, 0, 3 * n) { cin >> num[i]; }
  rep(i, 0, n) {
    pu.push(num[i]);
    sum += num[i];
  }
  res[n - 1] = sum;
  rep(i, n, 2 * n) {
    pu.push(num[i]);
    sum += num[i];
    sum -= pu.top();
    pu.pop();
    res[i] = sum;
  }
  priority_queue<int> Q1;
  int sum1 = 0;
  for (int i = 3 * n - 1; i >= 2 * n; i--) {
    Q1.push(num[i]);
    sum1 += num[i];
  }
  res[2 * n - 1] -= sum1;
  for (int i = 2 * n - 1; i >= n; i--) {
    Q1.push(num[i]);
    sum1 += num[i];
    sum1 -= Q1.top();
    Q1.pop();
    res[i - 1] -= sum1;
  }
  int ans = -1e15;
  for (int i = n - 1; i < 2 * n; i++) {
    ans = max(ans, res[i]);
  }
  cout << ans << endl;
  return 0;
}
Problem p03714 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define len(v) ll(v.size())
#define fi first
#define se second

template <class T> void cout_vec(const vector<T> &vec) {
  for (auto itr : vec)
    cout << itr << ' ';
  cout << endl;
}

typedef pair<ll, ll> P;
const ll mod = 1e9 + 7;
const ll inf = 1e15;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<ll> a(3 * n + 1), x(2 * n + 1), y(2 * n + 1);
  priority_queue<ll, vector<ll>, greater<ll>> cnt1;
  priority_queue<ll> cnt2;
  ll sum1 = 0, sum2 = 0, ans = -inf;
  FOR(i, 1, 3 * n + 1) {
    cin >> a[i];
    if (i <= n) {
      cnt1.push(a[i]);
      sum1 += a[i];
    } else if (2 * n + 1 <= i) {
      cnt2.push(a[i]);
      sum2 += a[i];
    }
  }
  x[n] = sum1, y[2 * n + 1] = sum2;
  FOR(i, n + 1, 2 * n + 1) {
    cnt1.push(a[i]);
    sum1 += a[i] - cnt1.top();
    cnt1.pop();
    x[i] = sum1;
  }
  for (int i = 2 * n; i >= n + 1; i--) {
    cnt2.push(a[i]);
    sum2 += a[i] - cnt2.top();
    cnt2.pop();
    y[i] = sum2;
  }
  FOR(i, n, 2 * n + 1) ans = max(ans, x[i] - y[i + 1]);
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define len(v) ll(v.size())
#define fi first
#define se second

template <class T> void cout_vec(const vector<T> &vec) {
  for (auto itr : vec)
    cout << itr << ' ';
  cout << endl;
}

typedef pair<ll, ll> P;
const ll mod = 1e9 + 7;
const ll inf = 1e15;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  vector<ll> a(3 * n + 1), x(2 * n + 1), y(2 * n + 10);
  priority_queue<ll, vector<ll>, greater<ll>> cnt1;
  priority_queue<ll> cnt2;
  ll sum1 = 0, sum2 = 0, ans = -inf;
  FOR(i, 1, 3 * n + 1) {
    cin >> a[i];
    if (i <= n) {
      cnt1.push(a[i]);
      sum1 += a[i];
    } else if (2 * n + 1 <= i) {
      cnt2.push(a[i]);
      sum2 += a[i];
    }
  }
  x[n] = sum1, y[2 * n + 1] = sum2;
  FOR(i, n + 1, 2 * n + 1) {
    cnt1.push(a[i]);
    sum1 += a[i] - cnt1.top();
    cnt1.pop();
    x[i] = sum1;
  }
  for (int i = 2 * n; i >= n + 1; i--) {
    cnt2.push(a[i]);
    sum2 += a[i] - cnt2.top();
    cnt2.pop();
    y[i] = sum2;
  }
  FOR(i, n, 2 * n + 1) ans = max(ans, x[i] - y[i + 1]);
  cout << ans << endl;
}
Problem p03714 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>
using namespace std;
const int max_n = 100000 + 5;
long long n, W, L, P, c;

long long v[max_n], w[max_n], m[max_n], A[max_n], spf[max_n], co[max_n];
typedef pair<long, long> pii;
pii p[max_n];
pii bb[max_n];
struct Cmp {
  bool operator()(const pii &a, const pii &b) { return a.second > b.second; }
};
void solve() {
  priority_queue<long> ba;
  priority_queue<long, vector<long>, greater<long>> fo;
  vector<long long> s(2 * n + 1, 0);
  for (int i = 0; i < n; i++) {
    s[i + 1] = s[i] + v[i];
    fo.push(v[i]);
  }
  for (int i = n; i < 2 * n; i++) {
    long long mi = fo.top();
    if (mi < v[i]) {
      s[i + 1] = s[i] + v[i] - mi;
      fo.pop();
      fo.push(v[i]);
    } else {
      s[i + 1] = s[i];
    }
  }
  vector<long long> T(2 * n + 1);
  for (int i = 0; i < n; i++) {
    T[i + 1] = T[i] + v[3 * n - i - 1];
    ba.push(v[3 * n - i - 1]);
  }
  for (int i = n; i < 2 * n; i++) {
    long long ma = ba.top();
    if (v[3 * n - i - 1] < ma) {
      T[i + 1] = T[i] - ma + v[3 * n - i - 1];
      ba.pop();
      ba.push(v[3 * n - i - 1]);
    } else {
      T[i + 1] = T[i];
    }
  }
  long long res = -(1LL << 60);
  for (int i = n; i <= 2 * n; i++) {
    res = max(res, s[i] - T[3 * n - i]);
  }

  cout << res << endl;
}
int main() {
  cin >> n;
  for (int i = 0; i < n * 3; i++) {
    cin >> v[i];
  }
  solve();
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>
using namespace std;
const int max_n = 100000 + 5;
long long n, W, L, P, c;

long long v[3 * max_n], w[max_n], m[max_n], A[max_n], spf[max_n], co[max_n];
typedef pair<long, long> pii;
pii p[max_n];
pii bb[max_n];
struct Cmp {
  bool operator()(const pii &a, const pii &b) { return a.second > b.second; }
};
void solve() {
  priority_queue<long> ba;
  priority_queue<long, vector<long>, greater<long>> fo;
  vector<long long> s(2 * n + 1, 0);
  for (int i = 0; i < n; i++) {
    s[i + 1] = s[i] + v[i];
    fo.push(v[i]);
  }
  for (int i = n; i < 2 * n; i++) {
    long long mi = fo.top();
    if (mi < v[i]) {
      s[i + 1] = s[i] + v[i] - mi;
      fo.pop();
      fo.push(v[i]);
    } else {
      s[i + 1] = s[i];
    }
  }
  vector<long long> T(2 * n + 1);
  for (int i = 0; i < n; i++) {
    T[i + 1] = T[i] + v[3 * n - i - 1];
    ba.push(v[3 * n - i - 1]);
  }
  for (int i = n; i < 2 * n; i++) {
    long long ma = ba.top();
    if (v[3 * n - i - 1] < ma) {
      T[i + 1] = T[i] - ma + v[3 * n - i - 1];
      ba.pop();
      ba.push(v[3 * n - i - 1]);
    } else {
      T[i + 1] = T[i];
    }
  }
  long long res = -(1LL << 60);
  for (int i = n; i <= 2 * n; i++) {
    res = max(res, s[i] - T[3 * n - i]);
  }

  cout << res << endl;
}
int main() {
  cin >> n;
  for (int i = 0; i < n * 3; i++) {
    cin >> v[i];
  }
  solve();
  return 0;
}
Problem p03716 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int long long
#define inf (int)(3e18)
#define mod (int)(1e9 + 7)
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
int n, a[114514];
int sum[333333], sum2[333333];
signed main() {
  cin >> n;
  rep(i, 3 * n) cin >> a[i];
  {
    multiset<int> st;
    rep(i, n) {
      st.insert(a[i]);
      sum[n - 1] += a[i];
    }
    for (int i = n; i < 2 * n; i++) {
      sum[i] = sum[i - 1];
      auto it = st.lower_bound(a[i]);
      if (it != st.begin()) {
        sum[i] -= *st.begin();
        sum[i] += a[i];
        st.erase(st.begin());
        st.insert(a[i]);
      }
    }
  }
  {
    multiset<int> st;
    for (int i = 3 * n - 1; i >= 2 * n; i--) {
      st.insert(a[i]);
      sum2[2 * n] += a[i];
    }
    for (int i = 2 * n - 1; i >= n; i--) {
      sum2[i] = sum2[i + 1];
      auto it = st.lower_bound(a[i]);
      if (it != st.end()) {
        auto it2 = st.end();
        it2--;
        sum2[i] -= *it2;
        sum2[i] += a[i];
        st.erase(it2);
        st.insert(a[i]);
      }
    }
  }
  int ans = -inf;
  for (int i = n - 1; i < 2 * n; i++) {
    ans = max(ans, sum[i] - sum2[i + 1]);
  }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#define int long long
#define inf (int)(3e18)
#define mod (int)(1e9 + 7)
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
int n, a[333333];
int sum[333333], sum2[333333];
signed main() {
  cin >> n;
  rep(i, 3 * n) cin >> a[i];
  {
    multiset<int> st;
    rep(i, n) {
      st.insert(a[i]);
      sum[n - 1] += a[i];
    }
    for (int i = n; i < 2 * n; i++) {
      sum[i] = sum[i - 1];
      auto it = st.lower_bound(a[i]);
      if (it != st.begin()) {
        sum[i] -= *st.begin();
        sum[i] += a[i];
        st.erase(st.begin());
        st.insert(a[i]);
      }
    }
  }
  {
    multiset<int> st;
    for (int i = 3 * n - 1; i >= 2 * n; i--) {
      st.insert(a[i]);
      sum2[2 * n] += a[i];
    }
    for (int i = 2 * n - 1; i >= n; i--) {
      sum2[i] = sum2[i + 1];
      auto it = st.lower_bound(a[i]);
      if (it != st.end()) {
        auto it2 = st.end();
        it2--;
        sum2[i] -= *it2;
        sum2[i] += a[i];
        st.erase(it2);
        st.insert(a[i]);
      }
    }
  }
  int ans = -inf;
  for (int i = n - 1; i < 2 * n; i++) {
    ans = max(ans, sum[i] - sum2[i + 1]);
  }
  cout << ans << endl;
}
Problem p03716 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, n) for (int i = 1; i < n; i++)
#define all(v) v.begin(), v.end()
#define inf (int)(3e18)
#define P pair<int, int>
#define mod (int)(1e9 + 7)

#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")

template <class T> inline void chmin(T &a, T b) { a = min(a, b); }
template <class T> inline void chmax(T &a, T b) { a = max(a, b); }

int N, A[100005];
int mx[100005], mn[100005];
multiset<int> stmx, stmn;
signed main() {
  cin >> N;
  rep(i, 3 * N) cin >> A[i];
  rep(i, N) {
    mx[N - 1] += A[i];
    stmx.insert(A[i]);
  }
  for (int i = N; i < 2 * N; i++) {
    mx[i] = mx[i - 1];
    int j = *stmx.begin();
    if (A[i] > j) {
      mx[i] += A[i];
      mx[i] -= j;
      stmx.erase(stmx.begin());
      stmx.insert(A[i]);
    }
  }
  for (int i = 3 * N - 1; i >= 2 * N; i--) {
    mn[2 * N] += A[i];
    stmn.insert(A[i]);
  }
  for (int i = 2 * N - 1; i >= N; i--) {
    mn[i] = mn[i + 1];
    int j = *--stmn.end();
    if (A[i] < j) {
      mn[i] += A[i];
      mn[i] -= j;
      stmn.erase(--stmn.end());
      stmn.insert(A[i]);
    }
  }
  int ans = -inf;
  for (int i = N - 1; i < 2 * N; i++)
    chmax(ans, mx[i] - mn[i + 1]);
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, n) for (int i = 1; i < n; i++)
#define all(v) v.begin(), v.end()
#define inf (int)(3e18)
#define P pair<int, int>
#define mod (int)(1e9 + 7)

#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")

template <class T> inline void chmin(T &a, T b) { a = min(a, b); }
template <class T> inline void chmax(T &a, T b) { a = max(a, b); }

int N, A[300005];
int mx[300005], mn[300005];
multiset<int> stmx, stmn;
signed main() {
  cin >> N;
  rep(i, 3 * N) cin >> A[i];
  rep(i, N) {
    mx[N - 1] += A[i];
    stmx.insert(A[i]);
  }
  for (int i = N; i < 2 * N; i++) {
    mx[i] = mx[i - 1];
    int j = *stmx.begin();
    if (A[i] > j) {
      mx[i] += A[i];
      mx[i] -= j;
      stmx.erase(stmx.begin());
      stmx.insert(A[i]);
    }
  }
  for (int i = 3 * N - 1; i >= 2 * N; i--) {
    mn[2 * N] += A[i];
    stmn.insert(A[i]);
  }
  for (int i = 2 * N - 1; i >= N; i--) {
    mn[i] = mn[i + 1];
    int j = *--stmn.end();
    if (A[i] < j) {
      mn[i] += A[i];
      mn[i] -= j;
      stmn.erase(--stmn.end());
      stmn.insert(A[i]);
    }
  }
  int ans = -inf;
  for (int i = N - 1; i < 2 * N; i++)
    chmax(ans, mx[i] - mn[i + 1]);
  cout << ans << endl;
}
Problem p03716 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 32345;
ll a[N], l[N], r[N];

int main() {
  int n;
  cin >> n;
  priority_queue<int, vector<int>, greater<int>> pq1;
  priority_queue<int> pq2;
  for (int i = 1; i <= 3 * n; i++) {
    cin >> a[i];
  }

  ll sum = 0;
  for (int i = 1; i <= 3 * n; i++) {
    sum += a[i];
    pq1.push(a[i]);
    if (pq1.size() > n) {
      sum -= pq1.top();
      pq1.pop();
    }
    l[i] = sum;
  }
  sum = 0;
  for (int i = 3 * n; i >= 0; i--) {
    sum += a[i];
    pq2.push(a[i]);
    if (pq2.size() > n) {
      sum -= pq2.top();
      pq2.pop();
    }
    r[i] = sum;
  }
  ll ans = -1e18;
  for (int i = n; i <= 2 * n; i++) {
    ans = max(ans, l[i] - r[i + 1]);
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 323456;
ll a[N], l[N], r[N];

int main() {
  int n;
  cin >> n;
  priority_queue<int, vector<int>, greater<int>> pq1;
  priority_queue<int> pq2;
  for (int i = 1; i <= 3 * n; i++) {
    cin >> a[i];
  }

  ll sum = 0;
  for (int i = 1; i <= 3 * n; i++) {
    sum += a[i];
    pq1.push(a[i]);
    if (pq1.size() > n) {
      sum -= pq1.top();
      pq1.pop();
    }
    l[i] = sum;
  }
  sum = 0;
  for (int i = 3 * n; i >= 0; i--) {
    sum += a[i];
    pq2.push(a[i]);
    if (pq2.size() > n) {
      sum -= pq2.top();
      pq2.pop();
    }
    r[i] = sum;
  }
  ll ans = -1e18;
  for (int i = n; i <= 2 * n; i++) {
    ans = max(ans, l[i] - r[i + 1]);
  }
  cout << ans << endl;
}
Problem p03716 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

using ll = long long;

const int MAXN = 2e5;
int N;
int A[MAXN];

ll maxSum[2][MAXN];

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> N;
  for (int i = 0; i < 3 * N; i++) {
    cin >> A[i];
  }

  for (int z = 0; z < 2; z++) {
    multiset<int> s;
    ll curSum = 0;
    for (int i = 0; i < 3 * N; i++) {
      s.insert(A[i]);
      curSum += A[i];
      if (int(s.size()) > N) {
        curSum -= *s.begin();
        s.erase(s.begin());
      }
      assert(int(s.size()) <= N);
      maxSum[z][i + 1] = curSum;
    }
    reverse(A, A + 3 * N);
    for (int i = 0; i < 3 * N; i++) {
      A[i] *= -1;
    }
  }

  ll ans = -9e18;
  for (int i = N; i <= 2 * N; i++) {
    ans = max(ans, maxSum[0][i] + maxSum[1][3 * N - i]);
  }
  cout << ans << '\n';

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

using ll = long long;

const int MAXN = 4e5;
int N;
int A[MAXN];

ll maxSum[2][MAXN];

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> N;
  for (int i = 0; i < 3 * N; i++) {
    cin >> A[i];
  }

  for (int z = 0; z < 2; z++) {
    multiset<int> s;
    ll curSum = 0;
    for (int i = 0; i < 3 * N; i++) {
      s.insert(A[i]);
      curSum += A[i];
      if (int(s.size()) > N) {
        curSum -= *s.begin();
        s.erase(s.begin());
      }
      assert(int(s.size()) <= N);
      maxSum[z][i + 1] = curSum;
    }
    reverse(A, A + 3 * N);
    for (int i = 0; i < 3 * N; i++) {
      A[i] *= -1;
    }
  }

  ll ans = -9e18;
  for (int i = N; i <= 2 * N; i++) {
    ans = max(ans, maxSum[0][i] + maxSum[1][3 * N - i]);
  }
  cout << ans << '\n';

  return 0;
}
Problem p03716 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define MAXN 100100
#define pii pair<int, int>
#define pb push_back
typedef long long ll;

using namespace std;

int n, a[MAXN];
ll ans = -1e18;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  for (int i = 0; i < 3 * n; i++) {
    cin >> a[i];
  }
  multiset<int> l, r, rr;
  ll ls = 0, rs = 0;
  for (int i = 0; i < n; i++) {
    l.insert(a[i]);
    ls += a[i];
  }
  for (int i = n; i < 3 * n; i++) {
    r.insert(a[i]);
    rr.insert(a[i]);
  }
  auto ri = r.begin();
  for (int i = 0; i < n; i++) {
    rs += *ri;
    ri++;
  }
  r.erase(ri, r.end());
  while (rr.size() > n)
    rr.erase(rr.begin());

  ans = max(ans, ls - rs);
  for (int i = n; i < 2 * n; i++) { // add i to left side
    // cout << ans;
    l.insert(a[i]);
    ls += a[i];
    ls -= *l.begin();
    l.erase(l.begin());

    if (rr.find(a[i]) != rr.end()) {
      rr.erase(rr.find(a[i]));
    } else {
      r.erase(r.find(a[i]));
      rs += *rr.begin();
      r.insert(*rr.begin());
      rr.erase(rr.begin());
      rs -= a[i];
    }

    ans = max(ans, ls - rs);
  }
  cout << ans;
}

Fixed submission:
#include <bits/stdc++.h>
#define MAXN 300100
#define pii pair<int, int>
#define pb push_back
typedef long long ll;

using namespace std;

int n, a[MAXN];
ll ans = -1e18;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  for (int i = 0; i < 3 * n; i++) {
    cin >> a[i];
  }
  multiset<int> l, r, rr;
  ll ls = 0, rs = 0;
  for (int i = 0; i < n; i++) {
    l.insert(a[i]);
    ls += a[i];
  }
  for (int i = n; i < 3 * n; i++) {
    r.insert(a[i]);
    rr.insert(a[i]);
  }
  auto ri = r.begin();
  for (int i = 0; i < n; i++) {
    rs += *ri;
    ri++;
  }
  r.erase(ri, r.end());
  while (rr.size() > n)
    rr.erase(rr.begin());

  ans = max(ans, ls - rs);
  for (int i = n; i < 2 * n; i++) { // add i to left side
    // cout << ans;
    l.insert(a[i]);
    ls += a[i];
    ls -= *l.begin();
    l.erase(l.begin());

    if (rr.find(a[i]) != rr.end()) {
      rr.erase(rr.find(a[i]));
    } else {
      r.erase(r.find(a[i]));
      rs += *rr.begin();
      r.insert(*rr.begin());
      rr.erase(rr.begin());
      rs -= a[i];
    }

    ans = max(ans, ls - rs);
  }
  cout << ans;
}
Problem p03716 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define FOR(x, n) for (int x = 0; x < n; x++)
#define ALL(a) (a).begin(), (a).end()
#define SZ(a) ((int)(a).size())
#define FIN                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
using namespace std;
typedef long long ll;
const int MXN = 1e5 + 1;
int N;
ll A[MXN], m1[MXN] = {}, m2[MXN] = {};
int main() {
  FIN;
  cin >> N;
  FOR(x, 3 * N) cin >> A[x];

  priority_queue<int, vector<int>, greater<int>> mns;
  priority_queue<int> mxs;

  ll totalMX = 0;
  FOR(x, N) totalMX += A[x], mns.push(A[x]);
  m1[N - 1] = totalMX;
  for (int x = N; x < 3 * N; x++) {
    totalMX += A[x];
    mns.push(A[x]);
    totalMX -= mns.top();
    mns.pop();
    m1[x] = totalMX;
  }

  ll totalMN = 0, i = 3 * N - 1;
  FOR(x, N)
  totalMN += A[3 * N - 1 - x],
      mxs.push(A[3 * N - 1 - x]), i = 3 * N - 1 - x;
  m2[i] = totalMN;
  for (int x = i - 1; x >= 0; x--) {
    totalMN += A[x];
    mxs.push(A[x]);
    totalMN -= mxs.top();
    mxs.pop();
    m2[x] = totalMN;
  }

  ll ans = -(1LL << 62);
  for (int x = N - 1; x < i; x++)
    ans = max(ans, m1[x] - m2[x + 1]);

  cout << ans << "\n";
}

Fixed submission:
#include <bits/stdc++.h>
#define FOR(x, n) for (int x = 0; x < n; x++)
#define ALL(a) (a).begin(), (a).end()
#define SZ(a) ((int)(a).size())
#define FIN                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
using namespace std;
typedef long long ll;
const int MXN = 1e5 + 1;
int N;
ll A[3 * MXN], m1[3 * MXN] = {}, m2[3 * MXN] = {};
int main() {
  FIN;
  cin >> N;
  FOR(x, 3 * N) cin >> A[x];

  priority_queue<int, vector<int>, greater<int>> mns;
  priority_queue<int> mxs;

  ll totalMX = 0;
  FOR(x, N) totalMX += A[x], mns.push(A[x]);
  m1[N - 1] = totalMX;
  for (int x = N; x < 3 * N; x++) {
    totalMX += A[x];
    mns.push(A[x]);
    totalMX -= mns.top();
    mns.pop();
    m1[x] = totalMX;
  }

  ll totalMN = 0, i = 3 * N - 1;
  FOR(x, N)
  totalMN += A[3 * N - 1 - x],
      mxs.push(A[3 * N - 1 - x]), i = 3 * N - 1 - x;
  m2[i] = totalMN;
  for (int x = i - 1; x >= 0; x--) {
    totalMN += A[x];
    mxs.push(A[x]);
    totalMN -= mxs.top();
    mxs.pop();
    m2[x] = totalMN;
  }

  ll ans = -(1LL << 62);
  for (int x = N - 1; x < i; x++)
    ans = max(ans, m1[x] - m2[x + 1]);

  cout << ans << "\n";
}
Problem p03716 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define DEBUG(x) cout << #x << " = " << x << endl
#define pb push_back
using namespace std;
typedef long long ll;

const int MAXN = 100005;
ll a[MAXN];
ll l[MAXN];
ll r[MAXN];
ll INF = 1e16;
multiset<ll> ms;

int main() {
  // freopen("input.txt", "r", stdin);
  int n;
  cin >> n;
  for (int i = 0; i < 3 * n; i++)
    cin >> a[i];

  ll sum = 0;
  for (int i = 0; i < n; i++) {
    ms.insert(a[i]);
    sum += a[i];
  }
  l[n - 1] = sum;

  for (int i = n; i < 2 * n; i++) {
    if (a[i] > *(ms.begin())) {
      sum -= *(ms.begin());
      ms.erase(ms.begin());
      sum += a[i];
      ms.insert(a[i]);
    }
    l[i] = sum;
  }

  ms.clear();
  sum = 0;
  for (int i = 2 * n; i < 3 * n; i++) {
    ms.insert(-a[i]);
    sum += a[i];
  }
  r[2 * n] = sum;

  for (int i = 2 * n - 1; i >= n; i--) {
    if (a[i] < -(*(ms.begin()))) {
      sum += *(ms.begin());
      ms.erase(ms.begin());
      sum += a[i];
      ms.insert(-a[i]);
    }
    r[i] = sum;
  }

  ll ans = -INF;
  for (int i = n - 1; i < 2 * n; i++) {
    ans = max(l[i] - r[i + 1], ans);
  }

  cout << ans;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define DEBUG(x) cout << #x << " = " << x << endl
#define pb push_back
using namespace std;
typedef long long ll;

const int MAXN = 400005;
ll a[MAXN];
ll l[MAXN];
ll r[MAXN];
ll INF = 1e16;
multiset<ll> ms;

int main() {
  // freopen("input.txt", "r", stdin);
  int n;
  cin >> n;
  for (int i = 0; i < 3 * n; i++)
    cin >> a[i];

  ll sum = 0;
  for (int i = 0; i < n; i++) {
    ms.insert(a[i]);
    sum += a[i];
  }
  l[n - 1] = sum;

  for (int i = n; i < 2 * n; i++) {
    if (a[i] > *(ms.begin())) {
      sum -= *(ms.begin());
      ms.erase(ms.begin());
      sum += a[i];
      ms.insert(a[i]);
    }
    l[i] = sum;
  }

  ms.clear();
  sum = 0;
  for (int i = 2 * n; i < 3 * n; i++) {
    ms.insert(-a[i]);
    sum += a[i];
  }
  r[2 * n] = sum;

  for (int i = 2 * n - 1; i >= n; i--) {
    if (a[i] < -(*(ms.begin()))) {
      sum += *(ms.begin());
      ms.erase(ms.begin());
      sum += a[i];
      ms.insert(-a[i]);
    }
    r[i] = sum;
  }

  ll ans = -INF;
  for (int i = n - 1; i < 2 * n; i++) {
    ans = max(l[i] - r[i + 1], ans);
  }

  cout << ans;
  return 0;
}
Problem p03716 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using vi = vector<int>;
using db = double;
#define pb push_back

const int mxn = 100006;

int n;
int a[mxn];

struct {
  multiset<int> H, L;
  ll s = 0;
  ll max() { return s; }
  void add(int x) {
    H.insert(x);
    s += x;
    while (H.size() > n) {
      s -= *H.begin();
      L.insert(*H.begin());
      H.erase(H.begin());
    }
  }
  void rem(int x) {
    if (L.find(x) != L.end()) {
      L.erase(L.find(x));
    } else {
      H.erase(H.find(x));
      s -= x;
      if (!L.empty()) {
        H.insert(*L.rbegin());
        s += *L.rbegin();
        L.erase(--L.end());
      }
    }
  }
} A, B;

int main() {
  cin >> n;
  for (int i = 1; i <= n * 3; ++i)
    scanf("%d", &a[i]);

  for (int i = 1; i <= n; ++i)
    A.add(a[i]);
  for (int i = n + 1; i <= n * 3; ++i)
    B.add(-a[i]);

  ll s = -LLONG_MAX;
  for (int i = n; i <= n * 2; ++i) {
    s = max(s, A.max() - -B.max());
    // cout << A.max() << ' ' << -B.max() << endl;
    if (i + 1 <= n * 2)
      A.add(a[i + 1]), B.rem(-a[i + 1]);
  }

  cout << s << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using vi = vector<int>;
using db = double;
#define pb push_back

const int mxn = 100006 * 3;

int n;
int a[mxn];

struct {
  multiset<int> H, L;
  ll s = 0;
  ll max() { return s; }
  void add(int x) {
    H.insert(x);
    s += x;
    while (H.size() > n) {
      s -= *H.begin();
      L.insert(*H.begin());
      H.erase(H.begin());
    }
  }
  void rem(int x) {
    if (L.find(x) != L.end()) {
      L.erase(L.find(x));
    } else {
      H.erase(H.find(x));
      s -= x;
      if (!L.empty()) {
        H.insert(*L.rbegin());
        s += *L.rbegin();
        L.erase(--L.end());
      }
    }
  }
} A, B;

int main() {
  cin >> n;
  for (int i = 1; i <= n * 3; ++i)
    scanf("%d", &a[i]);

  for (int i = 1; i <= n; ++i)
    A.add(a[i]);
  for (int i = n + 1; i <= n * 3; ++i)
    B.add(-a[i]);

  ll s = -LLONG_MAX;
  for (int i = n; i <= n * 2; ++i) {
    s = max(s, A.max() - -B.max());
    // cout << A.max() << ' ' << -B.max() << endl;
    if (i + 1 <= n * 2)
      A.add(a[i + 1]), B.rem(-a[i + 1]);
  }

  cout << s << endl;

  return 0;
}
Problem p03716 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL;
const int maxn = 1e5 + 20;
int arr[maxn];
LL last[maxn], per[maxn];
priority_queue<int> que;
priority_queue<int, vector<int>, greater<int>> qq;
void work() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= 3 * n; ++i) {
    scanf("%d", arr + i);
  }
  LL sum = 0;
  for (int i = 3 * n; i >= 2 * n + 1; --i) {
    last[i] = (long long int)1e18;
    sum += arr[i];
    que.push(arr[i]);
  }
  last[2 * n + 1] = sum;
  for (int i = 2 * n; i >= 1; --i) {
    if (arr[i] >= que.top()) {
      last[i] = sum;
    } else {
      sum -= que.top();
      que.pop();
      sum += arr[i];
      que.push(arr[i]);
      last[i] = sum;
    }
  }
  //    for (int i = 1; i <= 3 * n; ++i) {
  //        printf("%d ", last[i]);
  //    }
  sum = 0;
  for (int i = 1; i <= n; ++i) {
    sum += arr[i];
    qq.push(arr[i]);
    per[i] = (long long int)-1e18;
  }
  per[n] = sum;
  for (int i = n + 1; i <= 3 * n; ++i) {
    if (arr[i] <= qq.top()) {
      per[i] = sum;
    } else {
      sum -= qq.top();
      qq.pop();
      qq.push(arr[i]);
      sum += arr[i];
      per[i] = sum;
    }
  }
  LL ans = -2e18;
  for (int i = 1; i <= 3 * n - 1; ++i) {
    ans = max(ans, per[i] - last[i + 1]);
  }
  cout << ans << endl;
}

int main() {
#ifdef local
  freopen("data.txt", "r", stdin);
//    freopen("data.txt", "w", stdout);
#endif
  work();
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL;
const int maxn = 3e5 + 20;
int arr[maxn];
LL last[maxn], per[maxn];
priority_queue<int> que;
priority_queue<int, vector<int>, greater<int>> qq;
void work() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= 3 * n; ++i) {
    scanf("%d", arr + i);
  }
  LL sum = 0;
  for (int i = 3 * n; i >= 2 * n + 1; --i) {
    last[i] = (long long int)1e18;
    sum += arr[i];
    que.push(arr[i]);
  }
  last[2 * n + 1] = sum;
  for (int i = 2 * n; i >= 1; --i) {
    if (arr[i] >= que.top()) {
      last[i] = sum;
    } else {
      sum -= que.top();
      que.pop();
      sum += arr[i];
      que.push(arr[i]);
      last[i] = sum;
    }
  }
  //    for (int i = 1; i <= 3 * n; ++i) {
  //        printf("%d ", last[i]);
  //    }
  sum = 0;
  for (int i = 1; i <= n; ++i) {
    sum += arr[i];
    qq.push(arr[i]);
    per[i] = (long long int)-1e18;
  }
  per[n] = sum;
  for (int i = n + 1; i <= 3 * n; ++i) {
    if (arr[i] <= qq.top()) {
      per[i] = sum;
    } else {
      sum -= qq.top();
      qq.pop();
      qq.push(arr[i]);
      sum += arr[i];
      per[i] = sum;
    }
  }
  LL ans = -2e18;
  for (int i = 1; i <= 3 * n - 1; ++i) {
    ans = max(ans, per[i] - last[i + 1]);
  }
  cout << ans << endl;
}

int main() {
#ifdef local
  freopen("data.txt", "r", stdin);
//    freopen("data.txt", "w", stdout);
#endif
  work();
  return 0;
}
Problem p03716 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>
#define sqr(x) (x) * (x)
#define ll long long
using namespace std;
ll n, m, num[100005], ans = -10000000000000, i, j, l[100005], r[100005];
priority_queue<ll> ri;
priority_queue<ll, vector<ll>, greater<ll> /**/> le;
int main() {
  cin >> n;
  for (i = 1; i <= n * 3; i++) {
    cin >> num[i];
    if (i <= n) {
      le.push(num[i]);
      l[n] += num[i];
    }
    if (i > n * 2) {
      ri.push(num[i]);
      r[n + n + 1] += num[i];
    }
  }
  for (i = n + 1; i <= n + n; i++) {
    l[i] = l[i - 1];
    if (le.top() < num[i]) {
      l[i] = l[i] - le.top() + num[i];
      le.pop();
      le.push(num[i]);
    }
  }
  for (i = n + n; i > n; i--) {
    r[i] = r[i + 1];
    if (ri.top() > num[i]) {
      r[i] = r[i] - ri.top() + num[i];
      ri.pop();
      ri.push(num[i]);
    }
  }
  for (i = n; i <= n + n; i++) {
    ans = max(ans, l[i] - r[i + 1]);
  }
  cout << ans;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>
#define sqr(x) (x) * (x)
#define ll long long
using namespace std;
ll n, num[300005], ans = -1152921504606846976, i, j, l[300005], r[300005];
priority_queue<ll> ri;
priority_queue<ll, vector<ll>, greater<ll> /**/> le;
int main() {
  cin >> n;
  for (i = 1; i <= n * 3; i++) {
    cin >> num[i];
    if (i <= n) {
      le.push(num[i]);
      l[n] += num[i];
    }
    if (i > n * 2) {
      ri.push(num[i]);
      r[n + n + 1] += num[i];
    }
  }
  for (i = n + 1; i <= n + n; i++) {
    l[i] = l[i - 1];
    if (le.top() < num[i]) {
      l[i] = l[i] - le.top() + num[i];
      le.pop();
      le.push(num[i]);
    }
  }
  for (i = n + n; i > n; i--) {
    r[i] = r[i + 1];
    if (ri.top() > num[i]) {
      r[i] = r[i] - ri.top() + num[i];
      ri.pop();
      ri.push(num[i]);
    }
  }
  for (i = n; i <= n + n; i++) {
    ans = max(ans, l[i] - r[i + 1]);
  }
  cout << ans;
  return 0;
}
Problem p03716 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
/*
前半と後半の分かれ目があいまい
123456
2個とる
12
34-56

56
12-34
前半で小さいのを除く
後半の大きいのを除く

DPかな
192837465

max-minが大きいほど効果的？
DP[3]=876-129
min3つ 129
max3つ 876
DP[4] 765-128
8
setなら早いかな
123456789
123 789確定

3
8 2 2 7 4 6 5 3 8
228 358
278
478
678 358

12345678oo
123 78o
234
345
対立箇所が最後だけになっちゃう
Nとおり全探索できる？sort?
set?
setの総和も出しとくとか
63
54
45
36

123456 789
3が境目
12345 6789
除いたのが6>=3 でスタメンなので3が候補入り
2が補欠

456 789
上位N個、下位N個だけおさえられればいい
setにいれて、最下をのぞけばいいとか
入った場合と除いた場合との差分だけとればいいか

*/
int main() {
  ll N, x = 0, y = 0, ans = -1e16;
  cin >> N;
  ll N2 = N * 2, N3 = N * 3;
  multiset<ll> Z1, Z2;
  vector<ll> A(N + 10, 0), X(N + 10, 0), Y(N + 10, 0);
  for (ll i = 0; i < N3; i++)
    cin >> A[i];
  // 初期セット
  for (ll i = 0; i < N; i++) {
    ll j = N3 - 1 - i;
    x += A[i];
    y += A[j];
    Z1.insert(A[i]);
    Z2.insert(A[j]);
    X[i] = x;
    Y[j] = y;
  }
  for (ll i = N; i < N2; i++) {
    ll j = N3 - 1 - i;
    x += A[i];
    y += A[j];
    Z1.insert(A[i]);
    Z2.insert(A[j]);
    auto IT1 = Z1.begin(), IT2 = Z2.end();
    IT2--;
    x -= *IT1; // 前半は最小を削除
    y -= *IT2; // 後半は最大を削除
    X[i] = x;
    Y[j] = y;
    Z1.erase(IT1);
    Z2.erase(IT2);
  }
  // ans
  for (ll i = N - 1; i < N2; i++) {
    ll tmp = X[i] - Y[i + 1];
    // cout<<"X["<<i<<"]:"<<X[i]<<" Y["<<i+1<<"]:"<<Y[i+1]<<endl;
    ans = max(ans, tmp);
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
/*
前半と後半の分かれ目があいまい
123456
2個とる
12
34-56

56
12-34
前半で小さいのを除く
後半の大きいのを除く

DPかな
192837465

max-minが大きいほど効果的？
DP[3]=876-129
min3つ 129
max3つ 876
DP[4] 765-128
8
setなら早いかな
123456789
123 789確定

3
8 2 2 7 4 6 5 3 8
228 358
278
478
678 358

12345678oo
123 78o
234
345
対立箇所が最後だけになっちゃう
Nとおり全探索できる？sort?
set?
setの総和も出しとくとか
63
54
45
36

123456 789
3が境目
12345 6789
除いたのが6>=3 でスタメンなので3が候補入り
2が補欠

456 789
上位N個、下位N個だけおさえられればいい
setにいれて、最下をのぞけばいいとか
入った場合と除いた場合との差分だけとればいいか

*/
int main() {
  ll N, x = 0, y = 0, ans = -1e16;
  cin >> N;
  ll N2 = N * 2, N3 = N * 3;
  multiset<ll> Z1, Z2;
  vector<ll> A(N3 + 10, 0), X(N3 + 10, 0), Y(N3 + 10, 0);
  for (ll i = 0; i < N3; i++)
    cin >> A[i];
  // 初期セット
  for (ll i = 0; i < N; i++) {
    ll j = N3 - 1 - i;
    x += A[i];
    y += A[j];
    Z1.insert(A[i]);
    Z2.insert(A[j]);
    X[i] = x;
    Y[j] = y;
  }
  for (ll i = N; i < N2; i++) {
    ll j = N3 - 1 - i;
    x += A[i];
    y += A[j];
    Z1.insert(A[i]);
    Z2.insert(A[j]);
    auto IT1 = Z1.begin(), IT2 = Z2.end();
    IT2--;
    x -= *IT1; // 前半は最小を削除
    y -= *IT2; // 後半は最大を削除
    X[i] = x;
    Y[j] = y;
    Z1.erase(IT1);
    Z2.erase(IT2);
  }
  // ans
  for (ll i = N - 1; i < N2; i++) {
    ll tmp = X[i] - Y[i + 1];
    // cout<<"X["<<i<<"]:"<<X[i]<<" Y["<<i+1<<"]:"<<Y[i+1]<<endl;
    ans = max(ans, tmp);
  }
  cout << ans << endl;
}
Problem p03716 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n, m) for (int i = (n); i < (m); i++)
#define rrep(i, n, m) for (int i = (n)-1; i >= (m); i--)
using namespace std;
using ll = long long;

int main() {
  int n;
  cin >> n;
  vector<int> a(3 * n);
  rep(i, 0, 3 * n) cin >> a[i];

  priority_queue<int, vector<int>, greater<int>> left;
  priority_queue<int, vector<int>, less<int>> right;

  vector<ll> ans(3 * n, 0);
  ll sum = 0;
  rep(pos, 0, 2 * n) {
    ll new_a = a[pos];
    left.push(new_a);
    sum += new_a;
    if (pos >= n) {
      ll old_a = left.top();
      left.pop();
      sum -= old_a;
      // cout << pos << ' ' << old_a << ' ' << new_a << ' ' << sum << endl;
    }
    ans[pos] += sum;
  }
  sum = 0;
  rrep(pos, 3 * n, n - 1) {
    ll new_a = a[pos];
    right.push(new_a);
    sum += new_a;

    if (pos < 2 * n) {
      ll old_a = right.top();
      right.pop();
      sum -= old_a;
      // cout << pos << '*' << old_a << ' ' << new_a << ' ' << sum << endl;
    }
    ans[pos - 1] -= sum;
  }

  // cout << "******" << endl;
  ll max_ans = ans[n - 1];
  rep(pos, n - 1, 2 * n)
      // cout << pos << ' ' << ans[pos] << endl;
      max_ans = max(max_ans, ans[pos]);
  cout << max_ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n, m) for (int i = (n); i < (m); i++)
#define rrep(i, n, m) for (int i = (n)-1; i >= (m); i--)
using namespace std;
using ll = long long;

int main() {
  int n;
  cin >> n;
  vector<int> a(3 * n);
  rep(i, 0, 3 * n) cin >> a[i];

  priority_queue<int, vector<int>, greater<int>> left;
  priority_queue<int, vector<int>, less<int>> right;

  vector<ll> ans(3 * n, 0);
  ll sum = 0;
  rep(pos, 0, 2 * n) {
    ll new_a = a[pos];
    left.push(new_a);
    sum += new_a;
    if (pos >= n) {
      ll old_a = left.top();
      left.pop();
      sum -= old_a;
      // cout << pos << ' ' << old_a << ' ' << new_a << ' ' << sum << endl;
    }
    ans[pos] += sum;
  }
  sum = 0;
  rrep(pos, 3 * n, n - 1) {
    ll new_a = a[pos];
    right.push(new_a);
    sum += new_a;

    if (pos < 2 * n) {
      ll old_a = right.top();
      right.pop();
      sum -= old_a;
      // cout << pos << '*' << old_a << ' ' << new_a << ' ' << sum << endl;
    }
    if (pos - 1 >= 0)
      ans[pos - 1] -= sum;
  }

  // cout << "******" << endl;
  ll max_ans = ans[n - 1];
  rep(pos, n - 1, 2 * n)
      // cout << pos << ' ' << ans[pos] << endl;
      max_ans = max(max_ans, ans[pos]);
  cout << max_ans << endl;
  return 0;
}
Problem p03716 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define MN 100010
using namespace std;
typedef long long LL;
int n, a[MN];
LL sum, ans = -(1e16 + 7), l[MN], r[MN];
priority_queue<int, vector<int>, greater<int>> pq;
priority_queue<int, vector<int>, less<int>> pq2;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= 3 * n; i++)
    scanf("%d", &a[i]);
  sum = 0;
  for (int i = 1; i <= 3 * n; i++) {
    sum += a[i];
    pq.push(a[i]);
    while (pq.size() > n) {
      sum -= pq.top();
      pq.pop();
    }
    l[i] = sum;
  }
  sum = 0;
  for (int i = 3 * n; i >= 1; i--) {
    sum += a[i];
    pq2.push(a[i]);
    while (pq2.size() > n) {
      sum -= pq2.top();
      pq2.pop();
    }
    r[i] = sum;
  }
  for (int i = n; i <= 2 * n; i++) {
    ans = max(ans, l[i] - r[i + 1]);
  }
  printf("%lld\n", ans);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define MN 100010
using namespace std;
typedef long long LL;
int n, a[3 * MN];
LL sum, ans = -(1e16 + 7), l[3 * MN], r[3 * MN];
priority_queue<int, vector<int>, greater<int>> pq;
priority_queue<int, vector<int>, less<int>> pq2;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= 3 * n; i++)
    scanf("%d", &a[i]);
  sum = 0;
  for (int i = 1; i <= 3 * n; i++) {
    sum += a[i];
    pq.push(a[i]);
    while (pq.size() > n) {
      sum -= pq.top();
      pq.pop();
    }
    l[i] = sum;
  }
  sum = 0;
  for (int i = 3 * n; i >= 1; i--) {
    sum += a[i];
    pq2.push(a[i]);
    while (pq2.size() > n) {
      sum -= pq2.top();
      pq2.pop();
    }
    r[i] = sum;
  }
  for (int i = n; i <= 2 * n; i++) {
    ans = max(ans, l[i] - r[i + 1]);
  }
  printf("%lld\n", ans);
  return 0;
}
Problem p03716 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
int n;
ll a[100010];
ll f[100010];
ll ans;
ll ret;
priority_queue<ll> q;

int main() {
  ios::sync_with_stdio(false);

  cin >> n;
  for (int i = 1; i <= 3 * n; i++)
    cin >> a[i];

  for (int i = 1; i <= n; i++) {
    q.push(-a[i]);
    ans += a[i];
    f[i] = ans;
  }
  for (int i = n + 1; i <= 2 * n; i++) {
    if (a[i] > -q.top()) {
      ans += a[i] + q.top();
      q.pop();
      q.push(-a[i]);
    }
    f[i] = ans;
  }

  while (!q.empty())
    q.pop();
  ans = 0;
  for (int i = 3 * n; i > 2 * n; i--) {
    q.push(a[i]);
    ans += a[i];
  }
  ret = f[2 * n] - ans;
  for (int i = 2 * n; i > n; i--) {
    if (a[i] < q.top()) {
      ans -= q.top();
      ans += a[i];
      q.pop();
      q.push(a[i]);
    }
    ret = max(ret, f[i - 1] - ans);
  }
  cout << ret;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
int n;
ll a[300010];
ll f[300010];
ll ans;
ll ret;
priority_queue<ll> q;

int main() {
  ios::sync_with_stdio(false);

  cin >> n;
  for (int i = 1; i <= 3 * n; i++)
    cin >> a[i];

  for (int i = 1; i <= n; i++) {
    q.push(-a[i]);
    ans += a[i];
    f[i] = ans;
  }
  for (int i = n + 1; i <= 2 * n; i++) {
    if (a[i] > -q.top()) {
      ans += a[i] + q.top();
      q.pop();
      q.push(-a[i]);
    }
    f[i] = ans;
  }

  while (!q.empty())
    q.pop();
  ans = 0;
  for (int i = 3 * n; i > 2 * n; i--) {
    q.push(a[i]);
    ans += a[i];
  }
  ret = f[2 * n] - ans;
  for (int i = 2 * n; i > n; i--) {
    if (a[i] < q.top()) {
      ans -= q.top();
      ans += a[i];
      q.pop();
      q.push(a[i]);
    }
    ret = max(ret, f[i - 1] - ans);
  }
  cout << ret;
}
Problem p03716 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>

using namespace std;

priority_queue<int, vector<int>, greater<int>> que;
priority_queue<int> que2;

long long int A1[200000], A2[200000], a[200000];

int main() {

  int N;

  cin >> N;

  for (int i = 0; i < 3 * N; i++) {
    cin >> a[i];
  }

  long long int sum = 0;

  for (int i = 0; i < N; i++) {
    A1[i] = 0;
    sum += a[i];
    que.push(a[i]);
  }

  A1[N - 1] = sum;

  for (int i = N; i < 3 * N; i++) {
    if (a[i] > que.top()) {
      sum += a[i] - que.top();
      que.pop();
      que.push(a[i]);
    }
    A1[i] = sum;
  }

  sum = 0;

  for (int i = 3 * N - 1; i >= 2 * N; i--) {
    A2[i] = 0;
    sum += a[i];
    que2.push(a[i]);
  }

  A2[2 * N] = sum;

  for (int i = 2 * N - 1; i >= 0; i--) {
    if (a[i] < que2.top()) {
      sum += a[i] - que2.top();
      que2.pop();
      que2.push(a[i]);
    }
    A2[i] = sum;
  }
  /*
  for(int i = 0; i < 3 * N; i++){
          cout << A1[i] << " ";
  }
  cout << endl;

  for(int i = 0; i < 3 * N; i++){
          cout << A2[i] << " ";
  }
  cout << endl;
  */
  long long int ans = -1000000000000000000;

  for (int i = N - 1; i < 2 * N; i++) {
    ans = max(ans, A1[i] - A2[i + 1]);
  }

  cout << ans << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>

using namespace std;

priority_queue<int, vector<int>, greater<int>> que;
priority_queue<int> que2;

long long int A1[500000], A2[500000], a[500000];

int main() {

  int N;

  cin >> N;

  for (int i = 0; i < 3 * N; i++) {
    cin >> a[i];
  }

  long long int sum = 0;

  for (int i = 0; i < N; i++) {
    A1[i] = 0;
    sum += a[i];
    que.push(a[i]);
  }

  A1[N - 1] = sum;

  for (int i = N; i < 3 * N; i++) {
    if (a[i] > que.top()) {
      sum += a[i] - que.top();
      que.pop();
      que.push(a[i]);
    }
    A1[i] = sum;
  }

  sum = 0;

  for (int i = 3 * N - 1; i >= 2 * N; i--) {
    A2[i] = 0;
    sum += a[i];
    que2.push(a[i]);
  }

  A2[2 * N] = sum;

  for (int i = 2 * N - 1; i >= 0; i--) {
    if (a[i] < que2.top()) {
      sum += a[i] - que2.top();
      que2.pop();
      que2.push(a[i]);
    }
    A2[i] = sum;
  }
  /*
  for(int i = 0; i < 3 * N; i++){
          cout << A1[i] << " ";
  }
  cout << endl;

  for(int i = 0; i < 3 * N; i++){
          cout << A2[i] << " ";
  }
  cout << endl;
  */
  long long int ans = -1000000000000000000;

  for (int i = N - 1; i < 2 * N; i++) {
    ans = max(ans, A1[i] - A2[i + 1]);
  }

  cout << ans << endl;

  return 0;
}
Problem p03716 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

typedef pair<int, int> P;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(c) (c).begin(), (c).end()
#define uniq(c) c.erase(unique(all(c)), (c).end())
#define _1 first
#define _2 second
#define pb push_back
#define INF (1LL << 60)
#define MOD 1000000007

int N;
int A[100000];
long long L[300000], R[300000];

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> N;
  rep(i, 3 * N) cin >> A[i];

  {
    priority_queue<int, vector<int>, greater<int>> pq;
    long long s = 0;
    for (int i = 0; i < 3 * N; i++) {
      s += A[i];
      pq.push(A[i]);
      if (pq.size() > N) {
        s -= pq.top();
        pq.pop();
      }
      if (pq.size() == N)
        L[i] = s;
      else
        L[i] = -INF;
    }
  }
  {
    priority_queue<int> pq;
    long long s = 0;
    for (int i = 3 * N - 1; i >= 0; i--) {
      if (pq.size() == N)
        R[i] = s;
      else
        R[i] = -INF;

      s += A[i];
      pq.push(A[i]);
      if (pq.size() > N) {
        s -= pq.top();
        pq.pop();
      }
    }
  }
  long long m = -INF;
  rep(i, 3 * N) {
    long long l = L[i], r = R[i];
    if (l == -INF || r == -INF)
      continue;
    m = max(m, l - r);
  }
  cout << m << "\n";
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

typedef pair<int, int> P;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(c) (c).begin(), (c).end()
#define uniq(c) c.erase(unique(all(c)), (c).end())
#define _1 first
#define _2 second
#define pb push_back
#define INF (1LL << 60)
#define MOD 1000000007

int N;
int A[400000];
long long L[400000], R[400000];

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> N;
  rep(i, 3 * N) cin >> A[i];

  {
    priority_queue<int, vector<int>, greater<int>> pq;
    long long s = 0;
    for (int i = 0; i < 3 * N; i++) {
      s += A[i];
      pq.push(A[i]);
      if (pq.size() > N) {
        s -= pq.top();
        pq.pop();
      }
      if (pq.size() == N)
        L[i] = s;
      else
        L[i] = -INF;
    }
  }
  {
    priority_queue<int> pq;
    long long s = 0;
    for (int i = 3 * N - 1; i >= 0; i--) {
      if (pq.size() == N)
        R[i] = s;
      else
        R[i] = -INF;

      s += A[i];
      pq.push(A[i]);
      if (pq.size() > N) {
        s -= pq.top();
        pq.pop();
      }
    }
  }
  long long m = -INF;
  rep(i, 3 * N) {
    long long l = L[i], r = R[i];
    if (l == -INF || r == -INF)
      continue;
    m = max(m, l - r);
  }
  cout << m << "\n";
  return 0;
}