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

#include <bits/stdc++.h>
using namespace std::literals::string_literals;
using i64 = std::int_fast64_t;
using std::cerr;
using std::cin;
using std::cout;
using std::endl;

template <typename T> std::vector<T> make_v(size_t a) {
  return std::vector<T>(a);
}

template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
  return std::vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}

int main() {
  int n, a, b, c;
  scanf("%d%d%d%d", &n, &a, &b, &c);
  std::vector<std::string> s(n);
  for (int i = 0; i < n; i++)
    cin >> s[i];

  std::vector<char> ans;
  auto solve = [&](auto &&solve, int i, std::vector<int> A) -> void {
    if (A[0] < 0 or A[1] < 0 or A[2] < 0)
      return;
    if (i == n) {
      printf("Yes\\n");
      for (auto c : ans)
        printf("%c\\n", c);
      exit(0);
    }

    int x = s[i][0] - 'A', y = s[i][1] - 'A';
    A[x]--;
    A[y]++;
    ans.push_back(s[i][1]);
    solve(solve, i + 1, A);
    ans.pop_back();
    A[x]++;
    A[y]--;
    A[x]++;
    A[y]--;
    ans.push_back(s[i][0]);
    solve(solve, i + 1, A);
    ans.pop_back();
    A[x]--;
    A[y]++;
  };
  solve(solve, 0, {a, b, c});

  printf("No\\n");
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std::literals::string_literals;
using i64 = std::int_fast64_t;
using std::cerr;
using std::cin;
using std::cout;
using std::endl;

template <typename T> std::vector<T> make_v(size_t a) {
  return std::vector<T>(a);
}

template <typename T, typename... Ts> auto make_v(size_t a, Ts... ts) {
  return std::vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}

int main() {
  int n, A, B, C;
  scanf("%d%d%d%d", &n, &A, &B, &C);
  std::vector<std::string> s(n);
  for (int i = 0; i < n; i++)
    cin >> s[i];

  auto dp = make_v<int>(n + 1, 7, 7, 7);
  dp[0][std::min(A, 2)][std::min(B, 2)][std::min(C, 2)] = true;
  auto tan = make_v<std::tuple<int, int, int, char>>(n + 1, 4, 4, 4);
  for (int i = 0; i < n; i++) {
    for (int x = 0; x < 7; x++) {
      for (int y = 0; y < 7 - x; y++) {
        for (int z = 0; z < 7 - (x + y); z++) {
          if (!dp[i][x][y][z])
            continue;

          if (s[i] == "AB") {
            if (x) {
              dp[i + 1][x - 1][y + 1][z] = true;
              tan[i + 1][x - 1][y + 1][z] = {x, y, z, 'A'};
            }
            if (y) {
              dp[i + 1][x + 1][y - 1][z] = true;
              tan[i + 1][x + 1][y - 1][z] = {x, y, z, 'B'};
            }
          } else if (s[i] == "BC") {
            if (y) {
              dp[i + 1][x][y - 1][z + 1] = true;
              tan[i + 1][x][y - 1][z + 1] = {x, y, z, 'B'};
            }
            if (z) {
              dp[i + 1][x][y + 1][z - 1] = true;
              tan[i + 1][x][y + 1][z - 1] = {x, y, z, 'C'};
            }
          } else if (s[i] == "AC") {
            if (x) {
              dp[i + 1][x - 1][y][z + 1] = true;
              tan[i + 1][x - 1][y][z + 1] = {x, y, z, 'A'};
            }
            if (z) {
              dp[i + 1][x + 1][y][z - 1] = true;
              tan[i + 1][x + 1][y][z - 1] = {x, y, z, 'C'};
            }
          }
        }
      }
    }
  }

  int now = -1, nx = 0, ny = 0, nz = 0;
  for (int x = 0; x < 7; x++) {
    for (int y = 0; y < 7; y++) {
      for (int z = 0; z < 7; z++) {
        if (!dp[n][x][y][z])
          continue;
        now = n;
        nx = x;
        ny = y;
        nz = z;
        break;
      }
    }
  }
  if (now == -1) {
    printf("No\\n");
    return 0;
  }

  std::vector<char> ans;
  while (now) {
    auto [a, b, c, d] = tan[now][nx][ny][nz];
    now--;
    nx = a;
    ny = b;
    nz = c;
    ans.push_back(d);
  }
  reverse(begin(ans), end(ans));

  printf("Yes\\n");
  for (auto c : ans)
    printf("%c\\n", c);
  return 0;
}