Problem p03653 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

#define sz(x) (int)(x.size())
#define fr(i, a, b) for (int i = a; i < b; ++i)
#define fdr(i, a, b) for (int i = b - 1; i >= a; --i)
#define pb push_back
#define mp make_pair
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
/////////////////////

int const N = 1e2 + 41;
ll const INF = 1e9 * 1LL * 1e9 + 41;

priority_queue<int> q;

ll pr[N], suf[N];
pll d[N];
ll a[N], b[N], c[N];
int n, x, y, z;

void solve() {
  cin >> x >> y >> z;
  n = x + y + z;
  ll add = 0;

  fr(i, 0, n) {
    cin >> a[i] >> b[i] >> c[i];
    add += c[i];
    a[i] -= c[i];
    b[i] -= c[i];
    d[i] = mp(a[i] - b[i], i);
  }
  sort(d, d + n);

  fr(i, 0, N) pr[i] = suf[i] = -INF;
  ll s = 0;
  fr(i, 0, n) {
    ll id = d[i].second;
    s += b[id];
    q.push(-b[id]);
    if (i)
      pr[i] = pr[i - 1];
    if (sz(q) > y) {
      ll v = q.top();
      q.pop();
      s += v;
    }
    if (sz(q) >= y)
      pr[i] = s;
  }
  s = 0;
  while (!q.empty())
    q.pop();

  fdr(i, 0, n) {
    ll id = d[i].second;
    s += a[id];
    q.push(-a[id]);
    suf[i] = suf[i + 1];
    if (sz(q) > x) {
      ll v = q.top();
      q.pop();
      s += v;
    }
    if (sz(q) >= x)
      suf[i] = s;
  }

  ll ans = -(4LL * INF);
  fr(i, 0, n - 1) { ans = max(ans, pr[i] + suf[i + 1]); }
  cout << ans + add << endl;
}

int main() {
#ifdef _DEBUG
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  solve();

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

#define sz(x) (int)(x.size())
#define fr(i, a, b) for (int i = a; i < b; ++i)
#define fdr(i, a, b) for (int i = b - 1; i >= a; --i)
#define pb push_back
#define mp make_pair
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
/////////////////////

int const N = 1e5 + 41;
ll const INF = 1e9 * 1LL * 1e9 + 41;

priority_queue<int> q;

ll pr[N], suf[N];
pll d[N];
ll a[N], b[N], c[N];
int n, x, y, z;

void solve() {
  cin >> x >> y >> z;
  n = x + y + z;
  ll add = 0;

  fr(i, 0, n) {
    cin >> a[i] >> b[i] >> c[i];
    add += c[i];
    a[i] -= c[i];
    b[i] -= c[i];
    d[i] = mp(a[i] - b[i], i);
  }
  sort(d, d + n);

  fr(i, 0, N) pr[i] = suf[i] = -INF;
  ll s = 0;
  fr(i, 0, n) {
    ll id = d[i].second;
    s += b[id];
    q.push(-b[id]);
    if (i)
      pr[i] = pr[i - 1];
    if (sz(q) > y) {
      ll v = q.top();
      q.pop();
      s += v;
    }
    if (sz(q) >= y)
      pr[i] = s;
  }
  s = 0;
  while (!q.empty())
    q.pop();

  fdr(i, 0, n) {
    ll id = d[i].second;
    s += a[id];
    q.push(-a[id]);
    suf[i] = suf[i + 1];
    if (sz(q) > x) {
      ll v = q.top();
      q.pop();
      s += v;
    }
    if (sz(q) >= x)
      suf[i] = s;
  }

  ll ans = -(4LL * INF);
  fr(i, 0, n - 1) { ans = max(ans, pr[i] + suf[i + 1]); }
  cout << ans + add << endl;
}

int main() {
#ifdef _DEBUG
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  solve();

  return 0;
}