Problem p02692 (C++)

Buggy submission (status: Runtime Error):
/*** author: yuji9511 ***/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using lpair = pair<ll, ll>;
const ll MOD = 1e9 + 7;
const ll INF = 1e18;
#define rep(i, m, n) for (ll i = (m); i < (n); i++)
#define rrep(i, m, n) for (ll i = (m); i >= (n); i--)
#define printa(x, n)                                                           \
  for (ll i = 0; i < n; i++) {                                                 \
    cout << (x[i]) << " \n"[i == n - 1];                                       \
  };
void print() {}
template <class H, class... T> void print(H &&h, T &&...t) {
  cout << h << " \n"[sizeof...(t) == 0];
  print(forward<T>(t)...);
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll N, A, B, C;
  cin >> N >> A >> B >> C;
  string S[100010];
  rep(i, 0, N) cin >> S[i];
  if (A == 0 && B == 0 && C == 0) {
    print("No");
  } else if (A + B + C == 1) {
    bool ok = true;
    vector<char> ans;
    rep(i, 0, N) {
      if (S[i] == "AB") {
        if (A == 0 && B == 0) {
          ok = false;
        } else {
          if (A == 1)
            ans.push_back('B');
          else
            ans.push_back('A');
          swap(A, B);
        }
      } else if (S[i] == "BC") {
        if (B == 0 && C == 0) {
          ok = false;
        } else {
          if (B == 1)
            ans.push_back('C');
          else
            ans.push_back('B');
          swap(B, C);
        }
      } else {
        if (A == 0 && C == 0) {
          ok = false;
        } else {
          if (A == 1)
            ans.push_back('C');
          else
            ans.push_back('A');
          swap(A, C);
        }
      }
    }
    if (ok) {
      print("Yes");
      rep(i, 0, N) print(ans[i]);
    } else {
      print("No");
    }
  } else if (A + B + C >= 3) {
    bool ok = true;
    vector<char> ans;
    rep(i, 0, N) {
      if (S[i] == "AB") {
        if (A + B == 0) {
          ok = false;
        } else {
          if (A < B) {
            ans.push_back('A');
            A++;
            B--;
          } else {
            ans.push_back('B');
            B++;
            A--;
          }
        }
      } else if (S[i] == "BC") {
        if (C + B == 0) {
          ok = false;
        } else {
          if (C < B) {
            ans.push_back('C');
            C++;
            B--;
          } else {
            ans.push_back('B');
            B++;
            C--;
          }
        }
      } else {
        if (A + C == 0) {
          ok = false;
        } else {
          if (A < C) {
            ans.push_back('A');
            A++;
            C--;
          } else {
            ans.push_back('C');
            C++;
            A--;
          }
        }
      }
    }
    if (ok) {
      print("Yes");
      rep(i, 0, N) print(ans[i]);
    } else {
      print("No");
    }
  } else {
    exit(1);
  }
}

Fixed submission:
/*** author: yuji9511 ***/
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using lpair = pair<ll, ll>;
const ll MOD = 1e9 + 7;
const ll INF = 1e18;
#define rep(i, m, n) for (ll i = (m); i < (n); i++)
#define rrep(i, m, n) for (ll i = (m); i >= (n); i--)
#define printa(x, n)                                                           \
  for (ll i = 0; i < n; i++) {                                                 \
    cout << (x[i]) << " \n"[i == n - 1];                                       \
  };
