alexjercan/bugnet huggingface dataset:
Problem p02692 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 2e5 + 5;
int n, a[3];
char s[MAX_N][10];
int ans[MAX_N];
int main() {
  scanf("%d", &n);
  for (int i = 0; i < 3; i++)
    scanf("%d", a + i);
  bool flag = 0;
  for (int i = 1; i <= n; i++)
    scanf("%s", s[i] + 1);
  for (int i = 1; i <= n; i++) {
    int x = s[i][1] - 'A', y = s[i][2] - 'A';
    if (a[x] < a[y])
      a[x]++, a[y]--, ans[i] = x;
    else if (a[y] < a[x])
      a[x]--, a[y]++, ans[i] = y;
    else {
      if (a[x] == 1 && !a[3 - x - y]) {
        if (i == n || (s[i + 1][1] != 3 - x - y + 'A' &&
                       s[i + 1][2] != 3 - x - y + 'A')) {
          a[x]--, a[y]++, ans[i] = y;
        } else {
          int x1 = s[i + 1][1] - 'A', x2 = s[i + 1][2] - 'A';
          if (x1 == x || x2 == x)
            ans[i] = x, a[x]++, a[y]--;
          else
            ans[i] = y, a[x]--, a[y]++;
        }
      } else
        a[x]--, a[y]++, ans[i] = y;
    }
    if (a[x] < 0 || a[y] < 0) {
      flag = 1;
      break;
    }
  }
  if (!flag) {
    puts("Yes");
    for (int i = 1; i <= n; i++)
      printf("%c\\n", ans[i] + 'A');
  } else
    puts("No");
  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using lint = long long;

signed main() {
  lint N, A, B, C;
  cin >> N >> A >> B >> C;
  string ans;
  vector<string> s;
  for (int i = 0; i < N; i++)
    cin >> s[i];
  bool ok = true;
  for (int i = 0; i < N; i++) {
    if (s[i] == "AB") {
      if (A == 0 && B == 0)
        ok = false;
      else if (A == B) {
        if (s[i + 1] == "AB")
          A++, B--;
        if (s[i + 1] == "BC")
          A--, B++;
        if (s[i + 1] == "AC")
          A++, B--;
      } else if (A <= B)
        ans.push_back('A'), A++, B--;
      else
        ans.push_back('B'), A--, B++;
    }
    if (s[i] == "BC") {
      if (B == 0 && C == 0)
        ok = false;
      else if (C == B) {
        if (s[i + 1] == "AB")
          B++, C--;
        if (s[i + 1] == "BC")
          C--, B++;
        if (s[i + 1] == "AC")
          C++, B--;
      } else if (C <= B)
        ans.push_back('C'), C++, B--;
      else
        ans.push_back('B'), C--, B++;
    }
    if (s[i] == "AC") {
      if (C == 0 && A == 0)
        ok = false;
      else if (C == A) {
        if (s[i + 1] == "AB")
          A++, C--;
        if (s[i + 1] == "BC")
          C++, A--;
        if (s[i + 1] == "AC")
          C++, A--;
      } else if (C <= A)
        ans.push_back('C'), C++, A--;
      else
        ans.push_back('A'), C--, A++;
    }
  }
  if (ok) {
    cout << "Yes" << endl;
    for (int i = 0; i < ans.size(); i++)
      cout << ans[i] << '\\n';
  } else
    cout << "No" << endl;
}
Problem p02692 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
string s[100000], m;
int main() {
  long long n, a[3];
  cin >> n >> a[0] >> a[1] >> a[2];
  for (int t = 0; t < n; t++) {
    cin >> s[t];
  }
  for (int t = 0; t < n; t++) {
    if (a[s[t][0] - 'A'] < a[s[t][1] - 'A']) {
      m += s[t][0];
      a[s[t][0] - 'A']++;
      a[s[t][1] - 'A']--;
    } else if (a[s[t][0] - 'A'] == a[s[t][1] - 'A']) {
      if (t != n - 1) {
        if (s[t][0] - 'A' == s[t + 1][0] - 'A' ||
            s[t][0] - 'A' == s[t + 1][1] - 'A') {
          m += s[t][0];
          a[s[t][0] - 'A']++;
          a[s[t][1] - 'A']--;
        } else {
          m += s[t][1];
          a[s[t][1] - 'A']++;
          a[s[t][0] - 'A']--;
        }
      } else {
        m += s[t][0];
        a[s[t][0] - 'A']++;
        a[s[t][1] - 'A']--;
      }
    } else {
      m += s[t][1];
      a[s[t][1] - 'A']++;
      a[s[t][0] - 'A']--;
    }
    if (a[0] < 0 || a[1] < 0 || a[2] < 0) {
      cout << "No";
      return 0;
    }
  }
  cout << "Yes" << endl;
  for (int t = 0; t < n; t++) {
    cout << m[t] << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
string s[100000], m;
int main() {
  long long n, a[3];
  cin >> n >> a[0] >> a[1] >> a[2];
  for (int t = 0; t < n; t++) {
    cin >> s[t];
  }
  for (int t = 0; t < n; t++) {
    if (a[s[t][0] - 'A'] < a[s[t][1] - 'A']) {
      m += s[t][0];
      a[s[t][0] - 'A']++;
      a[s[t][1] - 'A']--;
    } else if (a[s[t][0] - 'A'] == a[s[t][1] - 'A']) {
      if (s[t][0] - 'A' == s[t + 1][0] - 'A' ||
          s[t][0] - 'A' == s[t + 1][1] - 'A') {
        m += s[t][0];
        a[s[t][0] - 'A']++;
        a[s[t][1] - 'A']--;
      } else {
        m += s[t][1];
        a[s[t][1] - 'A']++;
        a[s[t][0] - 'A']--;
      }
    } else {
      m += s[t][1];
      a[s[t][1] - 'A']++;
      a[s[t][0] - 'A']--;
    }
    if (a[0] < 0 || a[1] < 0 || a[2] < 0) {
      cout << "No";
      return 0;
    }
  }
  cout << "Yes" << endl;
  for (int t = 0; t < n; t++) {
    cout << m[t] << endl;
  }
  return 0;
}
Problem p02692 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
string s[100000], m;
int main() {
  long long n, a, b, c, b1, b2;
  cin >> n >> a >> b >> c;
  for (int t = 0; t < n; t++) {
    cin >> s[t];
  }
  for (int t = 0; t < n; t++) {
    b1 = 0;
    b2 = 0;
    if (s[t] == "AB") {
      if (a < b) {
        m += 'A';
        a++;
        b--;
      } else if (a == b) {
        if (a == 1 && c == 0) {
          if (s[t + 1] == "AC") {
            m += 'A';
            a++;
            b--;
          } else {
            m += 'B';
            b++;
            a--;
          }
        } else {
          m += 'A';
          a++;
          b--;
        }
      } else {
        m += 'B';
        b++;
        a--;
      }
    } else if (s[t] == "AC") {
      b1 = 0;
      b2 = 0;
      if (a < c) {
        m += 'A';
        a++;
        c--;
      } else if (a == c) {
        if (a == 1 && b == 0) {
          if (s[t + 1] == "AB") {
            m += 'A';
            a++;
            c--;
          } else {
            m += 'C';
            c++;
            a--;
          }
        } else {
          m += 'A';
          a++;
          c--;
        }
      } else {
        m += 'C';
        c++;
        a--;
      }
    } else {
      b1 = 0;
      b2 = 0;
      if (c < b) {
        m += 'C';
        c++;
        b--;
      } else if (b == c) {
        if (b == 1 && a == 0) {
          if (s[t + 1] == "AB") {
            m += 'B';
            b++;
            c--;
          } else {
            m += 'C';
            c++;
            b--;
          }
        } else {
          m += 'B';
          b++;
          c--;
        }
      } else {
        m += 'B';
        b++;
        c--;
      }
    }
    if (a < 0 || b < 0 || c < 0) {
      cout << "No";
      return 0;
    }
  }
  cout << "Yes" << endl;
  for (int t = 0; t < n; t++) {
    cout << m[t] << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
string s[100000], m;
void mm(long long &d, long long &e) {
  if (d < e) {
    d++;
    e--;
  } else {
    e++;
    d--;
  }
}
int main() {
  long long n, a, b, c;
  cin >> n >> a >> b >> c;
  for (int t = 0; t < n; t++) {
    cin >> s[t];
  }
  for (int t = 0; t < n; t++) {
    // cout<<"t="<<t<<" ";
    if (s[t] == "AB") {
      if (a < b) {
        m[t] = 'A';
      } else {
        m[t] = 'B';
      }
      mm(a, b);
    } else if (s[t] == "AC") {
      if (a < c) {
        m[t] = 'A';
      } else {
        m[t] = 'C';
      }
      mm(a, c);
    } else {
      if (c < b) {
        m[t] = 'C';
      } else {
        m[t] = 'B';
      }
      mm(b, c);
      // cout<<"a="<<a<<" b="<<b<<" c="<<c<<" ";
    }
    if (a < 0 || b < 0 || c < 0) {
      cout << "No";
      return 0;
    }
  }
  cout << "Yes" << endl;
  for (int t = 0; t < n; t++) {
    cout << m[t] << endl;
  }
  return 0;
}
Problem p02692 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

const int M = 1e5 + 100;
int n, a, b, c;
string s[M];
char ans[M];

void dfs(int i, int a, int b, int c) {
  if (i == n) {
    cout << "Yes"
         << "\\n";
    for (int i = 0; i < n; i++)
      cout << ans[i] << "\\n";
    exit(0);
  }
  if (s[i] == "AB") {
    if (a)
      ans[i] = 'B', dfs(i + 1, a - 1, b + 1, c);
    if (b)
      ans[i] = 'A', dfs(i + 1, a + 1, b - 1, c);
  }
  if (s[i] == "BC") {
    if (b)
      ans[i] = 'C', dfs(i + 1, a, b - 1, c + 1);
    if (c)
      ans[i] = 'B', dfs(i + 1, a, b + 1, c - 1);
  }
  if (s[i] == "AC") {
    if (a)
      ans[i] = 'C', dfs(i + 1, a - 1, b, c + 1);
    if (c)
      ans[i] = 'A', dfs(i + 1, a + 1, b, c - 1);
  }
}

int main() {
  cin >> n >> a >> b >> c;
  for (int i = 0; i < n; i++)
    cin >> s[i];
  dfs(0, a, b, c);
  cout << "No";
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  freopen("in.txt", "r", stdin);
  int n, a, b, c;
  cin >> n >> a >> b >> c;
  string s[n];
  int A = 0, B = 0, C = 0;
  for (int i = 0; i < n; i++) {
    cin >> s[i];
    if (s[i] == "AB")
      ++A, ++B;
    else if (s[i] == "BC")
      ++B, ++C;
    else
      ++A, ++C;
  }
  char ans[n];
  for (int i = 0; i < n; i++) {
    if (s[i] == "AB") {
      int nx_a = A - 1;
      int nx_b = B - 1;
      if (a - 1 < 0 and b - 1 < 0) {
        cout << "No";
        return 0;
      }
      if (a - 1 >= 0)
        --a, ++b, ans[i] = 'B';
      else
        --b, ++a, ans[i] = 'A';
      --A, --B;
    }
    if (s[i] == "BC") {
      int nx_b = B - 1;
      int nx_c = C - 1;
      if (b - 1 < 0 and c - 1 < 0) {
        cout << "No";
        return 0;
      }
      if (b - 1 >= 0)
        --b, ++c, ans[i] = 'C';
      else
        --c, ++b, ans[i] = 'B';
      --B, --C;
    }
    if (s[i] == "AC") {
      int nx_a = A - 1;
      int nx_c = C - 1;
      if (a - 1 < 0 and c - 1 < 0) {
        cout << "No";
        return 0;
      }
      if (a - 1 >= 0)
        --a, ++c, ans[i] = 'C';
      else
        --c, ++a, ans[i] = 'A';
      --A, --C;
    }
    // cout << i << "\\n";
  }
  cout << "Yes"
       << "\\n";
  for (int i = 0; i < n; i++)
    cout << ans[i] << "\\n";
}
Problem p02692 (C++) — buggy submission, verdict: Runtime Error

#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
template <typename T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(vec) vec.begin(), vec.end()
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef tuple<ll, ll, ll> tlll;
typedef tuple<int, int, int> tiii;
const ll mod = 1e9 + 7;
const int inf = 1 << 30;

int main() {
  int n, a, b, c;
  cin >> n >> a >> b >> c;
  vector<char> ans(n);
  map<char, int> mp;
  mp['A'] = a;
  mp['B'] = b;
  mp['C'] = c;
  vector<string> s(n);
  rep(i, n) cin >> s[i];
  rep(i, n) {
    if (mp[s[i][0]] == 0 && mp[s[i][1]] == 0) {
      cout << "No" << endl;
      return 0;
    } else if (mp[s[i][0]] > mp[s[i][1]]) {
      mp[s[i][0]]--;
      mp[s[i][1]]++;
      ans[i] = s[i][1];
    } else if (mp[s[i][0]] < mp[s[i][1]]) {
      mp[s[i][0]]++;
      mp[s[i][1]]--;
      ans[i] = s[i][0];
    } else {
      if (i + 1 < n && (s[i][0] == s[i + 1][0] || s[i][0] == s[i + 1][1])) {
        mp[s[i][0]]++;
        mp[s[i][1]]--;
        ans[i] = s[i][0];
      } else {
        mp[s[i][1]]++;
        mp[s[i][0]]--;
        ans[i] = s[i][1];
      }
    }
  }
  cout << "Yes" << endl;
  rep(i, n) cout << ans[i] << endl;
}

Error: 0

Fixed submission:

#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
template <typename T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(vec) vec.begin(), vec.end()
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef tuple<ll, ll, ll> tlll;
typedef tuple<int, int, int> tiii;
const ll mod = 1e9 + 7;
const int inf = 1 << 30;

int main() {
  int n, a, b, c;
  cin >> n >> a >> b >> c;
  vector<char> ans(n);
  map<char, int> mp;
  mp['A'] = a;
  mp['B'] = b;
  mp['C'] = c;
  vector<string> s(n);
  rep(i, n) cin >> s[i];
  rep(i, n) {
    if (mp[s[i][0]] == 0 && mp[s[i][1]] == 0) {
      cout << "No" << endl;
      return 0;
    }
    if (mp[s[i][0]] > mp[s[i][1]]) {
      mp[s[i][0]]--;
      mp[s[i][1]]++;
      ans[i] = s[i][1];
    }
    if (mp[s[i][0]] < mp[s[i][1]]) {
      mp[s[i][0]]++;
      mp[s[i][1]]--;
      ans[i] = s[i][0];
    }
    if (mp[s[i][0]] == mp[s[i][1]]) {
      if (i + 1 < n && s[i][0] == s[i + 1][0] || s[i][0] == s[i + 1][1]) {
        mp[s[i][0]]++;
        mp[s[i][1]]--;
        ans[i] = s[i][0];
      } else if (i + 1 < n &&
                 (s[i][1] == s[i + 1][0] || s[i][1] == s[i + 1][1])) {
        mp[s[i][1]]++;
        mp[s[i][0]]--;
        ans[i] = s[i][1];
      }
    }
  }
  cout << "Yes" << endl;
  rep(i, n) cout << ans[i] << endl;
}
Problem p02692 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define ll long long
using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int n;
  cin >> n;
  vector<int> A(3);
  vector<string> s(n);
  vector<char> ans;
  for (auto &i : A)
    cin >> i;
  for (auto &i : s)
    cin >> i;
  for (int i = 0; i < n; ++i) {
    int a = s[i][0] - 'A', b = s[i][1] - 'A';
    if (!A[a] && !A[b]) {
      cout << "No";
      return 0;
    } else if (A[a] > A[b]) {
      ans.push_back('A' + b);
      A[a]--, A[b]++;
    } else if (A[a] < A[b]) {
      ans.push_back('A' + a);
      A[a]++, A[b]--;
    } else {
      if (i == n - 1 || s[i] == s[i + 1]) {
        ans.push_back('A' + a);
        A[b]--;
        A[a]++;
      } else {
        int c = s[i + 1][0] - 'A', d = s[i + 1][1] - 'A';
        if (a == c || a == d) {
          ans.push_back('A' + a);
          A[b]--;
          A[a]++;
        } else {
          ans.push_back('A' + b);
          A[a]--;
          A[b]++;
        }
      }
    }
  }
  cout << "Yes\\n";
  for (auto i : ans)
    cout << i << '\\n';
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int n;
  cin >> n;
  vector<int> A(3);
  vector<string> s(n);
  string ans;
  for (auto &i : A)
    cin >> i;
  for (auto &i : s)
    cin >> i;
  for (int i = 0; i < n; ++i) {
    int a = s[i][0] - 'A', b = s[i][1] - 'A';
    if (!a && !b) {
      cout << "No";
      return 0;
    } else if (A[a] > A[b]) {
      ans.push_back('A' + b);
      A[a]--, A[b]++;
    } else if (A[a] < A[b]) {
      ans.push_back('A' + a);
      A[a]++, A[b]--;
    } else {
      if (i == n - 1 || s[i] == s[i + 1]) {
        ans[i] = 'A' + a;
        A[b]--;
        A[a]++;
      } else {
        int c = s[i + 1][0] - 'A', d = s[i + 1][1] - 'A';
        if (a == c || a == d) {
          ans[i] = 'A' + a;
          A[b]--;
          A[a]++;
        } else {
          ans[i] = 'A' + b;
          A[a]--;
          A[b]++;
        }
      }
    }
  }
  cout << "Yes\\n" << ans;
}
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 main() {
  int n, a, b, c;
  cin >> n >> a >> b >> c;
  vector<string> s(n);
  vector<char> ans(n);
  rep(i, n) cin >> s.at(i);

  bool ok = true;

  auto next = [&](int i, int &x, int &y) {
    if (x + y == 0) {
      ok = false;
      return ok;
    }
    if (x == 0) {
      ans.at(i) = s.at(i).at(0);
      x++;
      y--;
    } else if (y == 0) {
      ans.at(i) = s.at(i).at(1);
      x--;
      y++;
    } else {
      if (i < n - 1 && s.at(i + 1).find(s.at(i).at(0)) != string::npos) {
        ans.at(i) = s.at(i).at(0);
        x++;
        y--;
      } else {
        ans.at(i) = s.at(i).at(1);
        x--;
        y++;
      }
    }
    return true;
  };

  rep(i, n) {
    if (s.at(i) == "AB") {
      if (!next(i, a, b))
        break;
    } else if (s.at(i) == "AC") {
      if (!next(i, a, c))
        break;
    } else if (s.at(i) == "BC") {
      if (!next(i, b, c))
        break;
    }
  }

  if (ok) {
    cout << "Yes" << endl;
    rep(i, n) cout << ans.at(i) << endl;
  } else {
    cout << "No" << endl;
  }

  return 0;
}

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 main() {
  int n, a, b, c;
  cin >> n >> a >> b >> c;
  vector<string> s(n);
  vector<char> ans(n);
  rep(i, n) cin >> s.at(i);

  bool ok = true;

  auto next = [&](int i, int &x, int &y) {
    if (x + y == 0) {
      ok = false;
      return ok;
    }
    if (x == 0) {
      ans.at(i) = s.at(i).at(0);
      x++;
      y--;
    } else if (y == 0) {
      ans.at(i) = s.at(i).at(1);
      x--;
      y++;
    } else {
      if (s.at(i + 1).find(s.at(i).at(0)) != string::npos && i < n - 1) {
        ans.at(i) = s.at(i).at(0);
        x++;
        y--;
      } else {
        ans.at(i) = s.at(i).at(1);
        x--;
        y++;
      }
    }
    return true;
  };

  rep(i, n) {
    if (s.at(i) == "AB") {
      if (!next(i, a, b))
        break;
    } else if (s.at(i) == "AC") {
      if (!next(i, a, c))
        break;
    } else if (s.at(i) == "BC") {
      if (!next(i, b, c))
        break;
    }
  }

  if (ok) {
    cout << "Yes" << endl;
    rep(i, n) cout << ans.at(i) << endl;
  } else {
    cout << "No" << endl;
  }

  return 0;
}
Problem p02692 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

vector<int64_t> num(3);
vector<int> ans(1000010);

void jadge(int i, int p, int q) {
  ans.at(i) = p;
  num.at(p)++;
  num.at(q)--;
}

int main() {
  int N, p, q;
  cin >> N >> num.at(0) >> num.at(1) >> num.at(2);

  vector<vector<int>> x(N, vector<int>(2));
  for (int i = 0; i < N; i++) {
    string s;
    cin >> s;
    x.at(i).at(0) = s.at(0) - 'A';
    x.at(i).at(1) = s.at(1) - 'A';
  }
  for (int i = 0; i < N; i++) {
    p = x.at(i).at(0);
    q = x.at(i).at(1);
    if (num.at(p) == 0 && num.at(q) == 0) {
      cout << "No" << endl;
      return 0;
    } else if (num.at(p) == 0)
      jadge(i, p, q);
    else if (num.at(q) == 0)
      jadge(i, q, p);
    else if (i != N - 1 && p == 3 - x.at(i + 1).at(0) - x.at(i + 1).at(1))
      jadge(i, q, p);
    else
      jadge(i, p, q);
  }
  cout << "Yes" << endl;
  for (int i = 0; i < N; i++) {
    cout << (char)(ans.at(i) + 'A') << endl;
  }
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

vector<int64_t> num(3);
vector<int> ans(1000010);

int jadge(int i, int p, int q) {
  ans.at(i) = p;
  num.at(p)++;
  num.at(q)--;
}

int main() {
  int N, p, q;

  cin >> N >> num.at(0) >> num.at(1) >> num.at(2);

  vector<vector<int>> x(N, vector<int>(3));
  vector<string> s(N);

  for (int i = 0; i < N; i++) {
    cin >> s.at(i);
  }
  for (int i = 0; i < N; i++) {
    p = s.at(i).at(0) - 'A';
    q = s.at(i).at(1) - 'A';
    if (num.at(p) == 0 && num.at(q) == 0) {
      cout << "NO" << endl;
      return 0;
    } else if (num.at(p) == 0)
      jadge(i, p, q);
    else if (num.at(q) == 0)
      jadge(i, q, p);
    else if (i != N - 1 && p == 3 - (s.at(i + 1).at(0) + s.at(i + 1).at(1)))
      jadge(i, q, p);
    else
      jadge(i, p, q);
  }
  cout << "YES" << endl;
  for (int i = 0; i < N; i++) {
    cout << (char)(ans.at(i) + 'A') << endl;
  }
  return 0;
}
Problem p02692 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ALL(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
typedef long double ld;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  vector<ll> vl(3);
  cin >> n;
  REP(i, 3) cin >> vl[i];
  vector<pair<int, int>> cmd(n);
  REP(i, n) {
    string s;
    cin >> s;
    cmd[i].first = s[0] - 'A';
    cmd[i].second = s[1] - 'A';
  }

  vector<char> ch(n);
  REP(i, n) {
    int x = cmd[i].first;
    int y = cmd[i].second;
    if (vl[x] == 0 && vl[y] == 0) {
      cout << "No\\n";
      return 0;
    }

    if (i < n - 1 && vl[x] == vl[y]) {
      int nx = cmd[i + 1].first;
      int ny = cmd[i + 1].second;
      if (x == nx || x == ny) {
        vl[x]++;
        vl[y]--;
        ch[i] = 'A' + x;
      } else {
        vl[x]--;
        vl[y]++;
        ch[i] = 'A' + y;
      }
    } else if (vl[x] < vl[y]) {
      vl[x]++;
      vl[y]--;
      ch[i] = 'A' + x;
    } else {
      vl[x]--;
      vl[y]++;
      ch[i] = 'A' + y;
    }
  }
  cout << "Yes\\n";
  REP(i, n) cout << ch[i] << '\\n';
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ALL(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
typedef long double ld;

int n;
vector<string> s;
bool suc = false;

void dfs(int k, ll a, ll b, ll c, vector<char> pro) {
  if (a < 0 || b < 0 || c < 0)
    return;
  if (suc)
    return;
  if (k == n) {
    suc = true;
    cout << "Yes\\n";
    REP(i, n) cout << pro[i] << '\\n';
    return;
  }
  if (s[k] == "AB") {
    if (a == 0 && b == 0)
      return;
    if (b > 0) {
      pro.push_back('A');
      dfs(k + 1, a + 1, b - 1, c, pro);
      pro.pop_back();
    }
    if (a > 0) {
      pro.push_back('B');
      dfs(k + 1, a - 1, b + 1, c, pro);
    }
  } else if (s[k] == "BC") {
    if (c > 0) {
      pro.push_back('B');
      dfs(k + 1, a, b + 1, c - 1, pro);
      pro.pop_back();
    }
    if (b > 0) {
      pro.push_back('C');
      dfs(k + 1, a, b - 1, c + 1, pro);
    }
  } else {
    if (c > 0) {
      pro.push_back('A');
      dfs(k + 1, a + 1, b, c - 1, pro);
      pro.pop_back();
    }
    if (a > 0) {
      pro.push_back('C');
      dfs(k + 1, a - 1, b, c + 1, pro);
    }
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  ll a, b, c;
  cin >> n >> a >> b >> c;
  s.resize(n);
  REP(i, n) cin >> s[i];
  dfs(0, a, b, c, vector<char>(0));
  if (!suc)
    cout << "No\\n";
  return 0;
}