Problem p02692 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define FAST ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set                                                            \\
  tree<int, null_type, less<int>, rb_tree_tag,                                 \\
       tree_order_statistics_node_update>

// #define int long long
#define double long double

#define all(a) (a).begin(), (a).end()
#define sz(x) (int)x.size()

#define ff first
#define ss second
#define mp make_pair

#define pb push_back
#define endl "\\n"

#define f(i, l, r) for (int i = l; i <= r; i++)
#define rf(i, r, l) for (int i = r; i >= l; i--)

#define bp __builtin_popcountll
#define inf 3e15

const int N = 1e5 + 2;
const int M = 1e9 + 7;
int dp[N][3][3][3];
int n, a, b, c;
string s[N];
int go(int idx, int aa, int bb, int cc) {
  if (aa < 0 || bb < 0 || cc < 0)
    return 0;
  if (idx == n + 1) {
    return 1;
  }
  int &ans = dp[idx][aa][bb][cc];
  if (ans != -1)
    return ans;
  ans = 0;
  if (s[idx] == "AB") {
    int pp = bb + 1, qq = aa + 1;
    if (bb + 1 > 2)
      pp = 2;
    if (aa + 1 > 2)
      qq = 2;
    ans = ans | (go(idx + 1, aa - 1, pp, cc));
    ans = ans | (go(idx + 1, qq, bb - 1, cc));
  } else if (s[idx] == "AC") {
    int pp = aa + 1, qq = cc + 1;
    if (aa + 1 > 2)
      pp = 2;
    if (cc + 1 > 2)
      qq = 2;
    ans = ans | (go(idx + 1, aa - 1, bb, qq));
    ans = ans | (go(idx + 1, pp, bb, cc - 1));
  } else {
    int pp = bb + 1, qq = cc + 1;
    if (bb + 1 > 2)
      pp = 2;
    if (cc + 1 > 2)
      qq = 2;
    ans = ans | (go(idx + 1, aa, bb - 1, qq));
    ans = ans | (go(idx + 1, aa, pp, cc - 1));
  }
  return ans;
}
void god(int idx, int aa, int bb, int cc) {

  if (idx == n + 1) {
    return;
  }

  if (s[idx] == "AB") {
    int pp = bb + 1, qq = aa + 1;
    if (bb + 1 > 2)
      pp = 2;
    if (aa + 1 > 2)
      qq = 2;
    // ans=ans|(go(idx+1,aa-1,pp,cc));
    // ans=ans|(go(idx+1,qq,bb-1,cc));
    if (go(idx + 1, aa - 1, pp, cc)) {
      cout << "B" << endl;
      god(idx + 1, aa - 1, pp, cc);
    } else {
      cout << "A" << endl;
      god(idx + 1, qq, bb - 1, cc);
    }

  } else if (s[idx] == "AC") {
    int pp = aa + 1, qq = cc + 1;
    if (aa + 1 > 2)
      pp = 2;
    if (cc + 1 > 2)
      qq = 2;
    // ans=ans|(go(idx+1,aa-1,bb,qq));
    // ans=ans|(go(idx+1,pp,bb,cc-1));
    if (go(idx + 1, aa - 1, bb, qq)) {
      cout << "C" << endl;
      god(idx + 1, aa - 1, bb, qq);
    } else {
      cout << "A" << endl;
      god(idx + 1, pp, bb, cc - 1);
    }
  } else {
    int pp = bb + 1, qq = cc + 1;
    if (bb + 1 > 2)
      pp = 2;
    if (cc + 1 > 2)
      qq = 2;
    // ans=ans|(go(idx+1,aa,bb-1,qq));
    // ans=ans|(go(idx+1,aa,pp,cc-1));
    if (go(idx + 1, aa, bb - 1, qq)) {
      cout << "C" << endl;
      god(idx + 1, aa, bb - 1, qq);
    } else {
      cout << "B" << endl;
      god(idx + 1, aa, pp, cc - 1);
    }
  }
  // return ans;
}
void solve() {
  cin >> n >> a >> b >> c;
  f(i, 1, n) cin >> s[i];
  a = min(a, 2);
  b = min(b, 2);
  c = min(c, 2);
  memset(dp, -1, sizeof(dp));
  int res = go(1, a, b, c);
  if (res == false) {
    cout << "No";
    return;
  }
  cout << "Yes" << endl;
  god(1, a, b, c);
}
signed main() {
  FAST int t = 1;
  // cin>>t;
  for (int tc = 1; tc <= t; tc++) {
    // cout<<"Case #"<<tc<<": ";
    solve();
  }
}


