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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
template <class T> using V = vector<T>;
#define fi first
#define se second
#define all(v) (v).begin(), (v).end()
const ll inf = (1e18);
// const ll mod=998244353;
const ll mod = 1000000007;
int GCD(ll a, ll b) { return b ? GCD(b, a % b) : a; }
ll LCM(ll c, ll d) { return c / GCD(c, d) * d; }
struct __INIT {
  __INIT() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(15);
  }
} __init;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
bool dp[200005][5][5][5];
int main() {
  ll n, a, b, c;
  cin >> n >> a >> b >> c;
  ll memoa = a, memob = b, memoc = c;
  V<string> s(n);
  for (int i = 0; i < n; i++)
    cin >> s[i];
  V<char> ans(n);
  bool ok = true;
  for (int i = 0; i < n; i++) {
    if (s[i] == "AC") {
      if (a == 0 && c == 0)
        ok = false;
      if (a < c) {
        a++;
        c--;
        ans[i] = 'A';
      } else if (a >= c) {
        a--;
        c++;
        ans[i] = 'C';
      }
    } else if (s[i] == "BC") {
      if (b == 0 && c == 0)
        ok = false;
      if (b > c) {
        b--;
        c++;
        ans[i] = 'C';
      } else if (b <= c) {
        b++;
        c--;
        ans[i] = 'B';
      }
    } else if (s[i] == "AB") {
      if (a == 0 && b == 0)
        ok = false;
      if (a < b) {
        a++;
        b--;
        ans[i] = 'A';
      } else if (a >= b) {
        a--;
        b++;
        ans[i] = 'B';
      }
    }
  }
  if (ok) {
    cout << "Yes"
         << "\\n";
    for (int i = 0; i < n; i++)
      cout << ans[i] << "\\n";
    return 0;
  }
  a = memoa;
  b = memob;
  c = memoc;
  if (a + b + c > 3) {
    cout << "No"
         << "\\n";
    return 0;
  }
  dp[0][a][b][c] = true;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 4; j++) {
      for (int k = 0; k < 4; k++) {
        for (int r = 0; r < 4; r++) {
          if (!dp[i][j][k][r])
            continue;
          if (s[i] == "AB") {
            if (j)
              dp[i + 1][j - 1][k + 1][r] = dp[i][j][k][r];
            if (k)
              dp[i + 1][j + 1][k - 1][r] = dp[i][j][k][r];
          }
          if (s[i] == "AC") {
            if (j)
              dp[i + 1][j - 1][k][r + 1] = dp[i][j][k][r];
            if (r)
              dp[i + 1][j + 1][k][r - 1] = dp[i][j][k][r];
          }
          if (s[i] == "BC") {
            if (k)
              dp[i + 1][j][k - 1][r + 1] = dp[i][j][k][r];
            if (r)
              dp[i + 1][j][k + 1][r - 1] = dp[i][j][k][r];
          }
        }
      }
    }
  }
  ok = false;
  ll now = a + b + c;
  for (a = 0; a < 4; a++) {
    for (b = 0; b < 4; b++) {
      for (c = 0; c < 4; c++) {
        if (dp[n][a][b][c] && a + b + c == now) {
          ok = true;
          break;
        }
      }
      if (ok)
        break;
    }
    if (ok)
      break;
  }
  if (!ok) {
    cout << "No"
         << "\\n";
    return 0;
  }
  for (int i = n - 1; i >= 0; i--) {
    if (s[i] == "AB") {
      if (a && dp[i][a - 1][b + 1][c]) {
        ans[i] = 'A';
        a--;
        b++;
      } else {
        ans[i] = 'B';
        a++;
        b--;
      }
    }
    if (s[i] == "AC") {
      if (a && dp[i][a - 1][b][c + 1]) {
        ans[i] = 'A';
        a--;
        c++;
      } else {
        ans[i] = 'C';
        a++;
        c--;
      }
    }
    if (s[i] == "BC") {
      if (b && dp[i][a][b - 1][c + 1]) {
        ans[i] = 'B';
        b--;
        c++;
      } else {
        ans[i] = 'C';
        b++;
        c--;
      }
    }
  }
  cout << "Yes"
       << "\\n";
  for (int i = 0; i < n; i++)
    cout << ans[i] << "\\n";
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
template <class T> using V = vector<T>;
#define fi first
#define se second
#define all(v) (v).begin(), (v).end()
const ll inf = (1e18);
// const ll mod=998244353;
const ll mod = 1000000007;
int GCD(ll a, ll b) { return b ? GCD(b, a % b) : a; }
ll LCM(ll c, ll d) { return c / GCD(c, d) * d; }
struct __INIT {
  __INIT() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(15);
  }
} __init;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
bool dp[200005][5][5][5];
int main() {
  ll n, a, b, c;
  cin >> n >> a >> b >> c;
  V<string> s(n);
  for (int i = 0; i < n; i++)
    cin >> s[i];
  V<char> ans(n);
  bool ok = true;
  for (int i = 0; i < n; i++) {
    if (s[i] == "AC") {
      if (a == 0 && c == 0)
        ok = false;
      if (a < c) {
        a++;
        c--;
        ans[i] = 'A';
      } else if (a >= c) {
        a--;
        c++;
        ans[i] = 'C';
      }
    } else if (s[i] == "BC") {
      if (b == 0 && c == 0)
        ok = false;
      if (b > c) {
        b--;
        c++;
        ans[i] = 'C';
      } else if (b <= c) {
        b++;
        c--;
        ans[i] = 'B';
      }
    } else if (s[i] == "AB") {
      if (a == 0 && b == 0)
        ok = false;
      if (a < b) {
        a++;
        b--;
        ans[i] = 'A';
      } else if (a >= b) {
        a--;
        b++;
        ans[i] = 'B';
      }
    }
  }
  if (ok) {
    cout << "Yes"
         << "\\n";
    for (int i = 0; i < n; i++)
      cout << ans[i] << "\\n";
    return 0;
  }
  if (a + b + c > 3) {
    cout << "No"
         << "\\n";
    return 0;
  }
  dp[0][a][b][c] = true;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 4; j++) {
      for (int k = 0; k < 4; k++) {
        for (int r = 0; r < 4; r++) {
          if (s[i] == "AB") {
            if (j)
              dp[i + 1][j - 1][k + 1][r] |= dp[i][j][k][r];
            if (k)
              dp[i + 1][j + 1][k - 1][r] |= dp[i][j][k][r];
          }
          if (s[i] == "AC") {
            if (j)
              dp[i + 1][j - 1][k][r + 1] |= dp[i][j][k][r];
            if (r)
              dp[i + 1][j + 1][k][r - 1] |= dp[i][j][k][r];
          }
          if (s[i] == "BC") {
            if (k)
              dp[i + 1][j][k - 1][r + 1] |= dp[i][j][k][r];
            if (r)
              dp[i + 1][j][k + 1][r - 1] |= dp[i][j][k][r];
          }
        }
      }
    }
  }
  ok = false;
  for (a = 0; a < 4; a++) {
    for (b = 0; b < 4; b++) {
      for (c = 0; c < 4; c++) {
        if (dp[n][a][b][c]) {
          ok = true;
          break;
        }
      }
      if (ok)
        break;
    }
    if (ok)
      break;
  }
  if (!ok) {
    exit(1);
    cout << "No"
         << "\\n";
    return 0;
  }
  for (int i = n - 1; i >= 0; i--) {
    if (s[i] == "AB") {
      if (a && dp[i][a - 1][b + 1][c]) {
        ans[i] = 'A';
        a--;
        b++;
      } else {
        ans[i] = 'B';
        a++;
        b--;
      }
    }
    if (s[i] == "AC") {
      if (a && dp[i][a - 1][b][c + 1]) {
        ans[i] = 'A';
        a--;
        c++;
      } else {
        ans[i] = 'C';
        a++;
        c--;
      }
    }
    if (s[i] == "BC") {
      if (b && dp[i][a][b - 1][c + 1]) {
        ans[i] = 'B';
        b--;
        c++;
      } else {
        ans[i] = 'C';
        b++;
        c--;
      }
    }
  }
  cout << "Yes"
       << "\\n";
  for (int i = 0; i < n; i++)
    cout << ans[i] << "\\n";
}