Problem p03653 (C++)

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

#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)
#define ford(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define ford1(i, n) for (int i = (int)(n); i >= 1; --i)
#define fored(i, a, b) for (int i = (int)(b); i >= (int)(a); --i)
#define sz(v) ((int)((v).size()))
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define clr(v) memset(v, 0, sizeof(v))
#define clr1(v) memset(v, 0xFF, sizeof(v))
#define mp make_pair
#define pb push_back
#define fi first
#define se second

using namespace std;

typedef pair<int, int> pii;
typedef vector<pii> vpi;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef long long i64;
typedef unsigned long long u64;
typedef long double ld;
typedef vector<ld> vld;

typedef vector<i64> vi64;

template <class T> bool uax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool uin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

void solve() {
  int x, y, z;
  cin >> x >> y >> z;
  vvi v(x + y + z, vi(3, 0));
  forn(i, v.size()) forn(j, 3) cin >> v[i][j];
  vpi u(x + y + z);
  i64 s_base = 0;
  forn(i, u.size()) {
    s_base += v[i][0];
    u[i] = mp(v[i][1] - v[i][0], v[i][2] - v[i][0]);
  }
  sort(rall(u));
  multiset<int> ML, MZ1, MZ2, MR;
  i64 sl = 0, sz1 = 0, sz2 = 0;
  // [0 .. i) -> y + z'
  // [i .. sz) -> z''
  forn(i, x + y + z) if (i < y) {
    sl += u[i].fi;
    ML.insert(u[i].se - u[i].fi);
  }
  else {
    MR.insert(u[i].se);
  }
  while (MZ2.size() < z) {
    auto it = MR.end();
    --it;
    sz2 += *it;
    MZ2.insert(*it);
    MR.erase(it);
  }
  // cerr << s_base << '\n';
  // forn(i, u.size()) cerr << u[i].fi << ' ' << u[i].se << '\n';

  i64 ans = sl + sz1 + sz2;
  // cerr << sl << ' ' << sz1 << ' ' << sz2 << '\n';
  forn(j, z) {
    auto e = u[y + j];
    // remove
    {
      auto it = MR.find(e.se);
      if (it == MR.end()) {
        sz2 -= e.se;
        MZ2.erase(MZ2.find(e.se));
      } else {
        MR.erase(e.se);
        auto it = MZ2.begin();
        sz2 -= *it;
        MR.insert(*it);
        MZ2.erase(it);
      }
    }
    // insert
    sl += e.fi;
    ML.insert(e.se - e.fi);
    {
      auto it = ML.end();
      --it;
      sz1 += *it;
      MZ1.insert(*it); // debug only
      ML.erase(it);
    }

    // cerr << sl << ' ' << sz1 << ' ' << sz2 << '\n';
    uax(ans, sl + sz1 + sz2);
  }
  cout << s_base + ans << '\n';
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout << fixed;
  cout.precision(15);
  cerr << fixed;
  cerr.precision(15);
#ifdef HOME
  freopen("input.txt", "r", stdin);
#endif
  solve();
  return 0;
}


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

#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)
#define ford(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define ford1(i, n) for (int i = (int)(n); i >= 1; --i)
#define fored(i, a, b) for (int i = (int)(b); i >= (int)(a); --i)
#define sz(v) ((int)((v).size()))
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define clr(v) memset(v, 0, sizeof(v))
#define clr1(v) memset(v, 0xFF, sizeof(v))
#define mp make_pair
#define pb push_back
#define fi first
#define se second

using namespace std;

typedef pair<int, int> pii;
typedef vector<pii> vpi;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef long long i64;
typedef unsigned long long u64;
typedef long double ld;
typedef vector<ld> vld;

typedef vector<i64> vi64;

template <class T> bool uax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool uin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

void solve() {
  int x, y, z;
  cin >> x >> y >> z;
  vvi v(x + y + z, vi(3, 0));
  forn(i, v.size()) forn(j, 3) cin >> v[i][j];
  vpi u(x + y + z);
  i64 s_base = 0;
  forn(i, u.size()) {
    s_base += v[i][0];
    u[i] = mp(v[i][1] - v[i][0], v[i][2] - v[i][0]);
  }
  sort(rall(u));
  multiset<int> ML, MZ1, MZ2, MR;
  i64 sl = 0, sz1 = 0, sz2 = 0;
  // [0 .. i) -> y + z'
  // [i .. sz) -> z''
  forn(i, x + y + z) if (i < y) {
    sl += u[i].fi;
    ML.insert(u[i].se - u[i].fi);
  }
  else {
    MR.insert(u[i].se);
  }
  while (MZ2.size() < z) {
    auto it = MR.end();
    --it;
    sz2 += *it;
    MZ2.insert(*it);
    MR.erase(it);
  }
  // cerr << s_base << '\n';
  // forn(i, u.size()) cerr << u[i].fi << ' ' << u[i].se << '\n';

  i64 ans = sl + sz1 + sz2;
  // cerr << sl << ' ' << sz1 << ' ' << sz2 << '\n';
  forn(j, z) {
    auto e = u[y + j];
    // remove
    {
      auto it = MR.find(e.se);
      if (it == MR.end()) {
        sz2 -= e.se;
        MZ2.erase(MZ2.find(e.se));
      } else {
        MR.erase(MR.find(e.se));
        auto it = MZ2.begin();
        sz2 -= *it;
        MR.insert(*it);
        MZ2.erase(it);
      }
    }
    // insert
    sl += e.fi;
    ML.insert(e.se - e.fi);
    {
      auto it = ML.end();
      --it;
      sz1 += *it;
      MZ1.insert(*it); // debug only
      ML.erase(it);
    }

    // cerr << sl << ' ' << sz1 << ' ' << sz2 << '\n';
    uax(ans, sl + sz1 + sz2);
  }
  cout << s_base + ans << '\n';
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout << fixed;
  cout.precision(15);
  cerr << fixed;
  cerr.precision(15);
#ifdef HOME
  freopen("input.txt", "r", stdin);
#endif
  solve();
  return 0;
}