void print() {}
template <class H, class... T> void print(H &&h, T &&...t) {
  cout << h << " \n"[sizeof...(t) == 0];
  print(forward<T>(t)...);
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll N, A, B, C;
  cin >> N >> A >> B >> C;
  string S[100010];
  rep(i, 0, N) cin >> S[i];
  if (A == 0 && B == 0 && C == 0) {
    print("No");
  } else if (A + B + C == 1) {
    bool ok = true;
    vector<char> ans;
    rep(i, 0, N) {
      if (S[i] == "AB") {
        if (A == 0 && B == 0) {
          ok = false;
        } else {
          if (A == 1)
            ans.push_back('B');
          else
            ans.push_back('A');
          swap(A, B);
        }
      } else if (S[i] == "BC") {
        if (B == 0 && C == 0) {
          ok = false;
        } else {
          if (B == 1)
            ans.push_back('C');
          else
            ans.push_back('B');
          swap(B, C);
        }
      } else {
        if (A == 0 && C == 0) {
          ok = false;
        } else {
          if (A == 1)
            ans.push_back('C');
          else
            ans.push_back('A');
          swap(A, C);
        }
      }
    }
    if (ok) {
      print("Yes");
      rep(i, 0, N) print(ans[i]);
    } else {
      print("No");
    }
  } else if (A + B + C >= 3) {
    bool ok = true;
    vector<char> ans;
    rep(i, 0, N) {
      if (S[i] == "AB") {
        if (A + B == 0) {
          ok = false;
        } else {
          if (A < B) {
            ans.push_back('A');
            A++;
            B--;
          } else {
            ans.push_back('B');
            B++;
            A--;
          }
        }
      } else if (S[i] == "BC") {
        if (C + B == 0) {
          ok = false;
        } else {
          if (C < B) {
            ans.push_back('C');
            C++;
            B--;
          } else {
            ans.push_back('B');
            B++;
            C--;
          }
        }
      } else {
        if (A + C == 0) {
          ok = false;
        } else {
          if (A < C) {
            ans.push_back('A');
            A++;
            C--;
          } else {
            ans.push_back('C');
            C++;
            A--;
          }
        }
      }
    }
    if (ok) {
      print("Yes");
      rep(i, 0, N) print(ans[i]);
    } else {
      print("No");
    }
  } else {
    vector<ll> pos[3];
    vector<char> ans;
    rep(i, 0, N) {
      if (S[i] == "AB")
        pos[0].push_back(i);
      else if (S[i] == "BC")
        pos[1].push_back(i);
      else
        pos[2].push_back(i);
    }
    bool ok = true;
    rep(i, 0, N) {
      if (S[i] == "AB") {
        if (A + B == 0) {
          ok = false;
        } else if (A < B) {
          ans.push_back('A');
          A++;
          B--;
        } else if (A > B) {
          ans.push_back('B');
          B++;
          A--;
        } else {
          ll p1 = lower_bound(pos[1].begin(), pos[1].end(), i) - pos[1].begin();
          ll p2 = lower_bound(pos[2].begin(), pos[2].end(), i) - pos[2].begin();
          ll v1 = INF, v2 = INF;
          if (p1 != pos[1].size())
            v1 = pos[1][p1];
          if (p2 != pos[2].size())
            v2 = pos[2][p2];
          if (v1 <= v2) {
            ans.push_back('B');
            B++;
            A--;
          } else {
            ans.push_back('A');
            A++;
            B--;
          }
        }

      } else if (S[i] == "BC") {
        if (C + B == 0) {
          ok = false;
        } else if (C < B) {
          ans.push_back('C');
          C++;
          B--;

        } else if (C > B) {
          ans.push_back('B');
          B++;
          C--;

        } else {
          ll p1 = lower_bound(pos[0].begin(), pos[0].end(), i) - pos[0].begin();
          ll p2 = lower_bound(pos[2].begin(), pos[2].end(), i) - pos[2].begin();
          ll v1 = INF, v2 = INF;
          if (p1 != pos[0].size())
            v1 = pos[0][p1];
          if (p2 != pos[2].size())
            v2 = pos[2][p2];
          if (v1 <= v2) {
            ans.push_back('B');
            B++;
            C--;
          } else {
            ans.push_back('C');
            C++;
            B--;
          }
          // print(v1,v2);
        }
        // print(A,B,C);
      } else {
        if (A + C == 0) {
          ok = false;
        } else if (A < C) {
          ans.push_back('A');
          A++;
          C--;

        } else if (A > C) {
          ans.push_back('C');
          C++;
          A--;

        } else {
          ll p1 = lower_bound(pos[0].begin(), pos[0].end(), i) - pos[0].begin();
          ll p2 = lower_bound(pos[1].begin(), pos[1].end(), i) - pos[1].begin();
          ll v1 = INF, v2 = INF;
          if (p1 != pos[0].size())
            v1 = pos[0][p1];
          if (p2 != pos[1].size())
            v2 = pos[1][p2];
          if (v1 <= v2) {
            ans.push_back('A');
            A++;
            C--;
          } else {
            ans.push_back('C');
            C++;
            A--;
          }
        }
      }
    }
    if (ok) {
      print("Yes");
      rep(i, 0, N) print(ans[i]);
    } else {
      print("No");
    }
  }
}