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

// #pragma GCC optimize("Ofast")
// #pragma GCC optimize ("unroll-loops")
// #pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define mod 998244353
#define fi first
#define se second
#define pii pair<int, int>
#define int long long
#define endl '\\n'
#define pll pair<long long, long long>
#define LONGLONG_MAX 100000000000000
using namespace std;
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
long long power(long long a, long long b) {
  long long ans = 1;
  while (b > 0) {
    if (b & 1) {
      ans = (ans * a) % mod;
    }
    a = (a * a) % mod;
    b >>= 1;
  }
  return ans;
}
string s[100005];
map<string, int> m;
string ans;
map<string, int> m1;
bool fun(int i, vector<int> v, int n) {
  if (v[0] < 0 || v[1] < 0 || v[2] < 0)
    return false;
  if (i == n) {
    return true;
  }
  v[s[i][0] - 'A']++;
  v[s[i][1] - 'A']--;
  if (fun(i + 1, v, n)) {
    ans.push_back(s[i][0]);
    return true;
  }
  v[s[i][0] - 'A']--;
  v[s[i][1] - 'A']++;

  v[s[i][0] - 'A']--;
  v[s[i][1] - 'A']++;
  if (fun(i + 1, v, n)) {
    ans.push_back(s[i][1]);
    return true;
  }
  return false;
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n, a, b, c;
  cin >> n >> a >> b >> c;
  for (int i = 0; i < n; i++) {
    cin >> s[i];
  }
  m1["AB"] = 0;
  m1["BC"] = 1;
  m1["AC"] = 2;
  vector<int> v;
  v.push_back(a);
  v.push_back(b);
  v.push_back(c);
  if (fun(0, v, n)) {
    cout << "Yes" << endl;
    reverse(ans.begin(), ans.end());
    for (auto j : ans)
      cout << j << endl;
    return 0;
  }
  cout << "No";
  return 0;
}


Error: 0

Fixed submission:

// #pragma GCC optimize("Ofast")
// #pragma GCC optimize ("unroll-loops")
// #pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define mod 998244353
#define fi first
#define se second
#define pii pair<int, int>
#define int long long
#define endl '\\n'
#define pll pair<long long, long long>
#define LONGLONG_MAX 100000000000000
using namespace std;
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
long long power(long long a, long long b) {
  long long ans = 1;
  while (b > 0) {
    if (b & 1) {
      ans = (ans * a) % mod;
    }
    a = (a * a) % mod;
    b >>= 1;
  }
  return ans;
}
string s[100005];
map<string, int> m;
string ans;
map<string, int> m1;
bool fun(int i, int mask, vector<int> v, int n) {
  if (i == n) {
    return true;
  }
  m[s[i]]++;
  int x = m[s[i]];
  v[s[i][(m[s[i]] + ((mask & (1 << m1[s[i]])) > 0)) % 2] - 'A']++;
  v[s[i][(m[s[i]] + ((mask & (1 << m1[s[i]])) > 0) + 1) % 2] - 'A']--;
  if (v[0] < 0 || v[1] < 0 || v[2] < 0)
    return false;
  if (fun(i + 1, mask, v, n)) {
    ans.push_back(s[i][(x + ((mask & (1 << m1[s[i]])) > 0)) % 2]);
    return true;
  } else
    return false;
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n, a, b, c;
  cin >> n >> a >> b >> c;
  for (int i = 0; i < n; i++) {
    cin >> s[i];
  }
  m1["AB"] = 0;
  m1["BC"] = 1;
  m1["AC"] = 2;
  vector<int> v;
  v.push_back(a);
  v.push_back(b);
  v.push_back(c);
  for (int i = 0; i < 8; i++) {
    m.clear();
    ans.clear();
    if (fun(0, i, v, n)) {
      cout << "Yes" << endl;
      reverse(ans.begin(), ans.end());
      for (auto j : ans)
        cout << j << endl;
      return 0;
    }
  }
  assert(false);
  cout << "No";
  return 0;
}