Problem p03653 (C++)

Buggy submission (status: Runtime Error):
///****In the name of ALLAH, most Gracious, most Compassionate****//

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

typedef long long ll;
typedef pair<ll, ll> pii;

#define ALL(a) a.begin(), a.end()
#define FastIO                                                                 \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define IN freopen("input.txt", "r+", stdin)
#define OUT freopen("output.txt", "w+", stdout)

#define DBG(a)                                                                 \
  cerr << "line " << __LINE__ << " : " << #a << " --> " << (a) << endl
#define NL cout << endl

template <class T1, class T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  os << "{" << p.first << "," << p.second << "}";
  return os;
}

const ll N = 1e6 + 5;

ll lft[N];
ll rht[N];

ll a[N];
ll b[N];
ll c[N];

bool a_b(ll l, ll r) { return a[l] - b[l] >= a[r] - b[r]; }

int32_t main() {
  FastIO;
  ll x, y, z;
  cin >> x >> y >> z;
  ll n = x + y + z;
  vector<ll> p(n, 0);
  ll ans = 0;
  for (ll i = 0; i < n; i++) {
    cin >> a[i] >> b[i] >> c[i];
    p[i] = i;
  }
  sort(ALL(p), a_b);

  {
    priority_queue<ll> pq;
    ll now = 0, q = 0;
    for (ll i = 0; i < n; i++) {
      ll ii = p[i];
      now += c[ii];
      ll d = a[ii] - c[ii];
      q += d;
      pq.push(-d);
      if (pq.size() > x) {
        //                assert(pq.size());
        q += pq.top();
        pq.pop();
      }
      lft[i] = now + q;
    }
  }
  {
    priority_queue<ll> pq;
    ll now = 0, q = 0;
    for (ll i = n - 1; i >= 0; i--) {
      ll ii = p[i];
      now += c[ii];
      ll d = b[ii] - c[ii];
      q += d;
      pq.push(-d);
      if (pq.size() > y) {
        q += pq.top();
        pq.pop();
      }
      rht[i] = now + q;
    }
  }
  for (ll i = 0; i < n - 1; i++) {
    if (i + 1 >= x and n - i - 1 >= y)
      ans = max(ans, lft[i] + rht[i + 1]);
  }
  cout << ans << "\n";
}


Fixed submission:
///****In the name of ALLAH, most Gracious, most Compassionate****//

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

typedef long long ll;
typedef pair<ll, ll> pii;

#define ALL(a) a.begin(), a.end()
#define FastIO                                                                 \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define IN freopen("input.txt", "r+", stdin)
#define OUT freopen("output.txt", "w+", stdout)

#define DBG(a)                                                                 \
  cerr << "line " << __LINE__ << " : " << #a << " --> " << (a) << endl
#define NL cout << endl

template <class T1, class T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  os << "{" << p.first << "," << p.second << "}";
  return os;
}

const ll N = 1e6 + 5;

ll lft[N];
ll rht[N];

ll a[N];
ll b[N];
ll c[N];

bool a_b(ll l, ll r) { return a[l] - b[l] > a[r] - b[r]; }

int32_t main() {
  FastIO;
  ll x, y, z;
  cin >> x >> y >> z;
  ll n = x + y + z;
  vector<ll> p(n, 0);
  ll ans = 0;
  for (ll i = 0; i < n; i++) {
    cin >> a[i] >> b[i] >> c[i];
    p[i] = i;
  }
  sort(ALL(p), a_b);

  {
    priority_queue<ll> pq;
    ll now = 0, q = 0;
    for (ll i = 0; i < n; i++) {
      ll ii = p[i];
      now += c[ii];
      ll d = a[ii] - c[ii];
      q += d;
      pq.push(-d);
      if (pq.size() > x) {
        //                assert(pq.size());
        q += pq.top();
        pq.pop();
      }
      lft[i] = now + q;
    }
  }
  {
    priority_queue<ll> pq;
    ll now = 0, q = 0;
    for (ll i = n - 1; i >= 0; i--) {
      ll ii = p[i];
      now += c[ii];
      ll d = b[ii] - c[ii];
      q += d;
      pq.push(-d);
      if (pq.size() > y) {
        q += pq.top();
        pq.pop();
      }
      rht[i] = now + q;
    }
  }
  for (ll i = 0; i < n - 1; i++) {
    if (i + 1 >= x and n - i - 1 >= y)
      ans = max(ans, lft[i] + rht[i + 1]);
  }
  cout << ans << "\n";
}