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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long int;
using vec = vector<int>;
using P = pair<int, int>;

int main() {
  int n, a, b, c;
  cin >> n >> a >> b >> c;
  vector<string> s(n);
  rep(i, n) cin >> s[i];

  if (a + b + c == 0 || s[0] == "AB" && a + b == 0 ||
      s[0] == "AC" && a + c == 0 || s[0] == "BC" && b + c == 0) {
    cout << "No" << endl;
  } else if (a + b + c == 1) {
    string t = "";
    rep(i, n) {
      if (s[i] == "AB") {
        if (a < b) {
          a++;
          b--;
          t.push_back('A');
        } else {
          a--;
          b++;
          t.push_back('B');
        }
      } else if (s[i] == "AC") {
        if (a < c) {
          a++;
          c--;
          t.push_back('A');
        } else {
          a--;
          c++;
          t.push_back('C');
        }
      } else if (s[i] == "BC") {
        if (b < c) {
          b++;
          c--;
          t.push_back('B');
        } else {
          c++;
          b--;
          t.push_back('C');
        }
      }
      if (a < 0 || b < 0 || c < 0) {
        cout << "No" << endl;
        return 0;
      }
    }
    cout << "Yes" << endl;
    rep(i, n) { cout << t[i] << endl; }
  } else {
    cout << "Yes" << endl;
    rep(i, n) {
      if (s[i] == "AB") {
        if (a < b ||
            a == 1 && b == 1 && c == 0 && i != n - 1 && s[i + 1] == "AC") {
          a++;
          b--;
          cout << "A" << endl;
        } else {
          a--;
          b++;
          cout << "B" << endl;
        }
      } else if (s[i] == "AC") {
        if (a < c ||
            a == 1 && b == 0 && c == 1 && i != n - 1 && s[i + 1] == "AB") {
          a++;
          c--;
          cout << "A" << endl;
        } else {
          a--;
          c++;
          cout << "C" << endl;
        }
      } else if (s[i] == "BC") {
        if (b < c ||
            a == 0 && b == 1 && c == 1 && i != n - 1 && s[i + 1] == "AB") {
          b++;
          c--;
          cout << "B" << endl;
        } else {
          c++;
          b--;
          cout << "C" << endl;
        }
      }
    }
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long int;
using vec = vector<int>;
using P = pair<int, int>;

bool dfs(int n, int a, int b, int c, int i, vector<string> s, string &res) {
  if (a < 0 || b < 0 || c < 0) {
    return false;
  }
  if (i == n - 1) {
    if (s[i] == "AB") {
      if (a > 0) {
        res.push_back('B');
        return true;
      } else if (b > 0) {
        res.push_back('A');
        return true;
      }
    } else if (s[i] == "AC") {
      if (a > 0) {
        res.push_back('C');
        return true;
      } else if (c > 0) {
        res.push_back('A');
        return true;
      }
    } else {
      if (b > 0) {
        res.push_back('C');
        return true;
      } else if (c > 0) {
        res.push_back('B');
        return true;
      }
    }
  }
  if (s[i] == "AB") {
    if (dfs(n, a + 1, b - 1, c, i + 1, s, res)) {
      res.push_back('A');
      return true;
    } else if (dfs(n, a - 1, b + 1, c, i + 1, s, res)) {
      res.push_back('B');
      return true;
    }
  } else if (s[i] == "AC") {
    if (dfs(n, a + 1, b, c - 1, i + 1, s, res)) {
      res.push_back('A');
      return true;
    } else if (dfs(n, a - 1, b, c + 1, i + 1, s, res)) {
      res.push_back('C');
      return true;
    }
  } else {
    if (dfs(n, a, b + 1, c - 1, i + 1, s, res)) {
      res.push_back('B');
      return true;
    } else if (dfs(n, a, b + 1, c - 1, i + 1, s, res)) {
      res.push_back('C');
      return true;
    }
  }
}

int main() {
  int n, a, b, c;
  cin >> n >> a >> b >> c;
  vector<string> s(n);
  rep(i, n) cin >> s[i];

  string res = "";

  if (dfs(n, a, b, c, 0, s, res)) {
    cout << "Yes" << endl;
    rep(i, n) { cout << res[n - i - 1] << endl; }
  } else {
    cout << "No" << endl;
  }
}