Problem p02692 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <chrono>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#define for0(i, N) for (long long i = 0; i < (N); ++i)
#define for1n(i, N) for (long long i = 1; i <= (N); ++i)
#define for1(i, N) for (long long i = 1; i < (N); ++i)
#define forn(i, N) for (long long i = 0; i <= (N); ++i)
#define forx(i, N, x) for (long long i = (x); i < (N); ++i)
#define forl(i, N, x, a)                                                       \\
  for (long long i = (x), a = 0; a < (N); i = (i + 1) % (N), ++a)
#define int long long

long double constexpr pi = 3.141592653589793;

template <class T> constexpr T sigma1(T i, T n) {
  return (n - i + 1) * (i + n) / 2;
}

template <class T> constexpr T sigma2(T i, T n) {
  return (i == 1 ? n * (n + 1) * (2 * n + 1) / 6
                 : sigma2(1, n) - sigma2(1, i - 1));
}

template <class T> constexpr T sigma3(T i, T n) {
  if (i == 1) {
    auto temp = sigma1(i, n);
    return temp * temp;
  }
  return sigma2(1, n) - sigma2(1, i - 1);
}

template <size_t i> std::bitset<i> operator++(std::bitset<i> b, signed) {
  static_assert(i <= 64, "Cannot cast 64bit int.");
  return b = (unsigned long long)b + 1;
}

template <size_t i> std::bitset<i> operator++(std::bitset<i> b) {
  static_assert(i <= 64, "Cannot cast 64bit int.");
  auto temp = b;
  b++;
  return temp;
}

template <size_t i> std::bitset<i> operator--(std::bitset<i> b, signed) {
  static_assert(i <= 64, "Cannot cast 64bit int.");
  return b = (unsigned long long)b - 1;
}

template <size_t i> std::bitset<i> operator--(std::bitset<i> b) {
  static_assert(i <= 64, "Cannot cast 64bit int.");
  auto temp = b;
  b--;
  return temp;
}

long long constexpr inf = std::numeric_limits<long long>::max();

using namespace std;

typedef long long ll;
typedef long double ld;
typedef priority_queue<ll> pq;
typedef priority_queue<ll, vector<ll>, greater<ll>> pql;
typedef stack<ll> stk;
typedef queue<ll> qu;
typedef pair<ll, ll> pll;
typedef map<ll, ll> mll;
typedef unordered_map<ll, ll> umll;
typedef set<ll> sll;
typedef unordered_set<ll> usll;
typedef vector<ll> vll;
typedef vector<ld> vld;
typedef vector<vector<ll>> vvll;

signed main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  ll n, a, b, c;
  cin >> n >> a >> b >> c;
  vector<string> ss(n);
  for0(i, n) { cin >> ss[i]; }
  ll all = a + b + c;
  vector<string> out;

  for0(i, n) {
    assert(a + b + c == all);

    string &s = ss[i];
    string next = (i + 1 == n ? "q" : ss[i + 1]);
    auto *x = &a, *y = &b, *other = &c;
    auto f = [&](auto p) -> string {
      return (p == &a ? "A" : (p == &b ? "B" : "C"));
    };

    if (s == "AB") {
      x = &a;
      y = &b;
      other = &c;
    } else if (s == "AC") {
      x = &a;
      y = &c;
      other = &b;
    } else {
      x = &b;
      y = &c;
      other = &a;
    }

    if (*x == 0 && *y == 0) {
      cout << "No"
           << "\\n";
      return 0;
    }

    if (*x == 1 && *y == 1 && *other == 0) {
      if (next == f(x) + f(other) || next == f(other) + f(x)) {
        out.emplace_back(f(x));
        ++*x, --*y;
      } else {
        out.emplace_back(f(y));
        ++*y, --*x;
      }
      continue;
    }

    out.emplace_back(f(*x > *y ? y : x));
    if (*x > *y)
      ++*y, --*x;
    else
      ++*x, --*y;
  }

  cout << "Yes"
       << "\\n";

  assert(out.size() == n);

  for (auto &s : out) {
    cout << s << "\\n";
  }

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <chrono>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

