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

#include <bits/stdc++.h>
// #include <chrono>
// #pragma GCC optimize("Ofast")
using namespace std;
#define reps(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) reps(i, 0, n)
#define Rreps(i, n, e) for (int i = n - 1; i >= e; --i)
#define Rrep(i, n) Rreps(i, n, 0)
#define ALL(a) a.begin(), a.end()
#define fi first
#define se second
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;

ll N, M, H, W, Q, K, A, B;
string S;
typedef pair<ll, ll> P;
const ll INF = (1LL << 60);

int main() {
  cin >> N;
  vec a_num(N, 0), b_num(N, 0);
  rep(i, N) {
    cin >> A;
    --A;
    ++a_num[A];
  }
  rep(i, N) {
    cin >> A;
    --A;
    ++b_num[A];
  }
  rep(i, N) {
    if (a_num[i] + b_num[i] > N) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
  vec ans(N, -1), ans_id(N, -1);
  multiset<ll> s;
  rep(i, N) rep(j, b_num[i]) s.insert(i);
  int k(0), last(-1), id(0);
  rep(i, N) {
    if (k + a_num[i] + b_num[i] >= N) {
      last = i;
      break;
    }
    reps(j, k, k + a_num[i]) ans_id[j] = id++;
    reps(j, k + a_num[i], k + a_num[i] + b_num[i]) ans[j] = i;
    s.erase(i);
    k += a_num[i] + b_num[i];
  }
  int over = k + a_num[last] + b_num[last] - N, restofa = a_num[last];
  Rrep(i, k) {
    if (over == 0 || restofa == 0)
      break;
    if (ans_id[i] != -1)
      continue;
    ans_id[i] = id++;
    --restofa;
    --over;
  }
  rep(i, k) {
    if (over == 0)
      break;
    if (ans[i] != -1)
      continue;
    ans[i] = last;
    s.erase(s.begin());
    --over;
  }
  reps(i, k, N) {
    if ((restofa--) > 0)
      ans_id[i] = id++;
    else {
      ans[i] = last;
      s.erase(s.begin());
    }
  }
  rep(i, N) {
    if (ans_id[i] == -1)
      ans_id[i] = id++;
    if (ans[i] == -1) {
      ans[i] = *s.begin();
      s.erase(s.begin());
    }
  }
  vec res(N);
  rep(i, N) res[ans_id[i]] = ans[i];
  rep(i, N) cout << res[i] + 1 << " \\n"[i == N - 1];
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
// #include <chrono>
// #pragma GCC optimize("Ofast")
using namespace std;
#define reps(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) reps(i, 0, n)
#define Rreps(i, n, e) for (int i = n - 1; i >= e; --i)
#define Rrep(i, n) Rreps(i, n, 0)
#define ALL(a) a.begin(), a.end()
#define fi first
#define se second
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;

ll N, M, H, W, Q, K, A, B;
string S;
typedef pair<ll, ll> P;
const ll INF = (1LL << 60);

int main() {
  cin >> N;
  vec a_num(N, 0), b_num(N, 0);
  rep(i, N) {
    cin >> A;
    --A;
    ++a_num[A];
  }
  rep(i, N) {
    cin >> A;
    --A;
    ++b_num[A];
  }
  rep(i, N) {
    if (a_num[i] + b_num[i] > N) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
  vec ans(N, -1), ans_id(N, -1);
  multiset<ll> s;
  rep(i, N) rep(j, b_num[i]) s.insert(i);
  int temp(0), k(0), id(0);
  int id_ite(0), ans_ite(0);
  for (; temp < N && k < N; ++temp) {
    if (k + a_num[temp] + b_num[temp] >= N)
      break;
    for (int v = 0; k < N && v < a_num[temp]; ++v, ++k)
      ans_id[k] = id++;
    for (int v = 0; k < N && v < b_num[temp]; ++v, ++k) {
      ans[k] = temp;
      s.erase(s.find(temp));
    }
    id_ite = id;
  }
  int over = k + a_num[temp] + b_num[temp] - N;
  for (; a_num[temp] > 0 && over > 0;) {
    if ((--id_ite) == -1)
      break;
    if (ans_id[id_ite] != -1)
      continue;
    ans_id[id_ite] = id++;
    --a_num[temp];
    --over;
  }
  for (; b_num[temp] > 0 && over > 0; ++ans_ite) {
    if (ans[ans_ite] != -1)
      continue;
    ans[ans_ite] = temp;
    s.erase(s.find(temp));
    --b_num[temp];
    --over;
  }
  rep(i, a_num[temp]) ans_id[k++] = id++;
  reps(i, k, N) {
    ans[i] = temp;
    s.erase(s.find(temp));
  }
  /*
      rep(i, N) cout<<ans_id[i]<<" \\n"[i == N - 1];
      rep(i, N) cout<<ans[i]<<" \\n"[i == N - 1];
  */
  rep(i, N) {
    if (ans_id[i] == -1)
      ans_id[i] = id++;
    if (ans[i] == -1) {
      ans[i] = *s.begin();
      s.erase(s.begin());
    }
  }
  vec res(N);
  rep(i, N) res[ans_id[i]] = ans[i];
  rep(i, N) cout << res[i] + 1 << " \\n"[i == N - 1];
}