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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <istream>
#include <map>
#include <ostream>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define int long long
#define pb push_back
#define all(a) (a).begin(), (a).end()
#define pii pair<int, int>
#define ld long double

ostream &operator<<(ostream &a, const vector<int> &b) {
  for (auto k : b)
    cout << k << " ";
  return a;
}

#ifdef LOCAL
#define dbg(x) cout << #x << " : " << (x) << "\\n";
const int INF = 1e18;
// const int mod = 2600000069;
// const int p = 10;
// const ld PI = 3.1415926535;
#else
#define dbg(x)
const int INF = 1e18;
// const int mod = 2600000069;
// const int p = 179;
// const ld PI = 3.1415926535;
#endif

// #pragma GCC optimize("Ofast,no-stack-protector")
// #pragma GCC target("sse,sse2,sse3,sse3,sse4")
// #pragma GCC optimize("unroll-loops")
// #pragma GCC optimize("fast-math")
// #pragma GCC target("avx2")
// #pragma GCC optimize("section-anchors")
// #pragma GCC optimize("profile-values,profile-reorder-functions,tracer")
// #pragma GCC optimize("vpt")
// #pragma GCC optimize("rename-registers")
// #pragma GCC optimize("move-loop-invariants")
// #pragma GCC optimize("unswitch-loops")
// #pragma GCC optimize("function-sections")
// #pragma GCC optimize("data-sections")`

int ind(char c) {
  if (c == 'A')
    return 0;
  if (c == 'B')
    return 1;
  return 2;
}

char c(int ind) {
  if (ind == 0)
    return 'A';
  if (ind == 1)
    return 'B';
  return 'C';
}

int m = 0;
map<vector<int>, int> h;
vector<vector<int>> alp;

void tryhard(vector<int> a, int lft, int sz) {
  if (sz == 3) {
    if (lft == 0) {
      alp.pb(a);
      h[a] = m;
      m++;
    }
    return;
  }
  for (int i = 0; i <= lft; i++) {
    a.pb(i);
    tryhard(a, lft - i, sz + 1);
    a.pop_back();
  }
}

const int MAXN = 1e5 + 10;
const int MAXM = 30;

int dp[MAXN][MAXM], par[MAXN][MAXM], val[MAXN][MAXM];

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  vector<int> a(3);
  string s;
  vector<string> q(n);
  for (int i = 0; i < 3; i++) {
    cin >> a[i];
    a[i] = min(a[i], 2ll);
  }
  for (int i = 0; i < n; i++) {
    cin >> q[i];
  }
  int ss = 0;
  for (int i = 0; i < 3; i++) {
    ss += a[i];
  }
  tryhard({}, ss, 0);
  dp[0][h[a]] = 1;
  par[0][h[a]] = -1;
  int ind1, ind2, hh;
  for (int i = 0; i < n; i++) {
    ind1 = ind(q[i][0]), ind2 = ind(q[i][1]);
    for (int j = 0; j < m; j++) {
      if (!dp[i][j])
        continue;
      if (alp[j][ind1]) {
        alp[j][ind1]--, alp[j][ind2]++;
        hh = h[alp[j]];
        dp[i + 1][hh] = 1;
        par[i + 1][hh] = j;
        val[i + 1][hh] = ind2;
        alp[j][ind1]++, alp[j][ind2]--;
      }
      if (alp[j][ind2]) {
        alp[j][ind2]--, alp[j][ind1]++;
        hh = h[alp[j]];
        dp[i + 1][hh] = 1;
        par[i + 1][hh] = j;
        val[i + 1][hh] = ind1;
        alp[j][ind2]++, alp[j][ind1]--;
      }
    }
  }
  int cur = -1;
  for (int i = 0; i < m; i++) {
    if (dp[n][i]) {
      cur = i;
    }
  }
  if (cur == -1) {
    cout << "No\\n";
    return 0;
  }
  vector<char> ans;
  for (int i = n; i > 0; i--) {
    ans.pb(c(val[i][cur]));
    cur = par[i][cur];
  }
  reverse(all(ans));
  cout << "Yes\\n";
  for (int i = 0; i < n; i++) {
    cout << ans[i] << "\\n";
  }
}
/*

*/


Error: -11

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <istream>
#include <map>
#include <ostream>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define int long long
#define pb push_back
#define all(a) (a).begin(), (a).end()
#define pii pair<int, int>
#define ld long double

ostream &operator<<(ostream &a, const vector<int> &b) {
  for (auto k : b)
    cout << k << " ";
  return a;
}

