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

#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("avx2")
#pragma GCC optimization("unroll-loops")

using namespace std;
using ll = long long;
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define ff first
#define ss second
#define Ceil(x, y) ((x + y - 1) / y)
#define pb(x) push_back(x)
#define eb(x) emplace_back(x);
const int mod = (int)1000000007;

void solve() {
  int n;
  cin >> n;
  deque<int> freq1(2e5 + 10), freq2(2e5 + 10), A(n), B(n);
  for (int i = 0; i < n; i++) {
    int e;
    cin >> e;
    A[i] = e;
    freq1[e]++;
  }
  for (int i = 0; i < n; i++) {
    int e;
    cin >> e;
    B[i] = e;
    freq2[e]++;
  }
  for (int i = 1; i <= 2e5 + 10; i++)
    if (freq1[i] + freq2[i] > n) {
      cout << "No";
      return;
    }
  cout << "Yes" << endl;
  vector<int> res(n);
  for (int i = 0; i < n; i++)
    if (A[i] != B.front()) {
      res[i] = B.front();
      B.pop_front();
    } else {
      res[i] = B.back();
      B.pop_back();
    }

  for (int i = 0; i < n; i++) {
    if (A[i] == res[i]) {
      for (int j = 0; j < n; j++)
        if (A[i] != res[j] && A[j] != res[i])
          swap(res[j], res[i]);
    }
  }
  for (auto i : res)
    cout << i << " ";
}

int main(int argc, char *argv[]) {
  clock_t clk_start = clock();
  fast;
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  int cases = 1;
  // cin>>cases;
  for (int i = 1; i <= cases; i++) {
    // cout<<"Case #"<<i<<": ";
    solve();
  }
  cerr << "Execution Time: " << fixed
       << double(clock() - clk_start) / double(CLOCKS_PER_SEC)
       << setprecision(6) << " sec\\n";
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("avx2")
#pragma GCC optimization("unroll-loops")

using namespace std;
using ll = long long;
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define ff first
#define ss second
#define Ceil(x, y) ((x + y - 1) / y)
#define pb(x) push_back(x)
#define eb(x) emplace_back(x);
const int mod = (int)1000000007;

void solve() {
  int n;
  cin >> n;
  deque<int> freq1(2e5 + 10), freq2(2e5 + 10), A(n), B(n);
  for (int i = 0; i < n; i++) {
    int e;
    cin >> e;
    A[i] = e;
    freq1[e]++;
  }
  for (int i = 0; i < n; i++) {
    int e;
    cin >> e;
    B[i] = e;
    freq2[e]++;
  }
  for (int i = 1; i <= 2e5 + 10; i++)
    if (freq1[i] + freq2[i] > n) {
      cout << "No";
      return;
    }
  cout << "Yes" << endl;
  set<int> S;
  vector<int> res(n);
  for (int i = 0; i < n; i++)
    S.insert(i);
  set<pair<int, int>, greater<pair<int, int>>> mx;
  for (int i = 1; i <= 2e5 + 10; i++)
    if (freq2[i] != 0)
      mx.insert({freq2[i], i});

  while (S.size()) {
    int i = *S.begin();
    S.erase(i);
    bool check = false;
    for (auto j : mx) {
      if (j.ss != A[i]) {
        check = true;
        res[i] = j.ss;
        int temp = j.ff;
        mx.erase({j.ff, j.ss});
        if (temp > 1)
          mx.insert({temp - 1, res[i]});
        break;
      }
    }
    assert(check);
  }
  for (auto i : res)
    cout << i << " ";
}

int main(int argc, char *argv[]) {
  clock_t clk_start = clock();
  fast;
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  int cases = 1;
  // cin>>cases;
  for (int i = 1; i <= cases; i++) {
    // cout<<"Case #"<<i<<": ";
    solve();
  }
  cerr << "Execution Time: " << fixed
       << double(clock() - clk_start) / double(CLOCKS_PER_SEC)
       << setprecision(6) << " sec\\n";
}