Problem p03653 (C++)

Buggy submission (status: Runtime Error):
/* You can't connect the dots looking forward
   you can only connect them looking backwards. */

#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define ull unsigned long long
#define boost                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);                                                                 \
  cout.precision(10);                                                          \
  cout << fixed;
#define dbset(x)                                                               \
  for (int i = 0; i < x.size(); i++)                                           \
    cerr << x[i] << " ";                                                       \
  cerr << endl;
#define inf 1000000007
#define INF 1000000000000000000LL
#define PI 3.14159265358979323846
#define mod 1000000007
#define mod1 1000696969
#define flusz fflush(stdout);
#define VI vector<int>
#define VPI vector<pair<int, int>>
#define PII pair<int, int>
#define BIT bitset<N>
#define st first
#define nd second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define endl '\n'
#define REP(x, y) for (int x = 0; x < (y); ++x)
#define FOR(x, y, z) for (int x = y; x <= (z); ++x)
#define FORR(x, y, z) for (int x = y; x >= (z); --x)
using namespace std;

template <class TH> void _dbg(const char *sdbg, TH h) {
  cerr << sdbg << "=" << h << "\n";
}
template <class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
  while (*sdbg != ',')
    cerr << *sdbg++;
  cerr << "=" << h << ",";
  _dbg(sdbg + 1, a...);
}
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)

#define int long long
#define N 500007

int test;

int x, y, z;

int a[N], b[N], c[N];

int n;

int res, odp = -INF;

VI g;

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

multiset<int> s;

int t1[N], t2[N];

void solve() {
  cin >> x >> y >> z;

  n = x + y + z;

  FOR(i, 1, n) {
    cin >> a[i] >> b[i] >> c[i];

    res += c[i];

    a[i] -= c[i];
    b[i] -= c[i];

    g.pb(i);
  }

  // debug(res);

  sort(g.begin(), g.end(), cmp);

  int res1 = 0, res2 = 0;

  // for(auto it:g){
  //     cout << it << " ";
  // }

  REP(i, x) {
    s.insert(a[g[i]]);
    res1 += a[g[i]];
  }

  t1[x - 1] = res1;

  FOR(i, x, g.size() - 1) {
    auto it = s.begin();

    if (*it < a[g[i]]) {
      res1 += (a[g[i]] - *it);
      s.erase(it);
      s.insert(a[g[i]]);
    }

    t1[i] = res1;
  }

  s.clear();

  FORR(i, g.size() - 1, g.size() - y) {
    s.insert(b[g[i]]);
    res2 += b[g[i]];
  }

  odp = res2 + (y != n ? t1[g.size() - y - 1] : 0);

  FORR(i, g.size() - y - 1, x) {
    auto it = s.begin();

    if (*it < b[g[i]]) {
      res2 += (b[g[i]] - *it);
      s.erase(it);
      s.insert(b[g[i]]);
    }

    odp = max(odp, res2 + t1[i - 1]);

    // debug(t1[i-1],res2,i);
  }

  cout << res + odp;

  return;
}

int32_t main() {
  boost

      test = 1;

  while (test--) {
    solve();
  }

  return 0;
}

Fixed submission:
/* You can't connect the dots looking forward
   you can only connect them looking backwards. */

#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define ull unsigned long long
#define boost                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);                                                                 \
  cout.precision(10);                                                          \
  cout << fixed;
#define dbset(x)                                                               \
  for (int i = 0; i < x.size(); i++)                                           \
    cerr << x[i] << " ";                                                       \
  cerr << endl;
#define inf 1000000007
#define INF 1000000000000000000LL
#define PI 3.14159265358979323846
#define mod 1000000007
#define mod1 1000696969
#define flusz fflush(stdout);
#define VI vector<int>
#define VPI vector<pair<int, int>>
#define PII pair<int, int>
#define BIT bitset<N>
#define st first
#define nd second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define endl '\n'
#define REP(x, y) for (int x = 0; x < (y); ++x)
#define FOR(x, y, z) for (int x = y; x <= (z); ++x)
#define FORR(x, y, z) for (int x = y; x >= (z); --x)
using namespace std;

template <class TH> void _dbg(const char *sdbg, TH h) {
  cerr << sdbg << "=" << h << "\n";
}
template <class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
  while (*sdbg != ',')
    cerr << *sdbg++;
  cerr << "=" << h << ",";
  _dbg(sdbg + 1, a...);
}
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)

#define int long long
#define N 500007

int test;

int x, y, z;

int a[N], b[N], c[N];

int n;

int res, odp = -INF;

VI g;

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

multiset<int> s;

int t1[N], t2[N];

void solve() {
  cin >> x >> y >> z;

  n = x + y + z;

  FOR(i, 1, n) {
    cin >> a[i] >> b[i] >> c[i];

    res += c[i];

    a[i] -= c[i];
    b[i] -= c[i];

    g.pb(i);
  }

  // debug(res);

  sort(g.begin(), g.end(), cmp);

  int res1 = 0, res2 = 0;

  // for(auto it:g){
  //     cout << it << " ";
  // }

  REP(i, x) {
    s.insert(a[g[i]]);
    res1 += a[g[i]];
  }

  t1[x - 1] = res1;

  FOR(i, x, g.size() - 1) {
    auto it = s.begin();

    if (*it < a[g[i]]) {
      res1 += (a[g[i]] - *it);
      s.erase(it);
      s.insert(a[g[i]]);
    }

    t1[i] = res1;
  }

  s.clear();

  FORR(i, g.size() - 1, g.size() - y) {
    s.insert(b[g[i]]);
    res2 += b[g[i]];
  }

  odp = res2 + (y != n ? t1[g.size() - y - 1] : 0);

  FORR(i, g.size() - y - 1, x) {
    auto it = s.begin();

    if (*it < b[g[i]]) {
      res2 += (b[g[i]] - *it);
      s.erase(it);
      s.insert(b[g[i]]);
    }

    odp = max(odp, res2 + t1[i - 1]);

    // debug(t1[i-1],res2,i);
  }

  cout << res + odp;

  return;
}

int32_t main() {
  boost

      test = 1;

  while (test--) {
    solve();
  }

  return 0;
}