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

#include <bits/stdc++.h>
#define ff first
#define ss second
#define IO                                                                     \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
using namespace std;
typedef long long ll;
void func(int &x, int &y) { x += 1, y -= 1; }

int main() {
  IO;
  int n, a, b, c;
  cin >> n >> a >> b >> c;
  vector<string> v(n);
  for (string &x : v)
    cin >> x;
  if (a == 0 && b == 0 && c == 0) {
    cout << "No\\n";
    return 0;
  }
  if (a + b + c == 1) {
    vector<char> ans;
    for (int i = 0; i < n; i++) {
      if (v[i] == "AB") {
        if (a + b == 0) {
          cout << "No\\n";
          return 0;
        }
        if (a == 0)
          func(a, b), ans.push_back('A');
        else
          func(b, a), ans.push_back('B');
      } else if (v[i] == "AC") {
        if (a + c == 0) {
          cout << "No\\n";
          return 0;
        }
        if (a == 0)
          func(a, c), ans.push_back('A');
        else
          func(c, a), ans.push_back('C');
      } else if (v[i] == "BC") {
        if (b + c == 0) {
          cout << "No\\n";
          return 0;
        }
        if (c == 0)
          func(c, b), ans.push_back('C');
        else
          func(b, c), ans.push_back('B');
      }
    }

    cout << "Yes\\n";
    for (char &x : ans)
      cout << x << '\\n';
  } else {
    vector<char> ans;
    if (v[0] == "AB") {
      if (a + b == 0) {
        cout << "No\\n";
        return 0;
      }
    } else if (v[0] == "AC") {
      if (a + c == 0) {
        cout << "No\\n";
        return 0;
      }
    } else {
      if (b + c == 0) {
        cout << "No\\n";
        return 0;
      }
    }
    for (int i = 0; i < n; i++) {
      if (v[i] == "AB") {
        if (a == 1 && b == 1) {
          if (i != n - 1 && v[i + 1] == "BC")
            func(b, a), ans.push_back('B');
          else
            func(a, b), ans.push_back('A');
        } else if (a == 0 || b == 0) {
          if (a == 0)
            func(a, b), ans.push_back('A');
          else
            func(b, a), ans.push_back('B');
        } else {
          if (a < b)
            func(a, b), ans.push_back('A');
          else
            func(b, a), ans.push_back('B');
        }
      } else if (v[i] == "AC") {
        if (a == 1 && c == 1) {
          if (i != n - 1 && v[i + 1] == "BC")
            func(c, a), ans.push_back('C');
          else
            func(a, c), ans.push_back('A');
        } else if (a == 0 || c == 0) {
          if (a == 0)
            func(a, c), ans.push_back('A');
          else
            func(c, a), ans.push_back('C');
        } else {
          if (c > a)
            func(a, c), ans.push_back('A');
          else
            func(c, a), ans.push_back('C');
        }
      } else {
        if (c == 1 && b == 1) {
          if (i != n - 1 && v[i + 1] == "AC")
            func(c, b), ans.push_back('C');
          else
            func(b, c), ans.push_back('B');
        } else if (b == 0 || c == 0) {
          if (b == 0)
            func(b, c), ans.push_back('B');
          else
            func(c, b), ans.push_back('C');
        } else {
          if (b < c)
            func(b, c), ans.push_back('B');
          else
            func(c, b), ans.push_back('C');
        }
      }
    }
    cout << "Yes\\n";
    for (char &x : ans)
      cout << x << '\\n';
  }

  assert(a >= 0 && b >= 0 && c >= 0);
  return 0;
}

/*
7 1 1 1
AB
AC
BC
AB
AB
BC
BC

*/

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ff first
#define ss second
#define IO                                                                     \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
using namespace std;
typedef long long ll;
void func(int &x, int &y) {
  if (x + y == 1) {
    swap(x, y);
  } else
    x += 1, y -= 1;
}

int main() {
  IO;
  int n, a, b, c;
  cin >> n >> a >> b >> c;
  vector<string> v(n);
  for (string &x : v)
    cin >> x;
  if (a == 0 && b == 0 && c == 0) {
    cout << "No\\n";
    return 0;
  }
  if (a + b + c == 1) {
    vector<char> ans;
    for (int i = 0; i < n; i++) {
      if (v[i] == "AB") {
        if (a + b == 0) {
          cout << "No\\n";
          return 0;
        }
        if (a == 0)
          func(a, b), ans.push_back('A');
        else
          func(b, a), ans.push_back('B');
      } else if (v[i] == "AC") {
        if (a + c == 0) {
          cout << "No\\n";
          return 0;
        }
        if (a == 0)
          func(a, c), ans.push_back('A');
        else
          func(c, a), ans.push_back('C');
      } else if (v[i] == "BC") {
        if (b + c == 0) {
          cout << "No\\n";
          return 0;
        }
        if (c == 0)
          func(c, b), ans.push_back('C');
        else
          func(b, c), ans.push_back('B');
      }
    }

    cout << "Yes\\n";
    for (char &x : ans)
      cout << x << '\\n';
  } else {
    vector<char> ans;
    if (v[0] == "AB") {
      if (a + b == 0) {
        cout << "No\\n";
        return 0;
      }
    } else if (v[0] == "AC") {
      if (a + c == 0) {
        cout << "No\\n";
        return 0;
      }
    } else {
      if (b + c == 0) {
        cout << "No\\n";
        return 0;
      }
    }
    for (int i = 0; i < n; i++) {
      if (v[i] == "AB") {
        if (a == 1 && b == 1) {
          if (i != n - 1 && v[i + 1] == "BC")
            func(b, a), ans.push_back('B');
          else
            func(a, b), ans.push_back('A');
        } else if (a == 0 || b == 0) {
          if (a == 0)
            func(a, b), ans.push_back('A');
          else
            func(b, a), ans.push_back('B');
        } else {
          func(a, b);
          ans.push_back('A');
        }
      } else if (v[i] == "AC") {
        if (a == 1 && c == 1) {
          if (i != n - 1 && v[i + 1] == "BC")
            func(c, a), ans.push_back('C');
          else
            func(a, c), ans.push_back('A');
        } else if (a == 0 || c == 0) {
          if (a == 0)
            func(a, c), ans.push_back('A');
          else
            func(c, a), ans.push_back('C');
        } else {
          func(a, c);
          ans.push_back('A');
        }
      } else {
        if (c == 1 && b == 1) {
          if (i != n - 1 && v[i + 1] == "AC")
            func(c, b), ans.push_back('C');
          else
            func(b, c), ans.push_back('B');
        } else if (b == 0 || c == 0) {
          if (b == 0)
            func(b, c), ans.push_back('B');
          else
            func(c, b), ans.push_back('C');
        } else {
          func(b, c);
          ans.push_back('B');
        }
      }
    }
    cout << "Yes\\n";
    for (char &x : ans)
      cout << x << '\\n';
  }

  assert(a >= 0 && b >= 0 && c >= 0);
  return 0;
}