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

#include <bits/stdc++.h>
#define INF 1000000000
#define LINF 1000000000000000000
#define MOD 1000000007
#define mod 1000007
#define INF63 1061109567
#define INF127 9187201950435737471
#define UINF 18446744073709551615
#define F first
#define S second
#define ll long long
#define N 100010
using namespace std;
ll n, a, b, c, nxt[3], nx[N];
string ans = "", ss[N];
int main() {
  ll i, j;
  cin >> n >> a >> b >> c;
  for (i = 0; i < n; i++) {
    cin >> ss[i];
  }
  nxt[0] = nxt[1] = nxt[2] = n;
  for (i = n - 1; i >= 0; i--) {
    if (ss[i] == "AB") {
      nx[i] = min(nxt[1], nxt[2]);
      nxt[0] = i;
    } else if (ss[i] == "AC") {
      nx[i] = min(nxt[0], nxt[2]);
      nxt[1] = i;
    } else {
      nx[i] = min(nxt[0], nxt[1]);
      nxt[2] = i;
    }
  }
  for (i = 0; i < n; i++) {
    if (ss[i] == "AB") {
      if (a == b && a == 0) {
        puts("No");
        return 0;
      }
      if (a < b) {
        ans += "A";
        a++;
        b--;
      } else if (a > b) {
        ans += "B";
        a--;
        b++;
      } else {
        j = nx[i];
        if (j != n) {
          if (ss[j] == "BC") {
            ans += "B";
            a--;
            b++;
          } else {
            ans += "A";
            a++;
            b--;
          }
        } else {
          ans += "A";
          a++;
          b--;
        }
      }
    } else if (ss[i] == "AC") {
      if (a == c && a == 0) {
        puts("No");
        return 0;
      }
      if (a < c) {
        ans += "A";
        a++;
        c--;
      } else if (a > c) {
        ans += "C";
        a--;
        c++;
      } else {
        j = nx[i];
        if (j != n) {
          if (ss[j] == "BC") {
            ans += "C";
            a--;
            c++;
          } else {
            ans += "A";
            a++;
            c--;
          }
        } else {
          ans += "A";
          a++;
          c--;
        }
      }
    } else {
      if (b == c && b == 0) {
        puts("No");
        return 0;
      }
      if (b < c) {
        ans += "B";
        b++;
        c--;
      } else if (b > c) {
        ans += "C";
        b--;
        c++;
      } else {
        j = nx[i];
        if (j != n) {
          if (ss[j] == "AC") {
            ans += "C";
            b--;
            c++;
          } else {
            ans += "B";
            b++;
            c--;
          }
        } else {
          ans += "B";
          b++;
          c--;
        }
      }
    }
    if (a < 0 || b < 0 || c < 0) {
      puts("No");
      return 0;
    }
  }
  puts("Yes");
  for (i = 0; i < ans.size(); i++) {
    cout << ans[i] << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define INF 1000000000
#define LINF 1000000000000000000
#define MOD 1000000007
#define mod 1000007
#define INF63 1061109567
#define INF127 9187201950435737471
#define UINF 18446744073709551615
#define F first
#define S second
#define ll long long
#define N 100010
using namespace std;
ll n, a, b, c, nxt[3], nx[N];
string ans = "", ss[N];
int main() {
  ll i, j;
  cin >> n >> a >> b >> c;
  for (i = 0; i < n; i++) {
    cin >> ss[i];
  }
  nxt[0] = nxt[1] = nxt[2] = n;
  for (i = n - 1; i >= 0; i--) {
    if (ss[i] == "AB") {
      nx[i] = min(nxt[1], nxt[2]);
      nxt[0] = i;
    } else if (ss[i] == "AC") {
      nx[i] = min(nxt[0], nxt[2]);
      nxt[1] = i;
    } else {
      nx[i] = min(nxt[0], nxt[1]);
      nxt[2] = i;
    }
  }
  for (i = 0; i < n; i++) {
    if (ss[i] == "AB") {
      if (a == b && a == 0) {
        puts("No");
        return 0;
      }
      if (a < b) {
        ans += "A";
        a++;
        b--;
      } else if (a > b) {
        ans += "B";
        a--;
        b++;
      } else {
        j = nxt[i];
        if (j != n) {
          if (ss[j] == "BC") {
            ans += "B";
            a--;
            b++;
          } else {
            ans += "A";
            a++;
            b--;
          }
        } else {
          ans += "A";
          a++;
          b--;
        }
      }
    } else if (ss[i] == "AC") {
      if (a == c && a == 0) {
        puts("No");
        return 0;
      }
      if (a < c) {
        ans += "A";
        a++;
        c--;
      } else if (a > c) {
        ans += "C";
        a--;
        c++;
      } else {
        j = nxt[i];
        if (j != n) {
          if (ss[j] == "BC") {
            ans += "C";
            a--;
            c++;
          } else {
            ans += "A";
            a++;
            c--;
          }
        } else {
          ans += "A";
          a++;
          c--;
        }
      }
    } else {
      if (b == c && b == 0) {
        puts("No");
        return 0;
      }
      if (b < c) {
        ans += "B";
        b++;
        c--;
      } else if (b > c) {
        ans += "C";
        b--;
        c++;
      } else {
        j = nxt[i];
        if (j != n) {
          if (ss[j] == "AC") {
            ans += "C";
            b--;
            c++;
          } else {
            ans += "B";
            b++;
            c--;
          }
        } else {
          ans += "B";
          b++;
          c--;
        }
      }
    }
    if (a < 0 || b < 0 || c < 0) {
      puts("No");
      return 0;
    }
  }
  puts("Yes");
  for (i = 0; i < ans.size(); i++) {
    cout << ans[i] << endl;
  }
  return 0;
}