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

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define mod 1000000007
#include <string.h>
#define inf 9000000000000000000
#define maxn 200006
typedef pair<ll, ll> pll;
typedef pair<int, int> pint;
#define endl '\\n'
#define mapint_iterator map<int, int>::iterator
#define mapll_iterator map<ll, ll>::iterator
#define setint_iterator set<int>::iterator
#define setll_iterator set<ll>::iterator
#define setpint_iterator set<pint>::iterator
#define setpll_iterator set<pll>::iterator
#define trace(x) cout << #x << ": " << x << " " << endl
#define trace2(x, y) cout << #x << ": " << x << " | " << #y << ": " << y << endl
#define trace3(x, y, z)                                                        \\
  cout << #x << ":" << x << " | " << #y << ": " << y << " | " << #z << ": "    \\
       << z << endl
#define trace4(a, b, c, d)                                                     \\
  cout << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \\
       << c << " | " << #d << ": " << d << endl
#define cout1(a) cout << a << endl
#define cout2(a, b) cout << a << ' ' << b << endl
#define cout3(a, b, c) cout << a << " " << b << " " << c << endl
#define cout4(a, b, c, d) cout << a << " " << b << " " << c << " " << d << endl
#define vcout(v, i) cout << v[i].fi << " " << v[i].se << endl
typedef priority_queue<ll, vector<ll>, greater<ll>> pqset;
typedef priority_queue<pint, vector<pint>, greater<pint>> pqset_ll;
#define pb push_back
#define fi first
#define se second
#include <ext/pb_ds/assoc_container.hpp>
ll dxk[] = {0, 0, 1, -1, 1, 1, -1, -1};
ll dyk[] = {1, -1, 0, 0, 1, -1, 1, -1};
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    os;
typedef tree<pll, null_type, less<pll>, rb_tree_tag,
             tree_order_statistics_node_update>
    os_pair;
#define acc (ios::sync_with_stdio(false), cin.tie(0))
#define rep(i, n) for (ll i = 0; i < n; i++)
#define per(i, n) for (ll i = n - 1; i >= 0; i--)
#define rep1(i, n) for (ll i = 1; i <= n; i++)
#define per1(i, n) for (ll i = n; i > 0; i--)
#define repeat(i, start, n) for (ll i = start; i < n; i++)
#define print(a, n)                                                            \\
  for (ll i = 0; i < n; i++) {                                                 \\
    cout << a[i] << " ";                                                       \\
  }
