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>
#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 val[3];
string s[100005];
int dp[100002][3][3][3];
vector<int> ans;
void sol(int in, ll a, ll b, ll c, ll ca, ll cb, ll cc, vector<int> temp) {
  if (ans.size())
    return;
  ll af = val[0] + ca;
  ll bf = val[1] + cb;
  ll cf = val[2] + cc;
  if (af < 0 || bf < 0 || cf < 0)
    return;
  if (in == n + 1) {
    if (af >= 0 && bf >= 0 && cf >= 0) {
      if (ans.size() == 0)
        ans = temp;
      return;
    } else
      return;
  }

  bool fans = 0;
  if (s[in] == "AB") {
    temp.pb(1);
    sol(in + 1, -1, +1, 0, ca - 1, cb + 1, cc, temp);
    temp.pop_back();
    temp.pb(0);
    sol(in + 1, 1, -1, 0, ca + 1, cb - 1, cc, temp);
    temp.pop_back();
  } else if (s[in] == "BC") {
    temp.pb(1);
    sol(in + 1, 0, 1, -1, ca, cb + 1, cc - 1, temp);
    temp.pop_back();
    temp.pb(2);
    sol(in + 1, 0, -1, 1, ca, cb - 1, cc + 1, temp);
    temp.pop_back();
  } else if (s[in] == "AC") {
    temp.pb(0);
    sol(in + 1, 1, 0, -1, ca + 1, cb, cc - 1, temp);
    temp.pop_back();
    temp.pb(2);
    sol(in + 1, -1, 0, 1, ca - 1, cb, cc + 1, temp);
    temp.pop_back();
  }
  // dp[in][a+1][b+1][c+1]=ans;
  // return fans;
}
void solve() {
  cin >> n;
  for (int i = 0; i < 3; i++)
    cin >> val[i];

  ll ct[3] = {0};
  bool f = 1;
  for (int i = 1; i <= n; i++) {
    cin >> s[i];
    ct[s[i][0] - 'A'] += 1;
    ct[s[i][1] - 'A'] += 1;
  }
  vector<int> temp;
  sol(1, 0, 0, 0, 0, 0, 0, temp);
  if (ans.size()) {
    cout << "Yes" << endl;
    for (int it : ans) {
      cout << char('A' + it) << endl;
    }
  } else {
    cout << "No" << endl;
  }
}
int main() {
  fast();
  // int t;
  // cin>>t;
  // for(int casee=1;casee<=t;casee++)
  { // cout<<"Case #"<<casee<<": ";
    solve();
  }
}