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

#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long ll;
int n, A, B, C, P = 1;
char str[5], ans[100005];
char ss[100005][5];
void SolveRR() {
  for (int i = P; i <= n; i++) {
    scanf("%s", ss[i]);
  }
  for (int i = P; i <= n; i++) {
    if (ss[i][0] == 'A' && ss[i][1] == 'B') {
      if (max(A, B) <= 0)
        return puts("No"), void();
      if (A == 0)
        ans[i] = 'A', A++, B--;
      else if (B == 0)
        ans[i] = 'B', B++, A--;
      else if (i == n || (ss[i + 1][0] == 'A' && ss[i + 1][1] == 'B')) {
        if (i % 2 == 0)
          ans[i] = 'A', A++, B--;
        else
          ans[i] = 'B', A--, B++;
      } else if (ss[i + 1][0] == 'A')
        ans[i] = 'A', B--, A++;
      else
        ans[i] = 'B', A--, B++;
    }
    if (ss[i][0] == 'A' && ss[i][1] == 'C') {
      if (max(A, C) <= 0)
        return puts("No"), void();
      if (A == 0)
        ans[i] = 'A', A++, C--;
      else if (C == 0)
        ans[i] = 'C', C++, A--;
      else if (i == n || (ss[i + 1][0] == 'A' && ss[i + 1][1] == 'C')) {
        if (i % 2 == 0)
          ans[i] = 'A', A++, C--;
        else
          ans[i] = 'C', A--, C++;
      } else if (ss[i + 1][0] == 'A')
        ans[i] = 'A', C--, A++;
      else
        ans[i] = 'C', A--, C++;
    }
    if (ss[i][0] == 'B' && ss[i][1] == 'C') {
      if (max(B, C) <= 0)
        return puts("No"), void();
      if (B == 0)
        ans[i] = 'B', B++, C--;
      else if (C == 0)
        ans[i] = 'C', C++, B--;
      else if (i == n || (ss[i + 1][0] == 'B' && ss[i + 1][1] == 'C')) {
        if (i % 2 == 0)
          ans[i] = 'B', B++, C--;
        else
          ans[i] = 'C', B--, C++;
      } else if (ss[i + 1][0] == 'B' || ss[i + 1][1] == 'B')
        ans[i] = 'B', C--, B++;
      else
        ans[i] = 'C', B--, C++;
    }
  }
  puts("Yes");
  for (int i = 1; i <= n; i++)
    cout << ans[i] << '\\n';
}
int main() {
  cin >> n >> A >> B >> C;
  int cnt = 0;
  if (A)
    cnt++;
  if (B)
    cnt++;
  if (C)
    cnt++;
  if (cnt > 1) {
    SolveRR();
    return 0;
  }
  if (!cnt)
    return puts("No"), 0;
  scanf("%s", str);
  if (str[0] == 'A' && str[1] == 'B') {
    if (max(A, B) <= 0)
      return puts("No"), 0;
    if (A < B)
      ans[1] = 'A', A++, B--;
    else
      ans[1] = 'B', B++, A--;
  }
  if (str[0] == 'A' && str[1] == 'C') {
    if (max(A, C) <= 0)
      return puts("No"), 0;
    if (A < C)
      ans[1] = 'A', A++, C--;
    else
      ans[1] = 'C', C++, A--;
  }
  if (str[0] == 'B' && str[1] == 'C') {
    if (max(B, C) <= 0)
      return puts("No"), 0;
    if (B < C)
      ans[1] = 'B', B++, C--;
    else
      ans[1] = 'C', C++, B--;
  }
  P = 2;
  SolveRR();
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
typedef long long ll;
int n, A, B, C, P = 1;
char str[5], ans[100005];
char ss[100005][5];
void SolveRR() {
  for (int i = P; i <= n; i++) {
    scanf("%s", ss[i]);
  }
  for (int i = P; i <= n; i++) {
    if (ss[i][0] == 'A' && ss[i][1] == 'B') {
      if (max(A, B) <= 0)
        return puts("No"), assert(0), void();
      if (A == 0)
        ans[i] = 'A', A++, B--;
      else if (B == 0)
        ans[i] = 'B', B++, A--;
      else if (i == n || (ss[i + 1][0] == 'A' && ss[i + 1][1] == 'B')) {
        if (i % 2 == 0)
          ans[i] = 'A', A++, B--;
        else
          ans[i] = 'B', A--, B++;
      } else if (ss[i + 1][0] == 'A')
        ans[i] = 'A', B--, A++;
      else
        ans[i] = 'B', A--, B++;
    }
    if (ss[i][0] == 'A' && ss[i][1] == 'C') {
      if (max(A, C) <= 0)
        return puts("No"), assert(0), void();
      if (A == 0)
        ans[i] = 'A', A++, C--;
      else if (C == 0)
        ans[i] = 'C', C++, A--;
      else if (i == n || (ss[i + 1][0] == 'A' && ss[i + 1][1] == 'C')) {
        if (i % 2 == 0)
          ans[i] = 'A', A++, C--;
        else
          ans[i] = 'C', A--, C++;
      } else if (ss[i + 1][0] == 'A')
        ans[i] = 'A', C--, A++;
      else
        ans[i] = 'C', A--, C++;
    }
    if (ss[i][0] == 'B' && ss[i][1] == 'C') {
      if (max(B, C) <= 0)
        return puts("No"), assert(0), void();
      if (B == 0)
        ans[i] = 'B', B++, C--;
      else if (C == 0)
        ans[i] = 'C', C++, B--;
      else if (i == n || (ss[i + 1][0] == 'B' && ss[i + 1][1] == 'C')) {
        if (i % 2 == 0)
          ans[i] = 'B', B++, C--;
        else
          ans[i] = 'C', B--, C++;
      } else if (ss[i + 1][0] == 'B' || ss[i + 1][1] == 'B')
        ans[i] = 'B', C--, B++;
      else
        ans[i] = 'C', B--, C++;
    }
  }
  puts("Yes");
  for (int i = 1; i <= n; i++)
    cout << ans[i] << '\\n';
}
int main() {
  cin >> n >> A >> B >> C;
  int cnt = 0;
  if (A)
    cnt++;
  if (B)
    cnt++;
  if (C)
    cnt++;
  if (cnt > 1) {
    SolveRR();
    return 0;
  }
  if (!cnt)
    return puts("No"), 0;
  scanf("%s", str);
  if (str[0] == 'A' && str[1] == 'B') {
    if (max(A, B) <= 0)
      return puts("No"), 0;
    if (A < B)
      ans[1] = 'A', A++, B--;
    else
      ans[1] = 'B', B++, A--;
  }
  if (str[0] == 'A' && str[1] == 'C') {
    if (max(A, C) <= 0)
      return puts("No"), 0;
    if (A < C)
      ans[1] = 'A', A++, C--;
    else
      ans[1] = 'C', C++, A--;
  }
  if (str[0] == 'B' && str[1] == 'C') {
    if (max(B, C) <= 0)
      return puts("No"), 0;
    if (B < C)
      ans[1] = 'B', B++, C--;
    else
      ans[1] = 'C', C++, B--;
  }
  P = 2;
  SolveRR();
}