Problem p03653 (C++)

Buggy submission (status: Runtime Error):
// Nurbakyt Madibek
// Look at my code! IT'S AWESOME

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define pb push_back
#define pp pop_back
#define f first
#define s second
#define mp make_pair
#define sz(a) (int)((a).size())
#ifdef _WIN32
#define I64 "%I64d"
#else
#define I64 "%lld"
#endif
#define fname "."

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef vector<pi> vi;
typedef vector<pi> vl;

const int inf = (int)1e9 + 123;
const ll infl = (ll)1e18 + 123;
const double eps = 1e-6;

const int MAX_N = (int)1e5 + 5;
const int mod = (int)1e9 + 7;

int x, y, z;
int n;
int a[MAX_N], b[MAX_N], c[MAX_N];
int p[MAX_N];

bool cmp(int x, int y) { return a[x] - b[x] >= a[y] - b[y]; }

ll pref[MAX_N], suff[MAX_N];

multiset<ll> q;

ll findBest() {
  ll now = 0;
  for (int i = 1; i <= n; i++) {
    q.insert(a[p[i]]);
    now += a[p[i]];
    if (sz(q) > x) {
      now -= *q.begin();
      q.erase(q.begin());
    }
    pref[i] = now;
  }
  q.clear();
  now = 0;
  for (int i = n; i > 0; i--) {
    q.insert(b[p[i]]);
    now += b[p[i]];
    if (sz(q) > y) {
      now -= *q.begin();
      q.erase(q.begin());
    }
    suff[i] = now;
  }
  ll maxi = -infl;
  for (int i = x; n - i >= y; i++) {
    maxi = max(maxi, pref[i] + suff[i + 1]);
  }
  return maxi;
}

int main() {
#ifdef DEBUG
  freopen("input.txt", "r", stdin);
#endif
  cin >> x >> y >> z;
  n = x + y + z;
  ll all = 0;
  for (int i = 1; i <= n; i++) {
    cin >> a[i] >> b[i] >> c[i];
    a[i] -= c[i], b[i] -= c[i];
    all += c[i];
    p[i] = i;
  }
  sort(p + 1, p + n + 1, &cmp);
  cout << all + findBest() << endl;
  return 0;
}


Fixed submission:
// Nurbakyt Madibek
// Look at my code! IT'S AWESOME

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define pb push_back
#define pp pop_back
#define f first
#define s second
#define mp make_pair
#define sz(a) (int)((a).size())
#ifdef _WIN32
#define I64 "%I64d"
#else
#define I64 "%lld"
#endif
#define fname "."

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef vector<pi> vi;
typedef vector<pi> vl;

const int inf = (int)1e9 + 123;
const ll infl = (ll)1e18 + 123;
const double eps = 1e-6;

const int MAX_N = (int)1e5 + 5;
const int mod = (int)1e9 + 7;

int x, y, z;
int n;
int a[MAX_N], b[MAX_N], c[MAX_N];
int p[MAX_N];

bool cmp(int x, int y) { return a[x] - b[x] > a[y] - b[y]; }

ll pref[MAX_N], suff[MAX_N];

multiset<ll> q;

ll findBest() {
  ll now = 0;
  for (int i = 1; i <= n; i++) {
    q.insert(a[p[i]]);
    now += a[p[i]];
    if (sz(q) > x) {
      now -= *q.begin();
      q.erase(q.begin());
    }
    pref[i] = now;
  }
  q.clear();
  now = 0;
  for (int i = n; i > 0; i--) {
    q.insert(b[p[i]]);
    now += b[p[i]];
    if (sz(q) > y) {
      now -= *q.begin();
      q.erase(q.begin());
    }
    suff[i] = now;
  }
  ll maxi = -infl;
  for (int i = x; n - i >= y; i++) {
    maxi = max(maxi, pref[i] + suff[i + 1]);
  }
  return maxi;
}

int main() {
#ifdef DEBUG
  freopen("input.txt", "r", stdin);
#endif
  cin >> x >> y >> z;
  n = x + y + z;
  ll all = 0;
  for (int i = 1; i <= n; i++) {
    cin >> a[i] >> b[i] >> c[i];
    a[i] -= c[i], b[i] -= c[i];
    all += c[i];
    p[i] = i;
  }
  sort(p + 1, p + n + 1, &cmp);
  cout << all + findBest() << endl;
  return 0;
}