Error: TLE

Fixed submission:

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

#define FAST ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set                                                            \\
  tree<int, null_type, less<int>, rb_tree_tag,                                 \\
       tree_order_statistics_node_update>

#define int long long
#define double long double

#define all(a) (a).begin(), (a).end()
#define sz(x) (int)x.size()

#define ff first
#define ss second
#define mp make_pair

#define pb push_back
#define endl "\\n"

#define f(i, l, r) for (int i = l; i <= r; i++)
#define rf(i, r, l) for (int i = r; i >= l; i--)

#define bp __builtin_popcountll
#define inf 3e15

const int N = 1e5 + 1;
const int M = 1e9 + 7;
bool dp[N][3][3][3];
int n, a, b, c;
string s[N];
bool go(int idx, int aa, int bb, int cc) {
  if (aa < 0 || bb < 0 || cc < 0)
    return 0;
  if (idx == n + 1) {
    return 1;
  }
  bool &ans = dp[idx][aa][bb][cc];
  ans = 0;
  if (s[idx] == "AB") {
    int pp = bb + 1, qq = aa + 1;
    if (bb + 1 > 2)
      pp = 2;
    if (aa + 1 > 2)
      qq = 2;
    ans = ans | (go(idx + 1, aa - 1, pp, cc));
    ans = ans | (go(idx + 1, qq, bb - 1, cc));
  } else if (s[idx] == "AC") {
    int pp = aa + 1, qq = cc + 1;
    if (aa + 1 > 2)
      pp = 2;
    if (cc + 1 > 2)
      qq = 2;
    ans = ans | (go(idx + 1, aa - 1, bb, qq));
    ans = ans | (go(idx + 1, pp, bb, cc - 1));
  } else {
    int pp = bb + 1, qq = cc + 1;
    if (bb + 1 > 2)
      pp = 2;
    if (cc + 1 > 2)
      qq = 2;
    ans = ans | (go(idx + 1, aa, bb - 1, qq));
    ans = ans | (go(idx + 1, aa, pp, cc - 1));
  }
  return ans;
}
void god(int idx, int aa, int bb, int cc) {

  if (idx == n + 1) {
    return;
  }

  if (s[idx] == "AB") {
    int pp = bb + 1, qq = aa + 1;
    if (bb + 1 > 2)
      pp = 2;
    if (aa + 1 > 2)
      qq = 2;
    // ans=ans|(go(idx+1,aa-1,pp,cc));
    // ans=ans|(go(idx+1,qq,bb-1,cc));
    if (go(idx + 1, aa - 1, pp, cc)) {
      cout << "B" << endl;
      god(idx + 1, aa - 1, pp, cc);
    } else {
      cout << "A" << endl;
      god(idx + 1, qq, bb - 1, cc);
    }

  } else if (s[idx] == "AC") {
    int pp = aa + 1, qq = cc + 1;
    if (aa + 1 > 2)
      pp = 2;
    if (cc + 1 > 2)
      qq = 2;
    // ans=ans|(go(idx+1,aa-1,bb,qq));
    // ans=ans|(go(idx+1,pp,bb,cc-1));
    if (go(idx + 1, aa - 1, bb, qq)) {
      cout << "C" << endl;
      god(idx + 1, aa - 1, bb, qq);
    } else {
      cout << "A" << endl;
      god(idx + 1, pp, bb, cc - 1);
    }
  } else {
    int pp = bb + 1, qq = cc + 1;
    if (bb + 1 > 2)
      pp = 2;
    if (cc + 1 > 2)
      qq = 2;
    // ans=ans|(go(idx+1,aa,bb-1,qq));
    // ans=ans|(go(idx+1,aa,pp,cc-1));
    if (go(idx + 1, aa, bb - 1, qq)) {
      cout << "C" << endl;
      god(idx + 1, aa, bb - 1, qq);
    } else {
      cout << "B" << endl;
      god(idx + 1, aa, pp, cc - 1);
    }
  }
  // return ans;
}
void solve() {
  cin >> n >> a >> b >> c;
  f(i, 1, n) cin >> s[i];
  a = min(a, 2LL);
  b = min(b, 2LL);
  c = min(c, 2LL);
  bool res = go(1, a, b, c);
  if (res == false) {
    cout << "No";
    return;
  }
  cout << "Yes" << endl;
  god(1, a, b, c);
}
signed main() {
  FAST int t = 1;
  // cin>>t;
  for (int tc = 1; tc <= t; tc++) {
    // cout<<"Case #"<<tc<<": ";
    solve();
  }
}