Problem p02692 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define M 1000000007
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define ll long long int
#define mem(x) memset(x, 0, sizeof(x))
#define setbits(x) __builtin_popcount(x)
#define ull unsigned long long int
#define rep(i, a, b) for (int i = a; i < b; i++)
#define rep1(i, b, a) for (int i = b; i > a; i--)
#define all(c) (c).begin(), (c).end()
#define debug(x) cout << #x << " :: " << x << endl;
#define debug2(x, y)                                                           \
  cout << #x << " :: " << x << "\t" << #y << " :: " << y << endl;
#define cps CLOCKS_PER_SEC
#define complex                                                                \
  cerr << "Time (in ms): " << (double)(clock() - clk) * 1000.0 / cps << '\n';
#define db double
#define endl "\n"
#define fbo(x) find_by_order(x)
#define ook(x) order_of_key(x)

using namespace __gnu_pbds;
using namespace std;

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    os1;
typedef tree<int, null_type, greater<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    os2;
inline void fast() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}

const ll mod = (ll)(1e9) + 7;
ll pmod(ll x, ll n) {
  ll r = 1;
  while (n) {
    if (n & 1LL)
      r = (r * x) % mod;
    x = (x * x) % mod;
    n = n / 2;
  }
  return r;
}

// think recursively also
ll n;
ll a, b, c;
string s[100005];
int dp[100002][3][3][3];
bool sol(int in, ll ca, ll cb, ll cc) {

  if (ca < 0 || cb < 0 || cc < 0)
    return 0;
  if (in == n + 1) {
    dp[in + 1][ca][cb][cc] = 1;
    return 1;
  }
  if (dp[in][ca][cb][cc] != -1)
    return dp[in][ca][cb][cc];
  bool ans = 0;
  if (s[in] == "AB") {
    int pp = cb + 1;
    int qq = ca + 1;
    if (pp > 2)
      pp = 2;
    if (qq > 2)
      qq = 2;
    ans |= sol(in + 1, ca - 1, pp, cc);
    ans |= sol(in + 1, qq, cb - 1, cc);
  } else if (s[in] == "BC") {
    int pp = cb + 1;
    int qq = cc + 1;
    if (pp > 2)
      pp = 2;
    if (qq > 2)
      qq = 2;
    ans |= sol(in + 1, ca, pp, cc - 1);
    ans |= sol(in + 1, ca, cb - 1, qq);
  } else if (s[in] == "AC") {
    int pp = ca + 1;
    int qq = cc + 1;
    if (pp > 2)
      pp = 2;
    if (qq > 2)
      qq = 2;
    ans |= sol(in + 1, pp, cb, cc - 1);
    ans |= sol(in + 1, ca - 1, cb, qq);
  }
  dp[in][ca][cb][cc] = ans;
  return ans;
}
void go(int in, int a, int b, int c) {
  if (in == n + 1)
    return;
  if (s[in] == "AB") {
    int pp = a + 1;
    int qq = b + 1;
    if (pp > 2)
      pp = 2;
    if (qq > 2)
      qq = 2;
    if (sol(in + 1, a - 1, qq, c)) {
      cout << "B" << endl;
      go(in + 1, a - 1, qq, c);
    } else {
      cout << "A" << endl;
      go(in + 1, pp, b - 1, c);
    }
  } else if (s[in] == "BC") {
    int pp = b + 1;
    int qq = c + 1;
    if (pp > 2)
      pp = 2;
    if (qq > 2)
      qq = 2;
    if (sol(in + 1, a, b - 1, qq)) {
      cout << "C" << endl;
      go(in + 1, a, b - 1, qq);
    } else {
      cout << "B" << endl;
      go(in + 1, a, pp, c - 1);
    }
  } else if (s[in] == "AC") {
    int pp = a + 1;
    int qq = c + 1;
    if (pp > 2)
      pp = 2;
    if (qq > 2)
      qq = 2;
    if (sol(in + 1, a - 1, b, qq)) {
      cout << "C" << endl;
      go(in + 1, a - 1, b, qq);
    } else {
      cout << "A" << endl;
      go(in + 1, pp, b, c - 1);
    }
  }
}
void solve() {
  cin >> n >> a >> b >> c;

  for (int i = 1; i <= n; i++) {
    cin >> s[i];
  }
  memset(dp, -1, sizeof(dp));
  a = min(a, 2LL);
  b = min(b, 2LL);
  c = min(c, 2LL);
  if (sol(1, a, b, c)) {
    cout << "Yes" << endl;
    go(1, a, b, c);

  } else {
    cout << "No" << endl;
  }
}
int main() {
  fast();
  // int t;
  // cin>>t;
  // for(int casee=1;casee<=t;casee++)
  { // cout<<"Case #"<<casee<<": ";
    solve();
  }
}


Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define M 1000000007
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define ll long long int
#define mem(x) memset(x, 0, sizeof(x))
#define setbits(x) __builtin_popcount(x)
#define ull unsigned long long int
#define rep(i, a, b) for (int i = a; i < b; i++)
#define rep1(i, b, a) for (int i = b; i > a; i--)
#define all(c) (c).begin(), (c).end()
#define debug(x) cout << #x << " :: " << x << endl;
#define debug2(x, y)                                                           \
  cout << #x << " :: " << x << "\t" << #y << " :: " << y << endl;
