Problem p02692 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define ll long long int
#define pb push_back
#define se second
#define fi first
#define rep(i, l, r) for (ll i = (ll)(l); i < (ll)(r); i++)
#define print(v)                                                               \\
  for (const auto itr : v) {                                                   \\
    cout << itr << ' ';                                                        \\
  }                                                                            \\
  cout << endl;
#define fastIO                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define mod 1000000007
#define endl "\\n"
#define inf 1e18
using namespace std;
const ll INF = 1e18L + 5;
const ll maxn = 1e5 + 5, maxn1 = 202;
ll ceel(ll a, ll b) {
  if (a % b == 0)
    return a / b;
  return a / b + 1;
}
ll modneg(ll a, ll m) {
  a = a % m;
  if (a < 0)
    a = a + m;
  return a;
}
ll powermod(ll x, unsigned ll y, ll p) {
  ll res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1) {
      res = (res * x) % p;
    }
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
ll lcm(ll a, ll b) { return (a * b) / __gcd(a, b); };
ll csb(ll n) {
  ll count = 0;
  while (n) {
    count += n & 1;
    n >>= 1;
  }
  return count;
}
string s[100005];
stack<char> st;
ll n;
map<pair<ll, pair<ll, pair<ll, ll>>>, ll> mp;

bool calc(ll idx, ll a, ll b, ll c) {
  if (mp.find({a, {b, {c, idx}}}) != mp.end())
    return false;
  // cout<<idx<<" "<<a<<" "<<b<<" "<<c<<endl;
  if (a < 0 or b < 0 or c < 0)
    return 0;

  if (idx == n)
    return true;

  if (s[idx][0] == 'A' and s[idx][1] == 'C') {
    bool ans = calc(idx + 1, a + 1, b, c - 1);
    if (ans) {
      st.push('A');
      return 1;
    }
    ans = calc(idx + 1, a - 1, b, c + 1);
    if (ans) {
      st.push('C');
      return 1;
    }
    return 0;
  }
  if (s[idx][0] == 'A' and s[idx][1] == 'B') {
    bool ans = calc(idx + 1, a + 1, b - 1, c);
    if (ans) {
      st.push('A');
      return 1;
    }
    ans = calc(idx + 1, a - 1, b + 1, c);
    if (ans) {
      st.push('B');
      return 1;
    }
    return 0;
  }
  if (s[idx][0] == 'B' and s[idx][1] == 'C') {
    bool ans = calc(idx + 1, a, b + 1, c - 1);
    if (ans) {
      st.push('B');
      return 1;
    }
    ans = calc(idx + 1, a, b - 1, c + 1);
    if (ans) {
      st.push('C');
      return 1;
    }
    return 0;
  }
  mp[{a, {b, {c, idx}}}] = 1;
  return false;
}

int main() {

  fastIO;
  ll test, t, i, j, k, m, a, b, c, x, y, u, temp;
  cin >> n >> a >> b >> c;
  rep(i, 0, n) {
    cin >> s[i];
    sort(s[i].begin(), s[i].end());
  }
  if (calc(0, a, b, c)) {
    cout << "Yes\\n";
    while (!st.empty()) {
      cout << st.top() << endl;
      st.pop();
    }
  } else {
    cout << "No";
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ll long long int
#define pb push_back
#define se second
#define fi first
#define rep(i, l, r) for (ll i = (ll)(l); i < (ll)(r); i++)
#define print(v)                                                               \\
  for (const auto itr : v) {                                                   \\
    cout << itr << ' ';                                                        \\
  }                                                                            \\
  cout << endl;
#define fastIO                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define mod 1000000007
#define endl "\\n"
#define inf 1e18
using namespace std;
const ll INF = 1e18L + 5;
const ll maxn = 1e5 + 5, maxn1 = 202;
ll ceel(ll a, ll b) {
  if (a % b == 0)
    return a / b;
  return a / b + 1;
}
ll modneg(ll a, ll m) {
  a = a % m;
  if (a < 0)
    a = a + m;
  return a;
}
ll powermod(ll x, unsigned ll y, ll p) {
  ll res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1) {
      res = (res * x) % p;
    }
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
ll lcm(ll a, ll b) { return (a * b) / __gcd(a, b); };
ll csb(ll n) {
  ll count = 0;
  while (n) {
    count += n & 1;
    n >>= 1;
  }
  return count;
}
string s[100005];
stack<char> st;
ll n;

bool calc(ll idx, ll a, ll b, ll c) {
  // cout<<idx<<" "<<a<<" "<<b<<" "<<c<<endl;
  if (a < 0 or b < 0 or c < 0)
    return 0;
  if (idx == n and a > 0 and b > 0 and c > 0)
    return true;

  if (s[idx][0] == 'A' and s[idx][1] == 'C') {
    bool ans = calc(idx + 1, a + 1, b, c - 1);
    if (ans) {
      st.push('A');
      return 1;
    }
    ans = calc(idx + 1, a - 1, b, c + 1);
    if (ans) {
      st.push('C');
      return 1;
    }
    return 0;
  }
  if (s[idx][0] == 'A' and s[idx][1] == 'B') {
    bool ans = calc(idx + 1, a + 1, b - 1, c);
    if (ans) {
      st.push('A');
      return 1;
    }
    ans = calc(idx + 1, a - 1, b + 1, c);
    if (ans) {
      st.push('B');
      return 1;
    }
    return 0;
  }
  if (s[idx][0] == 'B' and s[idx][1] == 'C') {
    bool ans = calc(idx + 1, a, b + 1, c - 1);
    if (ans) {
      st.push('B');
      return 1;
    }
    ans = calc(idx + 1, a, b - 1, c + 1);
    if (ans) {
      st.push('C');
      return 1;
    }
    return 0;
  }
}

int main() {

  fastIO;
  ll test, t, i, j, k, m, a, b, c, x, y, u, temp;
  cin >> n >> a >> b >> c;
  rep(i, 0, n) {
    cin >> s[i];
    sort(s[i].begin(), s[i].end());
  }
  if (calc(0, a, b, c)) {
    cout << "Yes\\n";
    while (!st.empty()) {
      cout << st.top() << endl;
      st.pop();
    }
  } else {
    cout << "No";
  }
  return 0;
}