Problem p03653 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
using uint = unsigned int;
using ll = long long;
const int M = 1e9 + 7;
const ll MLL = 1e18L + 9;
#pragma unused(M)
#pragma unused(MLL)
#ifdef LOCAL
#include "rprint.hpp"
#else
template <ostream &out = cout, class... T> void prints(T &&...) {}
template <ostream &out = cout, class... T> void printd(T &&...) {}
template <ostream &out = cout, class... T> void printde(T &&...) {}
template <ostream &out = cout, class... T> void printb(T &&...) {}
template <ostream &out = cout, class... T> void printArr(T &&...) {}
#endif

struct EBase {
  ll a, b, c;
  EBase() {}
  template <class E> EBase(E e) : a(e.a), b(e.b), c(e.c) {}
  virtual operator ll() const = 0;
  bool operator<(const EBase &e) const { return ll(*this) > e; }
};
template <ll EBase::*l, ll EBase::*r> struct ED : EBase {
  template <class... E> ED(E... e) : EBase(e...) {}
  operator ll() const { return this->*l - this->*r; }
};
using EXY = ED<&EBase::b, &EBase::a>;
using EXZ = ED<&EBase::c, &EBase::a>;
using EYX = ED<&EBase::a, &EBase::b>;
using EYZ = ED<&EBase::c, &EBase::b>;
using EZX = ED<&EBase::a, &EBase::c>;
using EZY = ED<&EBase::b, &EBase::c>;