#define for0(i, N) for (long long i = 0; i < (N); ++i)
#define for1n(i, N) for (long long i = 1; i <= (N); ++i)
#define for1(i, N) for (long long i = 1; i < (N); ++i)
#define forn(i, N) for (long long i = 0; i <= (N); ++i)
#define forx(i, N, x) for (long long i = (x); i < (N); ++i)
#define forl(i, N, x, a)                                                       \\
  for (long long i = (x), a = 0; a < (N); i = (i + 1) % (N), ++a)
#define int long long

long double constexpr pi = 3.141592653589793;

template <class T> constexpr T sigma1(T i, T n) {
  return (n - i + 1) * (i + n) / 2;
}

template <class T> constexpr T sigma2(T i, T n) {
  return (i == 1 ? n * (n + 1) * (2 * n + 1) / 6
                 : sigma2(1, n) - sigma2(1, i - 1));
}

template <class T> constexpr T sigma3(T i, T n) {
  if (i == 1) {
    auto temp = sigma1(i, n);
    return temp * temp;
  }
  return sigma2(1, n) - sigma2(1, i - 1);
}

template <size_t i> std::bitset<i> operator++(std::bitset<i> b, signed) {
  static_assert(i <= 64, "Cannot cast 64bit int.");
  return b = (unsigned long long)b + 1;
}

template <size_t i> std::bitset<i> operator++(std::bitset<i> b) {
  static_assert(i <= 64, "Cannot cast 64bit int.");
  auto temp = b;
  b++;
  return temp;
}

template <size_t i> std::bitset<i> operator--(std::bitset<i> b, signed) {
  static_assert(i <= 64, "Cannot cast 64bit int.");
  return b = (unsigned long long)b - 1;
}

template <size_t i> std::bitset<i> operator--(std::bitset<i> b) {
  static_assert(i <= 64, "Cannot cast 64bit int.");
  auto temp = b;
  b--;
  return temp;
}

long long constexpr inf = std::numeric_limits<long long>::max();

using namespace std;

typedef long long ll;
typedef long double ld;
typedef priority_queue<ll> pq;
typedef priority_queue<ll, vector<ll>, greater<ll>> pql;
typedef stack<ll> stk;
typedef queue<ll> qu;
typedef pair<ll, ll> pll;
typedef map<ll, ll> mll;
typedef unordered_map<ll, ll> umll;
typedef set<ll> sll;
typedef unordered_set<ll> usll;
typedef vector<ll> vll;
typedef vector<ld> vld;
typedef vector<vector<ll>> vvll;

signed main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  ll n, a, b, c;
  cin >> n >> a >> b >> c;
  vector<string> ss(n);
  for0(i, n) { cin >> ss[i]; }
  ll all = a + b + c;
  vector<string> out;

  for0(i, n) {
    assert(a + b + c == all);

    string &s = ss[i];
    string next = (i + 1 == n ? "q" : ss[i + 1]);
    auto *x = &a, *y = &b, *other = &c;
    auto f = [&](auto p) -> string {
      return (p == &a ? "A" : (p == &b ? "B" : "C"));
    };

    if (s == "AB") {
      x = &a;
      y = &b;
      other = &c;
    } else if (s == "AC") {
      x = &a;
      y = &c;
      other = &b;
    } else {
      x = &b;
      y = &c;
      other = &a;
    }
    if (*x == 0 && *y == 0) {
      cout << "No"
           << "\\n";
      return 0;
    }

    if (*x == 1 && *y == 1 && *other == 0) {
      if (next == f(x) + f(other) || next == f(other) + f(x)) {
        out.emplace_back(f(x));
        ++*x, --*y;
      } else {
        out.emplace_back(f(y));
        ++*y, --*x;
      }
      continue;
    }

    out.emplace_back(f(*x > *y ? y : x));
    *x > *y ? ++*y, --*x : ++*x, --*y;
  }

  cout << "Yes"
       << "\\n";

  assert(out.size() == n);

  for (auto &s : out) {
    cout << s << "\\n";
  }

  return 0;
}