Problem p02692 (C++)

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

#define int long long int
#define fastio                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);

const int mod = 1e9 + 7;
const int N = 2e5 + 5;
const int inf = 1e16 + 2;

int dp[N][3][3][3], n, A, B, C, check = 0;
char ans[N][3][3][3];
string s[N];
int rec(int idx, int a, int b, int c) {
  if (a < 0 or b < 0 or c < 0)
    return 0;
  if (idx == n) {
    check = 1;
    return 1;
  }
  if (dp[idx][a][b][c] != -1)
    return dp[idx][a][b][c];

  int res = 0;
  if (s[idx] == "AB") {
    if (rec(idx + 1, a + 1, b - 1, c))
      ans[idx][a][b][c] = 'A', res = 1;
    else if (rec(idx + 1, a - 1, b + 1, c))
      ans[idx][a][b][c] = 'B', res = 1;
  } else if (s[idx] == "AC") {
    if (rec(idx + 1, a + 1, b, c - 1))
      ans[idx][a][b][c] = 'A', res = 1;
    else if (rec(idx + 1, a - 1, b, c + 1))
      ans[idx][a][b][c] = 'C', res = 1;
  } else {
    if (rec(idx + 1, a, b - 1, c + 1))
      ans[idx][a][b][c] = 'C', res = 1;
    else if (rec(idx + 1, a, b + 1, c - 1))
      ans[idx][a][b][c] = 'B', res = 1;
  }
  dp[idx][a][b][c] = res;
  return res;
}
void solve(int z) {
  memset(dp, -1, sizeof dp);
  cin >> n >> A >> B >> C;
  for (int i = 0; i < n; i++)
    cin >> s[i];

  rec(0, A, B, C);

  if (check == 0)
    cout << "No";
  else {
    cout << "Yes" << '\n';
    int i = 0;
    while (n--) {
      cout << ans[i][A][B][C] << '\n';
      if (ans[i][A][B][C] == 'A') {
        A++;
        if (s[i] == "AB")
          B--;
        else
          C--;
      }
      if (ans[i][A][B][C] == 'B') {
        B++;
        if (s[i] == "AB")
          A--;
        else
          C--;
      }
      if (ans[i][A][B][C] == 'C') {
        C++;
        if (s[i] == "AC")
          A--;
        else
          B--;
      }
      i++;
    }
  }
}

/*
 */

int32_t main() {
  fastio int testcase = 1;
  // cin >> testcase;
  int x = 1;
  while (testcase--) {
    solve(x++);
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define int long long int
#define fastio                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);

const int mod = 1e9 + 7;
const int N = 2e5 + 5;
const int inf = 1e16 + 2;

int dp[N][3][3][3], n, A, B, C, check = 0;
char ans[N][3][3][3];
string s[N];
int rec(int idx, int a, int b, int c) {
  if (a < 0 or b < 0 or c < 0)
    return 0;
  if (idx == n) {
    check = 1;
    return 1;
  }
  if (dp[idx][a][b][c] != -1)
    return dp[idx][a][b][c];

  int res = 0;
  if (s[idx] == "AB") {
    if (rec(idx + 1, a + 1, b - 1, c))
      ans[idx][a][b][c] = 'A', res = 1;
    else if (rec(idx + 1, a - 1, b + 1, c))
      ans[idx][a][b][c] = 'B', res = 1;
  } else if (s[idx] == "AC") {
    if (rec(idx + 1, a + 1, b, c - 1))
      ans[idx][a][b][c] = 'A', res = 1;
    else if (rec(idx + 1, a - 1, b, c + 1))
      ans[idx][a][b][c] = 'C', res = 1;
  } else {
    if (rec(idx + 1, a, b - 1, c + 1))
      ans[idx][a][b][c] = 'C', res = 1;
    else if (rec(idx + 1, a, b + 1, c - 1))
      ans[idx][a][b][c] = 'B', res = 1;
  }
  dp[idx][a][b][c] = res;
  return res;
}
void solve(int z) {
  memset(dp, -1, sizeof dp);
  cin >> n >> A >> B >> C;
  for (int i = 0; i < n; i++)
    cin >> s[i];

  A = min(2LL, A);
  B = min(2LL, B);
  C = min(2LL, C);
  rec(0, A, B, C);

  if (check == 0)
    cout << "No";
  else {
    cout << "Yes" << '\n';
    int i = 0;
    while (n--) {
      cout << ans[i][A][B][C] << '\n';
      if (ans[i][A][B][C] == 'A') {
        A++;
        if (s[i] == "AB")
          B--;
        else
          C--;
      }
      if (ans[i][A][B][C] == 'B') {
        B++;
        if (s[i] == "AB")
          A--;
        else
          C--;
      }
      if (ans[i][A][B][C] == 'C') {
        C++;
        if (s[i] == "AC")
          A--;
        else
          B--;
      }
      i++;
    }
  }
}

/*
 */

int32_t main() {
  fastio int testcase = 1;
  // cin >> testcase;
  int x = 1;
  while (testcase--) {
    solve(x++);
  }

  return 0;
}