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

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const ll MOD = 1000000007;
const long double PI = 3.14159265358979;

ll f(ll a, ll b) { return a * a * a * a * a - b * b * b * b * b; }

int main() {
  ll N, A, B, C, cnt = 1;
  cin >> N >> A >> B >> C;
  char c[N];
  string S[N];
  for (int i = 0; i < N; i++) {
    cin >> S[i];
  }
  for (int i = 0; i < N; i++) {
    string s;
    s = S[i];
    if (s == "AB") {
      if (A == 0 && B == 0) {
        cout << "No";
        cnt = 0;
        break;
      }
      if (A == 1 && B == 1 && C == 0 && i != N - 1) {
        string s2 = S[i + 1];
        if (s2 == "AC") {
          A++;
          B--;
          c[i] = 'A';
          continue;
        }
        if (s2 == "BC") {
          A--;
          B++;
          c[i] = 'B';
          continue;
        }
      }
      if (A > B) {
        A--;
        B++;
        c[i] = 'B';
      } else {
        A++;
        B--;
        c[i] = 'A';
      }
    }
    if (s == "BC") {
      if (C == 0 && B == 0) {
        cout << "No";
        cnt = 0;
        break;
      }
      if (A == 0 && B == 1 && C == 1 && i != N - 1) {
        string s2 = S[i + 1];
        if (s2 == "AC") {
          C++;
          B--;
          c[i] = 'C';
          continue;
        }
        if (s2 == "AB") {
          C--;
          B++;
          c[i] = 'B';
          continue;
        }
      }

      if (C > B) {
        C--;
        B++;
        c[i] = 'B';
      } else {
        C++;
        B--;
        c[i] = 'C';
      }
    }
    if (s == "AC") {
      if (A == 0 && C == 0) {
        cout << "No";
        cnt = 0;
        break;
      }
      if (A == 1 && B == 0 && C == 1 && i != N - 1) {
        string s2 = S[i + 1];
        if (s2 == "AB") {
          A++;
          C--;
          c[i] = 'A';
          continue;
        }
        if (s2 == "BC") {
          A--;
          C++;
          c[i] = 'C';
          continue;
        }
      }
      if (A > C) {
        A--;
        C++;
        c[i] = 'C';
      } else {
        A++;
        C--;
        c[i] = 'A';
      }
    }
  }
  if (cnt) {
    cout << "Yes" << endl;
    for (int i = 0; i < N; i++) {
      cout << c[i] << endl;
    }
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const ll MOD = 1000000007;
const long double PI = 3.14159265358979;

ll f(ll a, ll b) { return a * a * a * a * a - b * b * b * b * b; }

int main() {
  ll N, A, B, C, cnt = 1;
  cin >> N >> A >> B >> C;
  char c[N];
  string S[N];
  for (int i = 0; i < N; i++) {
    cin >> S[i];
  }
  for (int i = 0; i < N; i++) {
    string s;
    s = S[i];
    if (s == "AB") {
      if (A == 0 && B == 0) {
        cout << "No";
        cnt = 0;
        break;
      }
      if (A == 1 && B == 1 && C == 0) {
        string s2 = S[i + 1];
        if (s2 == "AC") {
          A++;
          B--;
          c[i] = 'A';
          continue;
        }
        if (s2 == "BC") {
          A--;
          B++;
          c[i] = 'B';
          continue;
        }
      }
      if (A > B) {
        A--;
        B++;
        c[i] = 'B';
      } else {
        A++;
        B--;
        c[i] = 'A';
      }
    }
    if (s == "BC") {
      if (C == 0 && B == 0) {
        cout << "No";
        cnt = 0;
        break;
      }
      if (A == 0 && B == 1 && C == 1) {
        string s2 = S[i + 1];
        if (s2 == "AC") {
          C++;
          B--;
          c[i] = 'C';
          continue;
        }
        if (s2 == "AB") {
          C--;
          B++;
          c[i] = 'B';
          continue;
        }
      }

      if (C > B) {
        C--;
        B++;
        c[i] = 'B';
      } else {
        C++;
        B--;
        c[i] = 'C';
      }
    }
    if (s == "AC") {
      if (A == 0 && C == 0) {
        cout << "No";
        cnt = 0;
        break;
      }
      if (A == 1 && B == 0 && C == 1) {
        string s2 = S[i + 1];
        if (s2 == "AB") {
          A++;
          C--;
          c[i] = 'A';
          continue;
        }
        if (s2 == "BC") {
          A--;
          C++;
          c[i] = 'C';
          continue;
        }
      }
      if (A > C) {
        A--;
        C++;
        c[i] = 'C';
      } else {
        A++;
        C--;
        c[i] = 'A';
      }
    }
  }
  if (cnt) {
    cout << "Yes" << endl;
    for (int i = 0; i < N; i++) {
      cout << c[i] << endl;
    }
  }
  return 0;
}