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

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

int main() {
  int n;
  vector<int> v(3, 0);
  cin >> n >> v[0] >> v[1] >> v[2];
  vector<string> vs(n, "");
  for (int i = 0; i < n; i++) {
    cin >> vs[i];
  }
  string res;
  for (int i = 0; i < n; i++) {
    if (v[0] < 0 || v[1] < 0 || v[2] < 0) {
      cout << "No" << endl;
      return 0;
    }
    int i1 = vs[i][0] - 'A', i2 = vs[i][1] - 'A';
    if (v[i1] == 0) {
      v[i1] += 1;
      v[i2] -= 1;
      res.push_back('A' + i1);
    } else if (v[i2] == 0) {
      v[i1] -= 1;
      v[i2] += 1;
      res.push_back('A' + i2);
    } else if (v[i1] == 1 && v[i2] == 1 && i + 1 < n && vs[i + 1] != vs[i]) {
      int next_i1 = vs[i + 1][0] - 'A', next_i2 = vs[i + 1][1] - 'A';
      bool incre_i1 = true;
      if (i1 != next_i1 && i1 != next_i2) {
        if (next_i1 == i2) {
          if (v[next_i2] == 0)
            incre_i1 = false;
        } else {
          if (v[next_i1] == 0)
            incre_i1 = false;
        }
      } else {
        if (next_i1 == i1) {
          if (v[next_i2] > 0)
            incre_i1 = false;
        } else {
          if (v[next_i1] > 0)
            incre_i1 = false;
        }
      }
      if (incre_i1) {
        v[i1]++;
        v[i2]--;
        res.push_back('A' + i1);
      } else {
        v[i1]--;
        v[i2]++;
        res.push_back('A' + i2);
      }
    } else {
      if (v[i1] < v[i2]) {
        v[i1] += 1;
        v[i2] -= 1;
        res.push_back('A' + i1);
      } else {
        v[i1] -= 1;
        v[i2] += 1;
        res.push_back('A' + i2);
      }
    }
  }
  if (v[0] < 0 || v[1] < 0 || v[2] < 0) {
    cout << "No" << endl;
    return 0;
  }
  cout << "Yes" << endl;
  for (auto r : res)
    cout << r << endl;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
#define ll long long int

int main() {
  ll t;
  t = 1;
  // cin t;
  while (t--) {
    ll n, a, b, c;
    cin >> n >> a >> b >> c;
    string s[n];
    for (int i = 0; i < n; i++) {
      cin >> s[i];
    }
    if (a + b + c == 0)
      cout << "No" << endl;
    else if (a + b + c == 1) {
      bool check = true;
      string ans[n];
      int i = 0;
      map<char, int> m;
      m['A'] = a;
      m['B'] = b;
      m['C'] = c;
      while (i < n) {
        if (m[s[i][0]] == 0 && m[s[i][1]] == 0) {
          check = false;
          break;
        } else if (m[s[i][0]] == 0) {
          m[s[i][0]] = 1;
          m[s[i][1]]--;
          ans[i] = s[i][0];
        } else {
          m[s[i][1]] = 1;
          m[s[i][0]]--;
          ans[i] = s[i][1];
        }
        i++;
      }
      if (!check)
        cout << "No" << endl;
      else {
        cout << "Yes" << endl;
        for (int i = 0; i < n; i++)
          cout << ans[i] << endl;
      }
    } else {
      map<char, int> m;
      m['A'] = a;
      m['B'] = b;
      m['C'] = c;
      if (m[s[0][0]] == 0 && m[s[0][1]] == 0) {
        cout << "No" << endl;
      } else {
        cout << "Yes" << endl;
        int i = 0;
        while (i < n) {
          if (m[s[i][0]] == 0) {
            m[s[i][1]]--;
            m[s[i][0]] = 1;
            cout << s[i][0] << endl;
          } else if (m[s[i][i]] == 0) {
            m[s[i][0]]--;
            m[s[i][1]] = 1;
            cout << s[i][1] << endl;
          } else if (m[s[i][0]] == 1 && m[s[i][1]] == 1 && i + 1 < n &&
                     s[i] != s[i + 1]) {
            if (s[i][0] == s[i + 1][0]) {
              m[s[i][0]]++;
              m[s[i][1]]--;
              cout << s[i][0] << endl;
            } else if (s[i][1] == s[i + 1][1]) {
              m[s[i][1]]++;
              m[s[i][0]]--;
              cout << s[i][1] << endl;
            } else if (s[i][0] == s[i + 1][1]) {
              m[s[i][0]]++;
              m[s[i][1]]--;
              cout << s[i][0] << endl;
            } else {
              m[s[i][1]]++;
              m[s[i][0]]--;
              cout << s[i][1] << endl;
            }
          } else {
            m[s[i][0]]--;
            m[s[i][1]]++;
            cout << s[i][1] << endl;
          }
          i++;
        }
      }
    }
  }
}