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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, n) for (int i = 1; i <= (int)(n); i++)
#define ll long long
#define umap unordered_map
using graph = vector<vector<int>>;
using graph2 = vector<vector<pair<int, int>>>;

ll mod(ll x, ll y) {
  if (x >= 0 || x % y == 0)
    return x % y;
  return y + x % y;
} // mod including minus
ll dv0(ll x, ll y) {
  if (x >= 0 || x % y == 0)
    return x / y;
  return x / y - 1;
} // rnd down
ll dv1(ll x, ll y) {
  if (x % y == 0)
    return dv0(x, y);
  return dv0(x, y) + 1;
} // rnd up
ll M = 1e9 + 7;
int quick_pow(int x, int p) {
  int a = 1, po = x;
  while (p) {
    if (p & 1)
      a = 1ll * a * po % M;
    po = 1ll * po * po % M;
    p >>= 1;
  }
  return a;
}
int N;
bool dfs(vector<string> &S, int A, int B, int C, int i, string &ans) {
  if (i == N) {
    if (A >= 0 && B >= 0 && C >= 0) {
      return true;
    } else {
      return false;
    }
  } else if (!(A >= 0 && B >= 0 && C >= 0)) {
    return false;
  } else {
    if (S.at(i) == "AB") {
      if (A > B) {
        ans.at(i) = 'B';
        return dfs(S, A - 1, B + 1, C, i + 1, ans);
      }
      if (B > A) {
        ans.at(i) = 'A';
        return dfs(S, A + 1, B - 1, C, i + 1, ans);
      }
      if (dfs(S, A + 1, B - 1, C, i + 1, ans)) {
        ans.at(i) = 'A';
        return true;
      } else if (dfs(S, A - 1, B + 1, C, i + 1, ans)) {
        ans.at(i) = 'B';
        return true;
      } else {
        return false;
      }
    } else if (S.at(i) == "BC") {
      if (B > C) {
        ans.at(i) = 'C';
        return dfs(S, A, B - 1, C + 1, i + 1, ans);
      }
      if (C > B) {
        ans.at(i) = 'B';
        return dfs(S, A, B + 1, C - 1, i + 1, ans);
      }
      if (dfs(S, A, B + 1, C - 1, i + 1, ans)) {
        ans.at(i) = 'B';
        return true;
      } else if (dfs(S, A, B - 1, C + 1, i + 1, ans)) {
        ans.at(i) = 'C';
        return true;
      } else {
        return false;
      }
    } else {
      if (C > A) {
        ans.at(i) = 'A';
        return dfs(S, A + 1, B, C - 1, i + 1, ans);
      }
      if (A > C) {
        ans.at(i) = 'C';
        return dfs(S, A - 1, B, C + 1, i + 1, ans);
      }
      if (dfs(S, A - 1, B, C + 1, i + 1, ans)) {
        ans.at(i) = 'C';
        return true;
      } else if (dfs(S, A + 1, B, C - 1, i + 1, ans)) {
        ans.at(i) = 'A';
        return true;
      } else {
        return false;
      }
    }
  }
}
// Start from Here
int main() {
  int A, B, C;
  cin >> N >> A >> B >> C;
  vector<string> S;
  string c;
  rep(i, N) {
    cin >> c;
    S.push_back(c);
  }
  string ans;
  rep(i, N) { ans += "A"; }
  if (dfs(S, A, B, C, 0, ans)) {
    cout << "Yes" << endl;
    rep(i, N) { cout << ans.at(i) << endl; }
  } else {
    cout << "No" << endl;
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, n) for (int i = 1; i <= (int)(n); i++)
#define ll long long
#define umap unordered_map
using graph = vector<vector<int>>;
using graph2 = vector<vector<pair<int, int>>>;

ll mod(ll x, ll y) {
  if (x >= 0 || x % y == 0)
    return x % y;
  return y + x % y;
} // mod including minus
ll dv0(ll x, ll y) {
  if (x >= 0 || x % y == 0)
    return x / y;
  return x / y - 1;
} // rnd down
ll dv1(ll x, ll y) {
  if (x % y == 0)
    return dv0(x, y);
  return dv0(x, y) + 1;
} // rnd up
ll M = 1e9 + 7;
int quick_pow(int x, int p) {
  int a = 1, po = x;
  while (p) {
    if (p & 1)
      a = 1ll * a * po % M;
    po = 1ll * po * po % M;
    p >>= 1;
  }
  return a;
}
// Start from Here
int main() {
  int N, A, B, C;
  cin >> N >> A >> B >> C;
  string S[N];
  rep(i, N) { cin >> S[i]; }
  if (A + B + C >= 3) {
    cout << "Yes" << endl;
    rep(i, N) {
      if (S[i] == "AB") {
        if (A > B) {
          cout << "B" << endl;
        } else {
          cout << "A" << endl;
        }
      } else if (S[i] == "BC") {
        if (B > C) {
          cout << "C" << endl;
        } else {
          cout << "B" << endl;
        }
      } else {
        if (A > C) {
          cout << "C" << endl;
        } else {
          cout << "A" << endl;
        }
      }
    }
  } else if (A + B + C == 0) {
    cout << "No" << endl;
  } else {
    vector<string> T;
    int check = 0;
    rep(i, N) {
      if (S[i] == "AB") {
        if (A == 0 && B == 0) {
          cout << "NO" << endl;
          check = 1;
          break;
        }
        if (A > B) {
          T.push_back("B");
          B += 1;
          A -= 1;
        } else {
          T.push_back("A");
          A += 1;
          B -= 1;
        }
      } else if (S[i] == "BC") {
        if (C == 0 && B == 0) {
          cout << "NO" << endl;
          check = 1;
          break;
        }
        if (B > C) {
          T.push_back("C");
          C += 1;
          B -= 1;
        } else {
          T.push_back("B");
          B += 1;
          C -= 1;
        }
      } else {
        if (C == 0 && A == 0) {
          cout << "NO" << endl;
          check = 1;
          break;
        }
        if (C > A) {
          T.push_back("A");
          A += 1;
          C -= 1;
        } else {
          T.push_back("C");
          C += 1;
          A -= 1;
        }
      }
      if (check == 0) {
        cout << "YES" << endl;
        rep(i, N) { cout << T.at(i) << endl; }
      }
    }
  }
}