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

#include <bits/stdc++.h>

using namespace std;

#define REP(i, n) for (int i = 0; i < (n); i++)
#define REPS(i, n) for (int i = 1; i <= (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define RREPS(i, n) for (int i = (n); i > 0; i--)
#define ALL(v) v.begin(), v.end()
#define RALL(v) v.rbegin(), v.rend()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())
#define mp make_pair
#define mt make_tuple
#define pb push_back

using ll = long long;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vs = vector<string>;
using vb = vector<bool>;
using vvi = vector<vi>;
using vvl = vector<vl>;

const int MOD = 1e9 + 7;
const int INF = 1e9 + 7;
const ll INFL = 1e18;
const double PI = 3.141592653589793;
const double EPS = 1e-9;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(20);

  int N;
  cin >> N;
  vi A(N), B(N);
  REP(i, N) cin >> A[i];
  REP(i, N) cin >> B[i];

  vi countA(200001), countB(200001);
  REP(i, N) {
    countA[A[i]]++;
    countB[B[i]]++;
  }

  vector<pi> tmp(N);
  REP(i, N) tmp[i] = mp(countA[A[i]], i);
  sort(ALL(tmp));
  vi indexA(N);
  REP(i, N) indexA[i] = tmp[i].second;

  /*
      cout << endl;
      REP(i, N) cout << indexA[i] << " ";
      cout << endl;
  */

  priority_queue<pi> q;
  REP(i, 200001) if (countB[i] > 0) q.push(mp(countA[i], i));
  q.push(mp(-1, -1));

  vi ans(N, -1);
  REP(i, N) {
    pi most = q.top();
    q.pop();
    pi less = q.top();
    q.pop();

    if (A[indexA[i]] == most.second) {
      if (countB[less.second] == 0 || less.first == -1)
        break;
      q.push(most);
      ans[indexA[i]] = less.second;
      countB[less.second]--;
      if (countB[less.second] > 0)
        q.push(less);
    } else {
      if (countB[most.second] == 0 || most.first == -1)
        break;
      q.push(less);
      ans[indexA[i]] = most.second;
      countB[most.second]--;
      if (countB[most.second] > 0)
        q.push(most);
    }
  }

  bool ok = true;
  REP(i, N) if (A[i] == ans[i] || ans[i] == -1) ok = false;

  if (ok) {
    cout << "Yes" << endl;
    REP(i, N) cout << ans[i] << " ";
    cout << endl;
    return 0;
  }
  cout << "No" << endl;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define REP(i, n) for (int i = 0; i < (n); i++)
#define REPS(i, n) for (int i = 1; i <= (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define RREPS(i, n) for (int i = (n); i > 0; i--)
#define ALL(v) v.begin(), v.end()
#define RALL(v) v.rbegin(), v.rend()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())
#define mp make_pair
#define mt make_tuple
#define pb push_back

using ll = long long;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vs = vector<string>;
using vb = vector<bool>;
using vvi = vector<vi>;
using vvl = vector<vl>;

const int MOD = 1e9 + 7;
const int INF = 1e9 + 7;
const ll INFL = 1e18;
const double PI = 3.141592653589793;
const double EPS = 1e-9;

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(20);

  int N;
  cin >> N;
  vi A(N), B(N);
  REP(i, N) cin >> A[i];
  REP(i, N) cin >> B[i];

  vi countA(200001), countB(200001);
  REP(i, N) {
    countA[A[i]]++;
    countB[B[i]]++;
  }

  vector<pi> tmp(N);
  REP(i, N) tmp[i] = mp(countA[A[i]], i);
  sort(ALL(tmp));
  vi indexA(N);
  REP(i, N) indexA[i] = tmp[i].second;

  /*
      cout << endl;
      REP(i, N) cout << indexA[i] << " ";
      cout << endl;
  */

  priority_queue<pi> q;
  REP(i, 200001) if (countB[i] > 0) q.push(mp(countA[i], i));
  q.push(mp(-1, -1));

  vi ans(N, -1);
  REP(i, N) {
    pi most = q.top();
    q.pop();
    pi less = q.top();
    q.pop();

    if (A[indexA[i]] == most.second) {
      if (less.first == 0)
        break;
      q.push(most);
      ans[indexA[i]] = less.second;
      countB[less.second]--;
      if (countB[less.second] > 0)
        q.push(less);
    } else {
      if (most.first == 0)
        break;
      q.push(less);
      ans[indexA[i]] = most.second;
      countB[most.second]--;
      if (countB[most.second] > 0)
        q.push(most);
    }
  }

  bool ok = true;
  REP(i, N) if (A[i] == ans[i] || ans[i] == -1) ok = false;

  if (ok) {
    cout << "Yes" << endl;
    REP(i, N) cout << ans[i] << " ";
    cout << endl;
    return 0;
  }
  cout << "No" << endl;
}