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

#include <bits/stdc++.h>
using namespace std;
// #pragma GCC optimize ("O3")
// #pragma GCC target ("sse4")
#define endl "\\n"
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
template <class T, class T2> inline bool chkmax(T &x, const T2 &y) {
  return x < y ? x = y, 1 : 0;
}
template <class T, class T2> inline bool chkmin(T &x, const T2 &y) {
  return x > y ? x = y, 1 : 0;
}
const ll mod = 1e9 + 7;
template <class T> inline void fix(T &x) {
  if (x >= mod | x <= -mod) {
    x %= mod;
  }
  if (x < 0) {
    x += mod;
  }
}
#define out(x) cout << __LINE__ << ": " << (#x) << " = " << (x) << endl

const int MAX_N = 2e5 + 10;
priority_queue<pair<int, int>> pq;
vector<int> cnta[MAX_N], cntb[MAX_N];
int a[MAX_N], b[MAX_N];
vector<int> freea, freeb;

void pushInPq(int i) {
  if (cnta[i].size() + cntb[i].size() > 0) {
    pq.push({cnta[i].size() + cntb[i].size(), i});
  }
}

signed main() {
  // ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    cnta[a[i]].push_back(i);
  }
  for (int i = 0; i < n; i++) {
    cin >> b[i];
    cntb[b[i]].push_back(b[i]);
  }
  for (int i = 0; i < MAX_N; i++) {
    pushInPq(i);
  }
  vector<int> ans;
  ans.resize(n);
  for (auto &it : ans) {
    it = -1;
  }
  for (int lft = n; lft >= 0; lft--) {
    while (!pq.empty()) {
      auto tp = pq.top();
      pq.pop();
      bool got = false;
      if (cnta[tp.second].size() && freeb.size()) {
        ans[cnta[tp.second].back()] = freeb.back();
        cnta[tp.second].pop_back();
        freeb.pop_back();
        got = true;
      }
      if (cntb[tp.second].size() && freea.size()) {
        ans[freea.back()] = cntb[tp.second].back();
        cntb[tp.second].pop_back();
        freea.pop_back();
        got = true;
      }
      pushInPq(tp.second);
      if (!got) {
        break;
      }
    }
    if (pq.size() >= 2) {
      auto frst = pq.top();
      pq.pop();
      auto sec = pq.top();
      pq.pop();
      if (cnta[frst.second].size() && cntb[sec.second].size()) {
        ans[cnta[frst.second].back()] = cntb[sec.second].back();
        cnta[frst.second].pop_back();
        cntb[sec.second].pop_back();
      } else if (cntb[frst.second].size() && cnta[sec.second].size()) {
        ans[cnta[sec.second].back()] = cntb[frst.second].back();
        cntb[frst.second].pop_back();
        cnta[sec.second].pop_back();
      } else if (cnta[frst.second].size()) {
        freea.push_back(cnta[frst.second].back());
        freea.push_back(cnta[sec.second].back());
        cnta[frst.second].pop_back();
        cnta[sec.second].pop_back();
      } else {
        freeb.push_back(frst.second);
        freeb.push_back(sec.second);
        cntb[sec.second].pop_back();
        cntb[frst.second].pop_back();
      }
      pushInPq(frst.second);
      pushInPq(sec.second);
    }
    if (!pq.empty() && pq.top().first > lft) {
      cout << "No" << endl;
      return 0;
    }
  }
  for (auto it : ans) {
    if (it == -1) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
  for (auto it : ans) {
    cout << it << " ";
  }
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
// #pragma GCC optimize ("O3")
// #pragma GCC target ("sse4")
#define endl "\\n"
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
template <class T, class T2> inline bool chkmax(T &x, const T2 &y) {
  return x < y ? x = y, 1 : 0;
}
template <class T, class T2> inline bool chkmin(T &x, const T2 &y) {
  return x > y ? x = y, 1 : 0;
}
const ll mod = 1e9 + 7;
template <class T> inline void fix(T &x) {
  if (x >= mod | x <= -mod) {
    x %= mod;
  }
  if (x < 0) {
    x += mod;
  }
}
#define out(x) cout << __LINE__ << ": " << (#x) << " = " << (x) << endl

const int MAX_N = 2e5 + 10;
priority_queue<pair<int, int>> pq;
vector<int> cnta[MAX_N], cntb[MAX_N];
int a[MAX_N], b[MAX_N];
vector<int> freea, freeb;

void pushInPq(int i) {
  if (cnta[i].size() + cntb[i].size() > 0) {
    pq.push({cnta[i].size() + cntb[i].size(), i});
  }
}

signed main() {
  // ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    cnta[a[i]].push_back(i);
  }
  for (int i = 0; i < n; i++) {
    cin >> b[i];
    cntb[b[i]].push_back(b[i]);
  }
  for (int i = 0; i < MAX_N; i++) {
    pushInPq(i);
  }
  vector<int> ans;
  ans.resize(n);
  for (auto &it : ans) {
    it = -1;
  }
  for (int lft = n; lft >= 0; lft--) {
    while (!pq.empty()) {
      auto tp = pq.top();
      pq.pop();
      bool got = false;
      if (cnta[tp.second].size() && freeb.size()) {
        ans[cnta[tp.second].back()] = freeb.back();
        cnta[tp.second].pop_back();
        freeb.pop_back();
        got = true;
      }
      if (cntb[tp.second].size() && freea.size()) {
        ans[freea.back()] = cntb[tp.second].back();
        cntb[tp.second].pop_back();
        freea.pop_back();
        got = true;
      }
      pushInPq(tp.second);
      if (!got) {
        break;
      }
    }
    if (pq.size() >= 2) {
      auto frst = pq.top();
      pq.pop();
      auto sec = pq.top();
      pq.pop();
      if (cnta[frst.second].size() && cntb[sec.second].size()) {
        ans[cnta[frst.second].back()] = cntb[sec.second].back();
        cnta[frst.second].pop_back();
        cntb[sec.second].pop_back();
      } else if (cntb[frst.second].size() && cnta[sec.second].size()) {
        ans[cnta[sec.second].back()] = cntb[frst.second].back();
        cntb[frst.second].pop_back();
        cnta[sec.second].pop_back();
      } else if (cnta[frst.second].size()) {
        freea.push_back(cnta[frst.second].back());
        freeb.push_back(sec.second);
        cnta[frst.second].pop_back();
        cntb[sec.second].pop_back();
      } else {
        freea.push_back(cnta[sec.second].back());
        freeb.push_back(frst.second);
        cnta[sec.second].pop_back();
        cntb[frst.second].pop_back();
      }
      pushInPq(frst.second);
      pushInPq(sec.second);
    }
    if (!pq.empty() && pq.top().first > lft) {
      cout << "No" << endl;
      return 0;
    }
  }
  for (auto it : ans) {
    if (it == -1) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
  for (auto it : ans) {
    cout << it << " ";
  }
  return 0;
}