Problem p02692 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cinttypes>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <iostream>
#include <numeric>
#include <string>
#include <utility>
#include <vector>
using u8t = std::uint64_t;
using i8t = std::int64_t;
#define F(I, N) for (i8t I = 0, I##_N = N; I < I##_N; I++)
#define FR(I, N) for (i8t I = N; I--;)
#define R(V)                                                                   \
  i8t V;                                                                       \
  std::cin >> V;
#define RV(V, N)                                                               \
  std::vector<i8t> V;                                                          \
  F(_i, N) { R(_x) V.push_back(_x); }
#define RL1(line) std::getline(std::cin, line);
#define RL(line)                                                               \
  std::string line;                                                            \
  RL1(line)
#define RM(V, L, C)                                                            \
  std::vector<char> V;                                                         \
  F(_i, L) { RL(_l) F(_j, C) V.push_back(_l[_j]); }
#define P(X) std::cout << (X) << std::endl;
int main() {
  R(N) R(A) R(B) R(C);
  RL(dummy);
  i8t t[3] = {A, B, C};
  std::string lines[3];
  std::vector<char> out;

  auto check = [&]() {
    for (int s = 0; s < 8; s++) {
      i8t t2[3] = {t[0], t[1], t[2]};
      bool fail = false;
      for (int i = 0; i < 3; i++) {
        int a = lines[i][0] - 'A', b = lines[i][1] - 'A';
        if (s >> i & 1)
          std::swap(a, b);
        t2[a]++;
        t2[b]--;
        if (t2[b] < 0) {
          fail = true;
          break;
        }
      }
      if (!fail) {
        out.push_back(lines[0][s & 1]);
        int a = lines[0][0] - 'A', b = lines[0][1] - 'A';
        if (s & 1)
          std::swap(a, b);
        t[a]++;
        t[b]--;
        return true;
      }
    }
    return false;
  };

  F(I, N) {
    RL1(lines[2]);
    if (I >= 2) {
      if (!check())
        goto No;
    }
    lines[0] = lines[1];
    lines[1] = lines[2];
  }
  lines[2] = "AA";
  if (!check())
    goto No;
  lines[0] = lines[1];
  lines[1] = lines[2];
  lines[2] = "AA";
  if (!check())
    goto No;
  P("Yes");
  for (char c : out)
    P(c);
  return 0;
No:
  P("No");
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cinttypes>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <iostream>
#include <numeric>
#include <string>
#include <utility>
#include <vector>
using u8t = std::uint64_t;
using i8t = std::int64_t;
#define F(I, N) for (i8t I = 0, I##_N = N; I < I##_N; I++)
#define FR(I, N) for (i8t I = N; I--;)
#define R(V)                                                                   \
  i8t V;                                                                       \
  std::cin >> V;
#define RV(V, N)                                                               \
  std::vector<i8t> V;                                                          \
  F(_i, N) { R(_x) V.push_back(_x); }
#define RL1(line) std::getline(std::cin, line);
#define RL(line)                                                               \
  std::string line;                                                            \
  RL1(line)
#define RM(V, L, C)                                                            \
  std::vector<char> V;                                                         \
  F(_i, L) { RL(_l) F(_j, C) V.push_back(_l[_j]); }
#define P(X) std::cout << (X) << std::endl;
int main() {
  R(N) R(A) R(B) R(C);
  RL(dummy);
  i8t t[3] = {A, B, C};
  std::string lines[3];
  std::vector<char> out;

  auto check = [&]() {
    for (int s = 0; s < 8; s++) {
      i8t t2[3] = {t[0], t[1], t[2]};
      bool fail = false;
      for (int i = 0; i < 3; i++) {
        int a = lines[i][0] - 'A', b = lines[i][1] - 'A';
        if (s >> i & 1)
          std::swap(a, b);
        t2[a]++;
        t2[b]--;
        if (t2[b] < 0) {
          fail = true;
          break;
        }
      }
      if (!fail) {
        out.push_back(lines[0][s & 1]);
        int a = lines[0][0] - 'A', b = lines[0][1] - 'A';
        if (s & 1)
          std::swap(a, b);
        t[a]++;
        t[b]--;
        return true;
      }
    }
    return false;
  };

  F(I, N) {
    RL1(lines[2]);
    if (I >= 2) {
      if (!check())
        goto No;
    }
    lines[0] = lines[1];
    lines[1] = lines[2];
  }
  lines[2] = "AA";
  if (N >= 2 && !check())
    goto No;
  lines[0] = lines[1];
  lines[1] = lines[2];
  lines[2] = "AA";
  if (!check())
    goto No;
  P("Yes");
  for (char c : out)
    P(c);
  return 0;
No:
  P("No");
  return 0;
}