#define cps CLOCKS_PER_SEC
#define complex                                                                \
  cerr << "Time (in ms): " << (double)(clock() - clk) * 1000.0 / cps << '\n';
#define db double
#define endl "\n"
#define fbo(x) find_by_order(x)
#define ook(x) order_of_key(x)

using namespace __gnu_pbds;
using namespace std;

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    os1;
typedef tree<int, null_type, greater<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    os2;
inline void fast() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}

const ll mod = (ll)(1e9) + 7;
ll pmod(ll x, ll n) {
  ll r = 1;
  while (n) {
    if (n & 1LL)
      r = (r * x) % mod;
    x = (x * x) % mod;
    n = n / 2;
  }
  return r;
}

// think recursively also
ll n;
ll a, b, c;
string s[100005];
int dp[100002][3][3][3];
bool sol(int in, ll ca, ll cb, ll cc) {

  if (ca < 0 || cb < 0 || cc < 0)
    return 0;
  if (in == n + 1) {
    return 1;
  }
  if (dp[in][ca][cb][cc] != -1)
    return dp[in][ca][cb][cc];
  bool ans = 0;
  if (s[in] == "AB") {
    int pp = cb + 1;
    int qq = ca + 1;
    if (pp > 2)
      pp = 2;
    if (qq > 2)
      qq = 2;
    ans |= sol(in + 1, ca - 1, pp, cc);
    ans |= sol(in + 1, qq, cb - 1, cc);
  } else if (s[in] == "BC") {
    int pp = cb + 1;
    int qq = cc + 1;
    if (pp > 2)
      pp = 2;
    if (qq > 2)
      qq = 2;
    ans |= sol(in + 1, ca, pp, cc - 1);
    ans |= sol(in + 1, ca, cb - 1, qq);
  } else if (s[in] == "AC") {
    int pp = ca + 1;
    int qq = cc + 1;
    if (pp > 2)
      pp = 2;
    if (qq > 2)
      qq = 2;
    ans |= sol(in + 1, pp, cb, cc - 1);
    ans |= sol(in + 1, ca - 1, cb, qq);
  }
  dp[in][ca][cb][cc] = ans;
  return ans;
}
void go(int in, int a, int b, int c) {
  if (in == n + 1)
    return;
  if (s[in] == "AB") {
    int pp = a + 1;
    int qq = b + 1;
    if (pp > 2)
      pp = 2;
    if (qq > 2)
      qq = 2;
    if (sol(in + 1, a - 1, qq, c)) {
      cout << "B" << endl;
      go(in + 1, a - 1, qq, c);
    } else {
      cout << "A" << endl;
      go(in + 1, pp, b - 1, c);
    }
  } else if (s[in] == "BC") {
    int pp = b + 1;
    int qq = c + 1;
    if (pp > 2)
      pp = 2;
    if (qq > 2)
      qq = 2;
    if (sol(in + 1, a, b - 1, qq)) {
      cout << "C" << endl;
      go(in + 1, a, b - 1, qq);
    } else {
      cout << "B" << endl;
      go(in + 1, a, pp, c - 1);
    }
  } else if (s[in] == "AC") {
    int pp = a + 1;
    int qq = c + 1;
    if (pp > 2)
      pp = 2;
    if (qq > 2)
      qq = 2;
    if (sol(in + 1, a - 1, b, qq)) {
      cout << "C" << endl;
      go(in + 1, a - 1, b, qq);
    } else {
      cout << "A" << endl;
      go(in + 1, pp, b, c - 1);
    }
  }
}
void solve() {
  cin >> n >> a >> b >> c;

  for (int i = 1; i <= n; i++) {
    cin >> s[i];
  }
  memset(dp, -1, sizeof(dp));
  a = min(a, 2LL);
  b = min(b, 2LL);
  c = min(c, 2LL);
  if (sol(1, a, b, c)) {
    cout << "Yes" << endl;
    go(1, a, b, c);

  } else {
    cout << "No" << endl;
  }
}
int main() {
  fast();
  // int t;
  // cin>>t;
  // for(int casee=1;casee<=t;casee++)
  { // cout<<"Case #"<<casee<<": ";
    solve();
  }
}