int main() {
  int x, y, z;
  cin >> x >> y >> z;
  multiset<EXY> xys;
  multiset<EXZ> xzs;
  multiset<EYX> yxs;
  multiset<EYZ> yzs;
  multiset<EZX> zxs;
  multiset<EZY> zys;
  EXY e;
  for (int i = 0; i < x; i++) {
    cin >> e.a >> e.b >> e.c;
    xys.insert(e);
    xzs.insert(e);
  }
  for (int i = 0; i < y; i++) {
    cin >> e.a >> e.b >> e.c;
    yzs.insert(e);
    yxs.insert(e);
  }
  for (int i = 0; i < z; i++) {
    cin >> e.a >> e.b >> e.c;
    zxs.insert(e);
    zys.insert(e);
  }
  auto f = [](auto &v1, auto &v2, auto &v11, auto &v22) {
    bool ret = false;
    auto a = *v1.begin();
    auto b = *v2.begin();
    while ((ll)a + b > 0) {
      ret = true;
      v1.erase(a);
      v11.erase(a);
      v2.erase(b);
      v22.erase(b);
      v1.insert(b);
      v11.insert(b);
      v2.insert(a);
      v22.insert(a);
      a = *v1.begin();
      b = *v2.begin();
    }
    return ret;
  };
  auto f3 = [](auto &v1, auto &v2, auto &v3, auto &v11, auto &v22, auto &v33) {
    bool ret = false;
    auto a = *v1.begin();
    auto b = *v2.begin();
    auto c = *v3.begin();
    while ((ll)a + b + c > 0) {
      ret = true;
      v1.erase(a);
      v11.erase(a);
      v2.erase(b);
      v22.erase(b);
      v3.erase(c);
      v33.erase(c);
      v1.insert(c);
      v11.insert(c);
      v2.insert(a);
      v22.insert(a);
      v3.insert(b);
      v33.insert(b);
      a = *v1.begin();
      b = *v2.begin();
      c = *v3.begin();
    }
    return ret;
  };
  bool cont = true;
  while (cont) {
    cont = false;
    cont |= f(xys, yxs, xzs, yzs);
    cont |= f(yzs, zys, yxs, zxs);
    cont |= f(zxs, xzs, zys, xys);
    cont |= f3(xys, yzs, zxs, xzs, yxs, zys);
    cont |= f3(xzs, zys, yxs, xys, zxs, yzs);
  }
  ll ans = 0;
  for (auto xy : xys) {
    ans += xy.a;
  }
  for (auto yz : yzs) {
    ans += yz.b;
  }
  for (auto zx : zxs) {
    ans += zx.c;
  }
  cout << ans << '\n';
  for (auto yx : yxs) {
    ans -= yx.b;
  }
  for (auto zy : zys) {
    ans -= zy.c;
  }
  for (auto xz : xzs) {
    ans -= xz.a;
  }
  if (ans) {
    return 1;
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
using uint = unsigned int;
using ll = long long;
const int M = 1e9 + 7;
const ll MLL = 1e18L + 9;
#pragma unused(M)
#pragma unused(MLL)
#ifdef LOCAL
#include "rprint.hpp"
#else
template <ostream &out = cout, class... T> void prints(T &&...) {}
template <ostream &out = cout, class... T> void printd(T &&...) {}
template <ostream &out = cout, class... T> void printde(T &&...) {}
template <ostream &out = cout, class... T> void printb(T &&...) {}
template <ostream &out = cout, class... T> void printArr(T &&...) {}
#endif

struct EBase {
  ll a, b, c;
  EBase() {}
  template <class E> EBase(E e) : a(e.a), b(e.b), c(e.c) {}
  virtual operator ll() const = 0;
  // bool operator < (const EBase& e) const { return ll(*this) > e; }
  bool operator<(const EBase &e) const {
    return ll(*this) != e ? ll(*this) > e
                          : make_tuple(a, b, c) < make_tuple(e.a, e.b, e.c);
  }
};
template <ll EBase::*l, ll EBase::*r> struct ED : EBase {
  template <class... E> ED(E... e) : EBase(e...) {}
  operator ll() const { return this->*l - this->*r; }
};
using EXY = ED<&EBase::b, &EBase::a>;
using EXZ = ED<&EBase::c, &EBase::a>;
using EYX = ED<&EBase::a, &EBase::b>;
using EYZ = ED<&EBase::c, &EBase::b>;
using EZX = ED<&EBase::a, &EBase::c>;
using EZY = ED<&EBase::b, &EBase::c>;

int main() {
  int x, y, z;
  cin >> x >> y >> z;
  multiset<EXY> xys;
  multiset<EXZ> xzs;
  multiset<EYX> yxs;
  multiset<EYZ> yzs;
  multiset<EZX> zxs;
  multiset<EZY> zys;
  EXY e;
  for (int i = 0; i < x; i++) {
    cin >> e.a >> e.b >> e.c;
    xys.insert(e);
    xzs.insert(e);
  }
  for (int i = 0; i < y; i++) {
    cin >> e.a >> e.b >> e.c;
    yzs.insert(e);
    yxs.insert(e);
  }
  for (int i = 0; i < z; i++) {
    cin >> e.a >> e.b >> e.c;
    zxs.insert(e);
    zys.insert(e);
  }
  auto f = [](auto &v1, auto &v2, auto &v11, auto &v22) {
    bool ret = false;
    auto a = *v1.begin();
    auto b = *v2.begin();
    while ((ll)a + b > 0) {
      ret = true;
      v1.erase(a);
      v11.erase(a);
      v2.erase(b);
      v22.erase(b);
      v1.insert(b);
      v11.insert(b);
      v2.insert(a);
      v22.insert(a);
      a = *v1.begin();
      b = *v2.begin();
    }
    return ret;
  };
  auto f3 = [](auto &v1, auto &v2, auto &v3, auto &v11, auto &v22, auto &v33) {
    bool ret = false;
    auto a = *v1.begin();
    auto b = *v2.begin();
    auto c = *v3.begin();
    while ((ll)a + b + c > 0) {
      ret = true;
      v1.erase(a);
      v11.erase(a);
      v2.erase(b);
      v22.erase(b);
      v3.erase(c);
      v33.erase(c);
      v1.insert(c);
      v11.insert(c);
      v2.insert(a);
      v22.insert(a);
      v3.insert(b);
      v33.insert(b);
      a = *v1.begin();
      b = *v2.begin();
      c = *v3.begin();
    }
    return ret;
  };
  bool cont = true;
  while (cont) {
    cont = false;
    cont |= f(xys, yxs, xzs, yzs);
    cont |= f(yzs, zys, yxs, zxs);
    cont |= f(zxs, xzs, zys, xys);
    cont |= f3(xys, yzs, zxs, xzs, yxs, zys);
    cont |= f3(xzs, zys, yxs, xys, zxs, yzs);
  }
  ll ans = 0;
  for (auto xy : xys) {
    ans += xy.a;
  }
  for (auto yz : yzs) {
    ans += yz.b;
  }
  for (auto zx : zxs) {
    ans += zx.c;
  }
  cout << ans << '\n';
  for (auto yx : yxs) {
    ans -= yx.b;
  }
  for (auto zy : zys) {
    ans -= zy.c;
  }
  for (auto xz : xzs) {
    ans -= xz.a;
  }
  if (ans) {
    return 1;
  }
  return 0;
}