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

#include "bits/stdc++.h"
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#ifdef on_linux
#include <sys/resource.h>
#endif

#ifdef dobby_is_a_free_elf
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
#else
#define trace(...) 0;
#endif

using namespace std;
using namespace __gnu_pbds;

#define all(x) (x).begin(), (x).end()
#define between(i, x, y) ((i) >= (x) && (i) <= (y))
#define DECIMAL(n) cout << fixed << setprecision(n)
#define fi first
#define lbd lower_bound
#define mem0(x) memset(x, 0, sizeof(x))
#define mem1(x) memset(x, -1, sizeof(x))
#define mp make_pair
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define sc second
#define sqr(a) ((a) * (a))
#define sz(x) (int)((x).size())
#define ubd upper_bound

// #define int                     long long

using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// find_by_order(k)  returns iterator to kth element starting from 0
// order_of_key(k) returns count of elements strictly smaller than k

const long double PI = 3.14159265358979323846264338;
const double eps = 1e-10;
#ifdef int
const long long INF = 1e18;
#else
const long long INF = 1e9;
#endif
const int32_t fftmod = 998244353;
const int32_t MOD = 1000000007;
const int32_t N = 2e5 + 10;
const int32_t NN = 1e6 + 10;

int cnta[N], cntb[N];

vector<int> ans[N];

inline void elf_magic(int testcase) {
  int n;
  cin >> n;

  int a[n], b[n];
  set<int> s;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    cnta[a[i]]++;
    s.insert(a[i]);
  }
  for (int i = 0; i < n; ++i) {
    cin >> b[i];
    cntb[b[i]]++;
  }

  set<pair<int, int>, greater<pair<int, int>>> ci;
  for (int i = 1; i < N; ++i) {
    if (cnta[i] + cntb[i] > n) {
      cout << "No";
      return;
    }
    if (cntb[i] != 0) {
      ci.insert({cnta[i] + cntb[i], i});
    }
  }

  cout << "Yes\\n";

  auto itr = s.begin();
  while (sz(ci)) {
    auto [c, i] = *ci.begin();
    if (cntb[i]) {
      if (*itr == i) {
        itr++;
        if (itr == s.end()) {
          itr = s.begin();
          assert(*itr != i);
        }
      }
      ans[*itr].pb(i);

      ci.erase({cnta[i] + cntb[i], i});
      ci.erase({cnta[*itr] + cntb[*itr], *itr});

      cntb[i]--;
      cnta[*itr]--;

      if (cntb[i] != 0) {
        ci.insert({cnta[i] + cntb[i], i});
      }
      if (cntb[*itr]) {
        ci.insert({cnta[*itr] + cntb[*itr], *itr});
      }

      if (cnta[*itr] == 0) {
        s.erase(itr);
        itr = s.begin();
      }
    } else {
      assert(0);
    }
  }

  for (int i = 1; i < N; ++i) {
    for (int x : ans[i]) {
      cout << x << ' ';
    }
  }

  return;
}

int32_t main() {
#ifdef on_linux
  rlimit cpu_time{.rlim_cur = 2, .rlim_max = RLIM_INFINITY};
  setrlimit(RLIMIT_CPU, &cpu_time);
#endif

  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);

  int TESTS = 1;
  // cin >> TESTS;
  for (int i = 1; i <= TESTS; ++i) {
    // cout << "Case #" << i << ": ";
    elf_magic(i);
    if (i != TESTS) {
      cout << '\\n';
    }
  }
  return 0;
}

Error: WA

Fixed submission:

#include "bits/stdc++.h"
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#ifdef on_linux
#include <sys/resource.h>
#endif

#ifdef dobby_is_a_free_elf
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
#else
#define trace(...) 0;
#endif

using namespace std;
using namespace __gnu_pbds;

#define all(x) (x).begin(), (x).end()
#define between(i, x, y) ((i) >= (x) && (i) <= (y))
#define DECIMAL(n) cout << fixed << setprecision(n)
#define fi first
#define lbd lower_bound
#define mem0(x) memset(x, 0, sizeof(x))
#define mem1(x) memset(x, -1, sizeof(x))
#define mp make_pair
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define sc second
#define sqr(a) ((a) * (a))
#define sz(x) (int)((x).size())
#define ubd upper_bound

// #define int                     long long

using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// find_by_order(k)  returns iterator to kth element starting from 0
// order_of_key(k) returns count of elements strictly smaller than k

const long double PI = 3.14159265358979323846264338;
const double eps = 1e-10;
#ifdef int
const long long INF = 1e18;
#else
const long long INF = 1e9;
#endif
const int32_t fftmod = 998244353;
const int32_t MOD = 1000000007;
const int32_t N = 2e5 + 10;
const int32_t NN = 1e6 + 10;

int cnta[N], cntb[N];

vector<int> ans[N];

inline void elf_magic(int testcase) {
  int n;
  cin >> n;

  int a[n], b[n];
  set<int> s;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    cnta[a[i]]++;
    s.insert(a[i]);
  }
  for (int i = 0; i < n; ++i) {
    cin >> b[i];
    cntb[b[i]]++;
  }

  vector<pair<int, int>> ci;
  for (int i = 1; i < N; ++i) {
    if (cnta[i] + cntb[i] > n) {
      cout << "No";
      return;
    }
    ci.pb({cnta[i] + cntb[i], i});
  }

  cout << "Yes\\n";

  sort(all(ci));
  reverse(all(ci));

  auto itr = s.begin();
  for (auto [c, i] : ci) {
    while (cntb[i]) {
      if (*itr == i) {
        itr++;
        if (itr == s.end()) {
          itr = s.begin();
          assert(*itr != i);
        }
      }
      ans[*itr].pb(i);
      cntb[i]--;
      cnta[*itr]--;
      if (cnta[*itr] == 0) {
        s.erase(itr);
        itr = s.begin();
      }
    }
  }

  for (int i = 1; i < N; ++i) {
    for (int x : ans[i]) {
      cout << x << ' ';
    }
  }

  return;
}

int32_t main() {
#ifdef on_linux
  rlimit cpu_time{.rlim_cur = 2, .rlim_max = RLIM_INFINITY};
  setrlimit(RLIMIT_CPU, &cpu_time);
#endif

  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);

  int TESTS = 1;
  // cin >> TESTS;
  for (int i = 1; i <= TESTS; ++i) {
    // cout << "Case #" << i << ": ";
    elf_magic(i);
    if (i != TESTS) {
      cout << '\\n';
    }
  }
  return 0;
}