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

/* ****mittal21**** */
#include <bits/stdc++.h>
#define ll long long int
#define ld long double
#define pb push_back
#define endl '\\n'
#define pii pair<long long int, long long int>
#define vi vector<ll>
#define all(a) (a).begin(), (a).end()
#define F first
#define S second
#define sz(x) (ll) x.size()
#define hell 1000000007
#define jell 998244353
#define rep(i, a, b) for (ll int i = a; i < b; i++)
#define repr(i, a, b) for (ll int i = a; i >= b; i--)
#define lbnd lower_bound
#define ubnd upper_bound
#define mp make_pair
#define DEC(N)                                                                 \\
  cout << std::fixed;                                                          \\
  cout << setprecision(N);
using namespace std;
#define N 100005
void solve() {
  ll n;
  cin >> n;
  ll a[n + 3], b[n + 3];
  ll lol[n + 3], hol[n + 3];
  memset(lol, 0, sizeof(lol));
  memset(hol, 0, sizeof(hol));
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    hol[a[i]]++;
  }
  for (int i = 1; i <= n; i++) {
    cin >> b[i];
    lol[b[i]]++;
  }
  ll fl = 0;
  for (int i = 1; i <= n; i++) {
    if (lol[a[i]] == 0) {
      continue;
    }
    if (lol[a[i]] > n - hol[a[i]])
      fl = 1;
  }
  if (fl) {
    cout << "No";
    return;
  }
  priority_queue<pair<ll, ll>> pq;
  for (int i = 1; i <= n; i++) {
    if (hol[i] > 0 && lol[i] > 0) {
      pq.push({lol[i], i});
      // cout<<lol[i]<<" "<<i<<endl;
    }
  }
  ll c[n + 3];
  memset(c, -1, sizeof(c));
  for (int i = 1; i <= n; i++) {
    if (pq.size() == 0)
      break;
    pair<ll, ll> temp = pq.top();
    pq.pop();
    if (temp.S == a[i]) {
      if (pq.size() == 0) {
        pq.push(temp);
        continue;
      }
      pair<ll, ll> temp1 = pq.top();
      pq.pop();
      c[i] = temp1.S;
      pq.push(temp);
      if (temp1.F != 1) {
        pq.push({temp1.F - 1, temp1.S});
      }
    } else {
      c[i] = temp.S;
      if (temp.F != 1) {
        pq.push({temp.F - 1, temp.S});
      }
    }
  }
  ll cnt = 1;
  std::vector<ll> vv;
  for (int i = 1; i <= n; i++) {
    if (lol[i] > 0 && hol[i] == 0) {
      for (int j = 0; j < lol[i]; j++)
        vv.pb(i);
    }
  }
  cnt = 0;
  for (int i = 1; i <= n; i++) {
    if (c[i] == -1) {
      if (cnt >= vv.size())
        break;
      c[i] = vv[cnt];
      cnt++;
    }
  }
  ll w = 0;
  for (int i = 1; i <= n; i++) {
    if (c[i] == -1) {
      for (int j = 1; j <= n; j++) {
        if (i != j && a[i] != a[j] && c[j] != a[i]) {
          c[i] = c[j];
          c[j] = a[i];
          w = 1;
          break;
        }
      }
    }
  }
  cout << "Yes" << endl;
  for (int i = 1; i <= n; i++) {
    cout << c[i] << " ";
  }
  return;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ll testcase = 1;
  // cin>>testcase;
  while (testcase--) {
    solve();
  }
  return 0;
}


Error: WA

Fixed submission:

/* ****mittal21**** */
#include <bits/stdc++.h>
#define ll long long int
#define ld long double
#define pb push_back
#define endl '\\n'
#define pii pair<long long int, long long int>
#define vi vector<ll>
#define all(a) (a).begin(), (a).end()
#define F first
#define S second
#define sz(x) (ll) x.size()
#define hell 1000000007
#define jell 998244353
#define rep(i, a, b) for (ll int i = a; i < b; i++)
#define repr(i, a, b) for (ll int i = a; i >= b; i--)
#define lbnd lower_bound
#define ubnd upper_bound
#define mp make_pair
#define DEC(N)                                                                 \\
  cout << std::fixed;                                                          \\
  cout << setprecision(N);
using namespace std;
#define N 100005
void solve() {
  ll n;
  cin >> n;
  ll a[n + 3], b[n + 3];
  ll lol[n + 3], hol[n + 3];
  memset(lol, 0, sizeof(lol));
  memset(hol, 0, sizeof(hol));
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    hol[a[i]]++;
  }
  for (int i = 1; i <= n; i++) {
    cin >> b[i];
    lol[b[i]]++;
  }
  ll fl = 0;
  for (int i = 1; i <= n; i++) {
    if (lol[a[i]] == 0) {
      continue;
    }
    if (lol[a[i]] > n - hol[a[i]])
      fl = 1;
  }
  if (fl) {
    cout << "No";
    return;
  }
  priority_queue<pair<ll, ll>> pq;
  for (int i = 1; i <= n; i++) {
    if (hol[i] > 0 && lol[i] > 0) {
      pq.push({lol[i], i});
      // cout<<lol[i]<<" "<<i<<endl;
    }
  }
  ll c[n + 3];
  memset(c, -1, sizeof(c));
  for (int i = 1; i <= n; i++) {
    if (pq.size() == 0)
      break;
    pair<ll, ll> temp = pq.top();
    pq.pop();
    if (temp.S == a[i]) {
      if (pq.size() == 0) {
        pq.push(temp);
        continue;
      }
      pair<ll, ll> temp1 = pq.top();
      pq.pop();
      c[i] = temp1.S;
      pq.push(temp);
      if (temp1.F != 1) {
        pq.push({temp1.F - 1, temp1.S});
      }
    } else {
      c[i] = temp.S;
      if (temp.F != 1) {
        pq.push({temp.F - 1, temp.S});
      }
    }
  }
  ll cnt = 1;
  std::vector<ll> vv;
  for (int i = 1; i <= n; i++) {
    if (lol[i] > 0 && hol[i] == 0) {
      for (int j = 0; j < lol[i]; j++)
        vv.pb(i);
    }
  }
  cnt = 0;
  for (int i = 1; i <= n; i++) {
    if (c[i] == -1) {
      c[i] = vv[cnt];
      cnt++;
      if (cnt >= vv.size())
        break;
    }
  }
  cout << "Yes" << endl;
  for (int i = 1; i <= n; i++) {
    cout << c[i] << " ";
  }
  return;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ll testcase = 1;
  // cin>>testcase;
  while (testcase--) {
    solve();
  }
  return 0;
}