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

#include <bits/stdc++.h>
#define sz(v) ((int)(v).size())
#define all(v) ((v).begin()), ((v).end())
#define allr(v) ((v).rbegin()), ((v).rend())
#define pb push_back
#define mp make_pair
#define clr(v, d) memset(v, d, sizeof(v))
#define PI acos(-1)
typedef long long ll;
typedef unsigned long long ull;
const double eps = (1e-9);
using namespace std;
int dcmp(long double a, long double b) {
  return fabsl(a - b) <= eps ? 0 : (a > b) ? 1 : -1;
}
int getBit(ll num, int idx) { return ((num >> idx) & 1ll) == 1; }
int setBit1(int num, int idx) { return num | (1 << idx); }
ll setBit0(ll num, int idx) { return num & ~(1ll << idx); }
ll flipBit(ll num, int idx) { return num ^ (1ll << idx); }
void GO() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
int countNumBit1(int mask) {
  int ret = 0;
  while (mask) {
    mask &= (mask - 1);
    ++ret;
  }
  return ret;
}
const int N = 200000 + 9, mod = 1e9 + 7;
ll a[N], b[N];
int vis1[N], vis2[N];
priority_queue<pair<int, int>> pq;
pair<int, int> get_sec() {

  pair<int, int> tmp = pq.top();
  pq.pop();
  pair<int, int> ret = pq.top();
  pq.pop();
  pq.push(tmp);
  return ret;
}
int main() {
  GO();

  int T;
  //  cin>>T;
  T = 1;
  while (T--) {
    int n;
    cin >> n;

    for (int i = 0; i < n; i++)
      cin >> a[i], vis2[a[i]]++;

    for (int i = 0; i < n; i++) {
      int no;
      cin >> no;
      vis1[no]++;
    }
    for (int i = 1; i <= 200000; i++) {
      if (vis1[i]) {
        pq.push(mp(min(vis1[i], vis2[i]), i));
      }
    }
    for (int i = 0; i < n; i++) {
      int num = pq.top().second;
      int cnt = pq.top().first;
      if (num != a[i]) {
        pq.pop();
        b[i] = num;
        vis1[num]--;
        int nw_cnt = min(vis1[num], vis2[num]);
        if (vis1[num]) {
          pq.push(mp(nw_cnt, num));
        }
      } else if (sz(pq) > 1) {
        pair<int, int> p = get_sec();
        num = p.second;
        cnt = p.first;

        b[i] = num;
        vis1[num]--;
        int nw_cnt = min(vis1[num], vis2[num]);
        if (vis1[num]) {
          pq.push(mp(nw_cnt, num));
        }
      } else {
        cout << "No\\n";
        return 0;
      }
      vis2[a[i]]--;
    }
    cout << "Yes\\n";
    for (int i = 0; i < n; i++)
      cout << b[i] << " ";
    cout << "\\n";
  }
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define sz(v) ((int)(v).size())
#define all(v) ((v).begin()), ((v).end())
#define allr(v) ((v).rbegin()), ((v).rend())
#define pb push_back
#define mp make_pair
#define clr(v, d) memset(v, d, sizeof(v))
#define PI acos(-1)
typedef long long ll;
typedef unsigned long long ull;
const double eps = (1e-9);
using namespace std;
int dcmp(long double a, long double b) {
  return fabsl(a - b) <= eps ? 0 : (a > b) ? 1 : -1;
}
int getBit(ll num, int idx) { return ((num >> idx) & 1ll) == 1; }
int setBit1(int num, int idx) { return num | (1 << idx); }
ll setBit0(ll num, int idx) { return num & ~(1ll << idx); }
ll flipBit(ll num, int idx) { return num ^ (1ll << idx); }
void GO() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
int countNumBit1(int mask) {
  int ret = 0;
  while (mask) {
    mask &= (mask - 1);
    ++ret;
  }
  return ret;
}
const int N = 200000 + 9, mod = 1e9 + 7;
ll a[N], b[N];
int vis1[N], vis2[N];
priority_queue<pair<int, int>> pq;
pair<int, int> get_sec() {

  pair<int, int> tmp = pq.top();
  pq.pop();
  pair<int, int> ret = pq.top();
  pq.pop();
  pq.push(tmp);
  return ret;
}
int main() {
  GO();

  int T;
  //  cin>>T;
  T = 1;
  while (T--) {
    int n;
    cin >> n;

    for (int i = 0; i < n; i++)
      cin >> a[i], vis2[a[i]]++;

    for (int i = 0; i < n; i++) {
      int no;
      cin >> no;
      vis1[no]++;
    }
    for (int i = 1; i <= 200000; i++) {
      if (vis1[i] && vis2[i]) {
        pq.push(mp(min(vis1[i], vis2[i]), i));
      }
    }
    for (int i = 0; i < n; i++) {
      int num = pq.top().second;
      int cnt = pq.top().first;
      if (num != a[i]) {
        pq.pop();
        b[i] = num;
        vis1[num]--;
        int nw_cnt = min(vis1[num], vis2[num]);
        if (nw_cnt > 0) {
          pq.push(mp(nw_cnt, num));
        }
      } else if (sz(pq) > 1) {
        pair<int, int> p = get_sec();
        num = p.second;
        cnt = p.first;

        b[i] = num;
        vis1[num]--;
        int nw_cnt = min(vis1[num], vis2[num]);
        if (nw_cnt > 0) {
          pq.push(mp(nw_cnt, num));
        }
      } else {
        cout << "No\\n";
        return 0;
      }
      vis2[a[i]]--;
    }
    cout << "Yes\\n";
    for (int i = 0; i < n; i++)
      cout << b[i] << " ";
    cout << "\\n";
  }
}