Problem p02692 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#include "string"
// #define int long long
#define pi pair<int, int>
#define ff first
#define ss second
#define boost                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(nullptr)
const int N = 1e5 + 1;
int n, a, b, c;
vector<string> s;
int dp[N][4][4][4];

int go(int id, int a, int b, int c) {
  if (a < 0 or b < 0 or c < 0)
    return 0;
  a = min(a, 3);
  b = min(b, 3);
  c = min(c, 3);
  if (id >= n) {
    dp[id][a][b][c] = 1;
    return 1;
  }

  int &tmp = dp[id][a][b][c];
  if (tmp != -1)
    return tmp;
  tmp = 0;
  if (s[id] == "AB") {
    tmp |= go(id + 1, a + 1, b - 1, c);
    tmp |= go(id + 1, a - 1, b + 1, c);
  } else if (s[id] == "AC") {
    tmp |= go(id + 1, a + 1, b, c - 1);
    tmp |= go(id + 1, a - 1, b, c + 1);
  } else {
    tmp |= go(id + 1, a, b + 1, c - 1);
    tmp |= go(id + 1, a, b - 1, c + 1);
  }
  return tmp;
}

void trace(int id, int a, int b, int c) {
  if (id >= n)
    return;
  if (s[id] == "AB") {
    if (a < 3 && b > 0 && dp[id + 1][a + 1][b - 1][c] == 1) {
      cout << 'A' << endl;
      trace(id + 1, a + 1, b - 1, c);
    } else {
      cout << 'B' << endl;
      trace(id + 1, a - 1, b + 1, c);
    }
  } else if (s[id] == "AC") {
    if (a < 3 && c > 0 && dp[id + 1][a + 1][b][c - 1] == 1) {
      cout << 'A' << endl;
      trace(id + 1, a + 1, b, c - 1);
    } else {
      cout << 'C' << endl;
      trace(id + 1, a - 1, b, c + 1);
    }
  } else {
    if (b < 3 && c > 0 && dp[id + 1][a][b + 1][c - 1] == 1) {
      cout << 'B' << endl;
      trace(id + 1, a, b + 1, c - 1);
    } else {
      cout << 'C' << endl;
      trace(id + 1, a, b - 1, c + 1);
    }
  }
}

int32_t main() {
  boost;
  cin >> n >> a >> b >> c;
  s.resize(n);
  for (int i = 0; i < n; i++)
    cin >> s[i];
  memset(dp, -1, sizeof dp);
  if (go(0, a, b, c)) {
    cout << "Yes" << endl;
    trace(0, a, b, c);
  } else
    cout << "No" << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#include "string"
// #define int long long
#define pi pair<int, int>
#define ff first
#define ss second
#define boost                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(nullptr)
const int N = 1e5 + 1;
int n, a, b, c;
vector<string> s;
int dp[N][4][4][4];

int go(int id, int a, int b, int c) {
  if (a < 0 or b < 0 or c < 0)
    return 0;
  a = min(a, 3);
  b = min(b, 3);
  c = min(c, 3);
  if (id >= n) {
    dp[id][a][b][c] = 1;
    return 1;
  }

  int &tmp = dp[id][a][b][c];
  if (tmp != -1)
    return tmp;
  tmp = 0;
  if (s[id] == "AB") {
    tmp |= go(id + 1, a + 1, b - 1, c);
    tmp |= go(id + 1, a - 1, b + 1, c);
  } else if (s[id] == "AC") {
    tmp |= go(id + 1, a + 1, b, c - 1);
    tmp |= go(id + 1, a - 1, b, c + 1);
  } else {
    tmp |= go(id + 1, a, b + 1, c - 1);
    tmp |= go(id + 1, a, b - 1, c + 1);
  }
  return tmp;
}

void trace(int id, int a, int b, int c) {
  if (id >= n)
    return;
  a = min(a, 3);
  b = min(b, 3);
  c = min(c, 3);
  if (s[id] == "AB") {
    if (a < 3 && b > 0 && dp[id + 1][a + 1][b - 1][c] == 1) {
      cout << 'A' << endl;
      trace(id + 1, a + 1, b - 1, c);
    } else {
      cout << 'B' << endl;
      trace(id + 1, a - 1, b + 1, c);
    }
  } else if (s[id] == "AC") {
    if (a < 3 && c > 0 && dp[id + 1][a + 1][b][c - 1] == 1) {
      cout << 'A' << endl;
      trace(id + 1, a + 1, b, c - 1);
    } else {
      cout << 'C' << endl;
      trace(id + 1, a - 1, b, c + 1);
    }
  } else {
    if (b < 3 && c > 0 && dp[id + 1][a][b + 1][c - 1] == 1) {
      cout << 'B' << endl;
      trace(id + 1, a, b + 1, c - 1);
    } else {
      cout << 'C' << endl;
      trace(id + 1, a, b - 1, c + 1);
    }
  }
}

int32_t main() {
  boost;
  cin >> n >> a >> b >> c;
  s.resize(n);
  for (int i = 0; i < n; i++)
    cin >> s[i];
  memset(dp, -1, sizeof dp);
  if (go(0, a, b, c)) {
    cout << "Yes" << endl;
    trace(0, a, b, c);
  } else
    cout << "No" << endl;
}