#ifdef LOCAL
#define dbg(x) cout << #x << " : " << (x) << "\\n";
const int INF = 1e18;
// const int mod = 2600000069;
// const int p = 10;
// const ld PI = 3.1415926535;
#else
#define dbg(x)
const int INF = 1e18;
// const int mod = 2600000069;
// const int p = 179;
// const ld PI = 3.1415926535;
#endif

// #pragma GCC optimize("Ofast,no-stack-protector")
// #pragma GCC target("sse,sse2,sse3,sse3,sse4")
// #pragma GCC optimize("unroll-loops")
// #pragma GCC optimize("fast-math")
// #pragma GCC target("avx2")
// #pragma GCC optimize("section-anchors")
// #pragma GCC optimize("profile-values,profile-reorder-functions,tracer")
// #pragma GCC optimize("vpt")
// #pragma GCC optimize("rename-registers")
// #pragma GCC optimize("move-loop-invariants")
// #pragma GCC optimize("unswitch-loops")
// #pragma GCC optimize("function-sections")
// #pragma GCC optimize("data-sections")`

int ind(char c) {
  if (c == 'A')
    return 0;
  if (c == 'B')
    return 1;
  return 2;
}

char c(int ind) {
  if (ind == 0)
    return 'A';
  if (ind == 1)
    return 'B';
  return 'C';
}

int m = 0;
map<vector<int>, int> h;
vector<vector<int>> alp;

void tryhard(vector<int> a, int lft, int sz) {
  if (sz == 3) {
    if (lft == 0) {
      alp.pb(a);
      h[a] = m;
      m++;
    }
    return;
  }
  for (int i = 0; i <= lft; i++) {
    a.pb(i);
    tryhard(a, lft - i, sz + 1);
    a.pop_back();
  }
}

const int MAXN = 1e5 + 10;
const int MAXM = 200;

int dp[MAXN][MAXM], par[MAXN][MAXM], val[MAXN][MAXM];

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int n;
  cin >> n;
  vector<int> a(3);
  string s;
  vector<string> q(n);
  for (int i = 0; i < 3; i++) {
    cin >> a[i];
    a[i] = min(a[i], 5ll);
  }
  for (int i = 0; i < n; i++) {
    cin >> q[i];
  }
  if (min(a[0], min(a[1], a[2])) >= 2) {
    return 1;
    set<pii> used;
    for (int i = 0; i < n; i++) {
      cin >> s;
      int ind1 = ind(s[0]);
      int ind2 = ind(s[1]);
      if (used.count({ind1, ind2})) {
        cout << c(ind1) << "\\n";
        used.erase({ind1, ind2});
      }
      if (used.count({ind2, ind1})) {
        cout << c(ind2) << "\\n";
        used.erase({ind2, ind1});
      }
      used.insert({ind1, ind2});
      cout << c(ind2) << "\\n";
      a[ind1]--, a[ind2]++;
    }
    return 0;
  }
  int ss = 0;
  for (int i = 0; i < 3; i++) {
    ss += a[i];
  }
  tryhard({}, ss, 0);
  dp[0][h[a]] = 1;
  par[0][h[a]] = -1;
  int ind1, ind2, hh;
  for (int i = 0; i < n; i++) {
    ind1 = ind(q[i][0]), ind2 = ind(q[i][1]);
    for (int j = 0; j < m; j++) {
      if (!dp[i][j])
        continue;
      if (alp[j][ind1]) {
        alp[j][ind1]--, alp[j][ind2]++;
        hh = h[alp[j]];
        dp[i + 1][hh] = 1;
        par[i + 1][hh] = j;
        val[i + 1][hh] = ind2;
        alp[j][ind1]++, alp[j][ind2]--;
      }
      if (alp[j][ind2]) {
        alp[j][ind2]--, alp[j][ind1]++;
        hh = h[alp[j]];
        dp[i + 1][hh] = 1;
        par[i + 1][hh] = j;
        val[i + 1][hh] = ind1;
        alp[j][ind2]++, alp[j][ind1]--;
      }
    }
  }
  int cur = -1;
  for (int i = 0; i < m; i++) {
    if (dp[n][i]) {
      cur = i;
    }
  }
  if (cur == -1) {
    cout << "No\\n";
    return 0;
  }
  vector<char> ans;
  for (int i = n; i > 0; i--) {
    ans.pb(c(val[i][cur]));
    cur = par[i][cur];
  }
  reverse(all(ans));
  cout << "Yes\\n";
  for (int i = 0; i < n; i++) {
    cout << ans[i] << "\\n";
  }
}
/*

*/