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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
using pll = pair<long long, long long>;
constexpr int INF = 1e9;
constexpr long long LINF = 1e17;
constexpr int MOD = 1000000007;
constexpr double PI = 3.14159265358979323846;

int cnta[200005];
int cntb[200005];
int a[200005];
int b[200005];

int main() {
  int n;
  cin >> n;
  rep(i, n) {
    cin >> a[i];
    cnta[a[i]]++;
  }
  rep(i, n) {
    cin >> b[i];
    cntb[b[i]]++;
  }

  bool ok = true;
  rep(i, 200005) {
    if (cnta[i] + cntb[i] > n)
      ok = false;
  }

  if (!ok) {
    cout << "No" << endl;
    return 0;
  }
  priority_queue<pll> pq;

  rep(i, 200005) {
    if (cntb[i] > 0)
      pq.push(pll{i, cntb[i]});
  }
  vector<int> c(n);

  int id = 0;
  rep(i, n) {
    if (a[i] != pq.top().first) {
      c[i] = pq.top().first;
      pll now = pq.top();
      pq.pop();
      now.second--;
      if (now.second == 0)
        continue;
      else
        pq.push(now);
    } else {
      pll before = pq.top();
      pq.pop();

      if (!pq.empty()) {
        c[i] = pq.top().first;
        pll now = pq.top();
        pq.pop();
        now.second--;
        if (now.second == 0)
          ;
        else
          pq.push(now);
      } else {
        ok = false;
        id = i;
        pq.push(before);
        break;
      }

      pq.push(before);
    }
  }

  if (!ok) {
    vector<int> idx;
    for (int i = id; i < n; i++) {
      if (a[i] == pq.top().first)
        idx.push_back(i);
      c[i] = pq.top().first;
    }
    int p = 0;
    for (int i = 0; i < id; i++) {
      if (a[i] == pq.top().first)
        continue;
      if (c[i] == pq.top().first)
        continue;

      swap(c[i], c[idx[p]]);
      ++p;
      if (p == (int)idx.size())
        break;
    }
  }

  cout << "Yes" << endl;
  rep(i, n) { cout << c[i] << " "; }
  cout << endl;
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
using pll = pair<long long, long long>;
constexpr int INF = 1e9;
constexpr long long LINF = 1e17;
constexpr int MOD = 1000000007;
constexpr double PI = 3.14159265358979323846;

int cnta[200005];
int cntb[200005];
int a[200005];
int b[200005];

int main() {
  int n;
  cin >> n;
  rep(i, n) {
    cin >> a[i];
    cnta[a[i]]++;
  }
  rep(i, n) {
    cin >> b[i];
    cntb[b[i]]++;
  }

  bool ok = true;
  rep(i, 200005) {
    if (cnta[i] + cntb[i] > n)
      ok = false;
  }

  if (!ok) {
    cout << "No" << endl;
    return 0;
  }
  priority_queue<pll> pq;

  rep(i, 200005) {
    if (cntb[i] > 0)
      pq.push(pll{i, cntb[i]});
  }
  vector<int> c(n);

  rep(i, n) {
    if (a[i] != pq.top().first) {
      c[i] = pq.top().first;
      pll now = pq.top();
      pq.pop();
      now.second--;
      if (now.second == 0)
        continue;
      else
        pq.push(now);
    } else {
      pll before = pq.top();
      pq.pop();

      if (!pq.empty()) {
        c[i] = pq.top().first;
        pll now = pq.top();
        pq.pop();
        now.second--;
        if (now.second == 0)
          ;
        else
          pq.push(now);
      } else {
        ok = false;
        break;
      }

      pq.push(before);
    }
  }

  if (!ok) {

    rep(i, 200005) {
      if (cntb[i] > 0)
        pq.push(pll{cntb[i], i});
    }

    rep(i, n) {
      if (a[i] != pq.top().second) {
        c[i] = pq.top().second;
        pll now = pq.top();
        pq.pop();
        now.first--;
        if (now.first == 0)
          continue;
        else
          pq.push(now);
      } else {
        pll before = pq.top();
        pq.pop();

        c[i] = pq.top().second;
        pll now = pq.top();
        pq.pop();
        now.first--;
        if (now.first == 0)
          ;
        else
          pq.push(now);

        pq.push(before);
      }
    }
  }

  cout << "Yes" << endl;
  rep(i, n) { cout << c[i] << " "; }
  cout << endl;
  return 0;
}