ll ll_max(ll a, ll b, ll c) { return max(a, max(b, c)); }
int int_max(int a, int b, int c) { return max(a, max(b, c)); }
ll ll_min(ll a, ll b, ll c) { return min(a, min(b, c)); }
int int_min(int a, int b, int c) { return min(a, min(b, c)); }
ll max(int a, ll b) { return max((ll)a, b); }
ll min(int a, ll b) { return min((ll)a, b); }
ll min(ll a, int b) { return min(a, (ll)b); }
ll max(ll a, int b) { return max(a, (ll)b); }
ll dx[] = {0, 0, 1, -1};
ll dy[] = {1, -1, 0, 0};
ll power(ll a, ll b) {
  if (a == 1)
    return 1;
  if (b == 0)
    return 1;
  ll c = power(a, b / 2);
  ll res = 1;
  if (b % 2) {
    res = (c * c) % mod;
    res *= a;
    res %= mod;
  } else
    res = ((c * c) % mod);
  return res;
}
// ifstream cin("b_read_on.txt"); ofstream cout("output3.txt");
// Chronology: Binary Search,Greedy,dp,dfs and similar
// Use (<<) for multiplication
// Use (>>) for division
//  In dp, first try top-down,then bottom-up
// ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);cout<<fixed;cerr.tie(NULL);
//  find_by_order -> value at index
//  order_of_key -> index of value
map<pair<pint, pint>, int> dp;
map<pair<pint, pint>, char> dp1;
ll n, a, b, c;
string s[100001];
ll j2;
ll func(int i, ll a1, ll b1, ll c1) {
  if (a1 < 0 || b1 < 0 || c1 < 0)
    return -1;
  if (i == n)
    return 1;
  if (dp[{{i, a1}, {b1, c1}}] != 0)
    return dp[{{i, a1}, {b1, c1}}];
  int m;
  if (s[i] == "AB") {
    if (a1 == b1)
      m = max(func(i + 1, a1 + 1, b1 - 1, c1), func(i + 1, a1 - 1, b1 + 1, c1));
    else if (a1 > b1)
      m = func(i + 1, a1 - 1, b1 + 1, c1);
    else
      m = func(i + 1, a1 + 1, b1 - 1, c1);
  } else if (s[i] == "AC") {
    if (a1 == c1)
      m = max(func(i + 1, a1 + 1, b1, c1 - 1), func(i + 1, a1 - 1, b1, c1 + 1));
    else if (a1 > c1)
      m = func(i + 1, a1 - 1, b1, c1 + 1);
    else
      m = func(i + 1, a1 + 1, b1, c1 - 1);
  } else {
    if (b1 == c1)
      m = max(func(i + 1, a1, b1 + 1, c1 - 1), func(i + 1, a1, b1 - 1, c1 + 1));
    else if (b1 > c1)
      m = func(i + 1, a1, b1 - 1, c1 + 1);
    else
      m = func(i + 1, a1, b1 + 1, c1 - 1);
  }
  dp[{{i, a1}, {b1, c1}}] = m;
  j2++;
  if (m == -1)
    return m;
  if (s[i] == "AB") {
    if (a1 == b1) {
      if (func(i + 1, a1 + 1, b1 - 1, c1) == 1)
        dp1[{{i, a1}, {b1, c1}}] = 'A';
      else
        dp1[{{i, a1}, {b1, c1}}] = 'B';
    } else if (a1 > b1)
      dp1[{{i, a1}, {b1, c1}}] = 'B';
    else
      dp1[{{i, a1}, {b1, c1}}] = 'A';
  } else if (s[i] == "AC") {
    if (a1 == c1) {
      if (func(i + 1, a1 + 1, b1, c1 - 1) == 1)
        dp1[{{i, a1}, {b1, c1}}] = 'A';
      else
        dp1[{{i, a1}, {b1, c1}}] = 'C';
    } else if (a1 > c1)
      dp1[{{i, a1}, {b1, c1}}] = 'C';
    else
      dp1[{{i, a1}, {b1, c1}}] = 'A';
  } else {
    if (b1 == c1) {
      if (func(i + 1, a1, b1 - 1, c1 + 1) == 1)
        dp1[{{i, a1}, {b1, c1}}] = 'C';
      else
        dp1[{{i, a1}, {b1, c1}}] = 'B';
    } else if (b1 > c1)
      dp1[{{i, a1}, {b1, c1}}] = 'C';
    else
      dp1[{{i, a1}, {b1, c1}}] = 'B';
  }
  return m;
}
void solve(int countu) {
  cin >> n >> a >> b >> c;
  int a1 = 0, b1 = 0, c1 = 0;
  rep(i, n) cin >> s[i];
  if (func(0, a, b, c) == -1) {
    cout1("No");
    return;
  }
  cout1("Yes");
  int i = 0;
  while (i < n) {
    char m = dp1[{{i, a}, {b, c}}];
    if (s[i] == "AB") {
      if (m == 'A') {
        cout1("A");
        a++;
        b--;
      } else {
        cout1("B");
        a--;
        b++;
      }
    } else if (s[i] == "AC") {
      if (m == 'A') {
        cout1("A");
        a++;
        c--;
      } else {
        cout1("C");
        a--;
        c++;
      }
    } else {
      if (m == 'C') {
        cout1("C");
        c++;
        b--;
      } else {
        cout1("B");
        c--;
        b++;
      }
    }
    i++;
  }
}
int main() {
  // ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);cout<<fixed;cerr.tie(NULL);cout.precision(10);
  int t = 1;
  // scanf("%d",&t);
  int countu = 1;
  while (t--) {
    solve(countu);
    countu++;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define mod 1000000007
#include <string.h>
#define inf 9000000000000000000
#define maxn 200006
typedef pair<ll, ll> pll;
typedef pair<int, int> pint;
#define endl '\\n'
#define mapint_iterator map<int, int>::iterator
#define mapll_iterator map<ll, ll>::iterator
#define setint_iterator set<int>::iterator
#define setll_iterator set<ll>::iterator
#define setpint_iterator set<pint>::iterator
#define setpll_iterator set<pll>::iterator
#define trace(x) cout << #x << ": " << x << " " << endl
#define trace2(x, y) cout << #x << ": " << x << " | " << #y << ": " << y << endl
#define trace3(x, y, z)                                                        \\
  cout << #x << ":" << x << " | " << #y << ": " << y << " | " << #z << ": "    \\
       << z << endl
#define trace4(a, b, c, d)                                                     \\
  cout << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \\
       << c << " | " << #d << ": " << d << endl
#define cout1(a) cout << a << endl
#define cout2(a, b) cout << a << ' ' << b << endl
#define cout3(a, b, c) cout << a << " " << b << " " << c << endl
#define cout4(a, b, c, d) cout << a << " " << b << " " << c << " " << d << endl
#define vcout(v, i) cout << v[i].fi << " " << v[i].se << endl
typedef priority_queue<ll, vector<ll>, greater<ll>> pqset;
typedef priority_queue<pint, vector<pint>, greater<pint>> pqset_ll;
#define pb push_back
#define fi first
#define se second
#include <ext/pb_ds/assoc_container.hpp>
ll dxk[] = {0, 0, 1, -1, 1, 1, -1, -1};
ll dyk[] = {1, -1, 0, 0, 1, -1, 1, -1};
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    os;
typedef tree<pll, null_type, less<pll>, rb_tree_tag,
             tree_order_statistics_node_update>
    os_pair;
#define acc (ios::sync_with_stdio(false), cin.tie(0))
#define rep(i, n) for (ll i = 0; i < n; i++)
#define per(i, n) for (ll i = n - 1; i >= 0; i--)
#define rep1(i, n) for (ll i = 1; i <= n; i++)
#define per1(i, n) for (ll i = n; i > 0; i--)
#define repeat(i, start, n) for (ll i = start; i < n; i++)
#define print(a, n)                                                            \\
  for (ll i = 0; i < n; i++) {                                                 \\
    cout << a[i] << " ";                                                       \\
  }
ll ll_max(ll a, ll b, ll c) { return max(a, max(b, c)); }
int int_max(int a, int b, int c) { return max(a, max(b, c)); }
ll ll_min(ll a, ll b, ll c) { return min(a, min(b, c)); }
int int_min(int a, int b, int c) { return min(a, min(b, c)); }
ll max(int a, ll b) { return max((ll)a, b); }
ll min(int a, ll b) { return min((ll)a, b); }
ll min(ll a, int b) { return min(a, (ll)b); }
ll max(ll a, int b) { return max(a, (ll)b); }
ll dx[] = {0, 0, 1, -1};
ll dy[] = {1, -1, 0, 0};
ll power(ll a, ll b) {
  if (a == 1)
    return 1;
  if (b == 0)
    return 1;
  ll c = power(a, b / 2);
  ll res = 1;
  if (b % 2) {
    res = (c * c) % mod;
    res *= a;
    res %= mod;
  } else
    res = ((c * c) % mod);
  return res;
}
// ifstream cin("b_read_on.txt"); ofstream cout("output3.txt");
// Chronology: Binary Search,Greedy,dp,dfs and similar
// Use (<<) for multiplication
// Use (>>) for division
//  In dp, first try top-down,then bottom-up
// ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);cout<<fixed;cerr.tie(NULL);
//  find_by_order -> value at index
//  order_of_key -> index of value
map<pair<pint, pint>, int> dp;
map<pair<pint, pint>, char> dp1;
ll n, a, b, c;
string s[100001];
ll func(int i, ll a1, ll b1, ll c1) {
  if (a1 < 0 || b1 < 0 || c1 < 0)
    return -1;
  if (i == n)
    return 1;
  if (dp[{{i, a1}, {b1, c1}}] != 0)
    return dp[{{i, a1}, {b1, c1}}];
  int m;
  if (s[i] == "AB")
    m = max(func(i + 1, a1 + 1, b1 - 1, c1), func(i + 1, a1 - 1, b1 + 1, c1));
  else if (s[i] == "AC")
    m = max(func(i + 1, a1 + 1, b1, c1 - 1), func(i + 1, a1 - 1, b1, c1 + 1));
  else
    m = max(func(i + 1, a1, b1 + 1, c1 - 1), func(i + 1, a1, b1 - 1, c1 + 1));
  dp[{{i, a1}, {b1, c1}}] = m;
  if (m == -1)
    return m;
  if (s[i] == "AB") {
    if (func(i + 1, a1 + 1, b1 - 1, c1) == 1)
      dp1[{{i, a1}, {b1, c1}}] = 'A';
    else
      dp1[{{i, a1}, {b1, c1}}] = 'B';
  } else if (s[i] == "AC") {
    if (func(i + 1, a1 + 1, b1, c1 - 1) == 1)
      dp1[{{i, a1}, {b1, c1}}] = 'A';
    else
      dp1[{{i, a1}, {b1, c1}}] = 'C';
  } else {
    if (func(i + 1, a1, b1 - 1, c1 + 1) == 1)
      dp1[{{i, a1}, {b1, c1}}] = 'C';
    else
      dp1[{{i, a1}, {b1, c1}}] = 'B';
  }
  return m;
}
void solve(int countu) {
  cin >> n >> a >> b >> c;
  rep(i, n) cin >> s[i];
  if (func(0, a, b, c) == -1) {
    cout1("No");
    return;
  }
  cout1("Yes");
  int i = 0;
  while (i < n) {
    char m = dp1[{{i, a}, {b, c}}];
    if (s[i] == "AB") {
      if (m == 'A') {
        cout1("A");
        a++;
        b--;
      } else {
        cout1("B");
        a--;
        b++;
      }
    } else if (s[i] == "AC") {
      if (m == 'A') {
        cout1("A");
        a++;
        c--;
      } else {
        cout1("C");
        a--;
        c++;
      }
    } else {
      if (m == 'C') {
        cout1("C");
        c++;
        b--;
      } else {
        cout1("B");
        c--;
        b++;
      }
    }
    i++;
  }
}
int main() {
  // ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);cout<<fixed;cerr.tie(NULL);cout.precision(10);
  int t = 1;
  // scanf("%d",&t);
  int countu = 1;
  while (t--) {
    solve(countu);
    countu++;
  }
}