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

#include "bits/stdc++.h"
// #include <ext/pb_ds/assoc_container.hpp> //required
// #include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define lb long double
#define pb push_back
#define loop(i, start, n) for (ll i = start; i < n; i++)
#define rloop(i, n, start) for (ll i = n; i >= start; i--)
#define mod 1000000007
#define endl "\\n"
#define INF 1e15
#define NEG_INF -1e15
#define pll pair<ll, ll>
#define x first
#define y second
#define all(x) x.begin(), x.end()
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
using namespace std;
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\\'' << x << '\\''; }
void __print(const char *x) { cerr << '\\"' << x << '\\"'; }
void __print(const string &x) { cerr << '\\"' << x << '\\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V> void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T> void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x)
    cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cerr << ", ";
  _print(v...);
}
#ifndef ONLINE_JUDGE
#define debug(x...)                                                            \\
  cerr << "[" << #x << "] = [";                                                \\
  _print(x)
#else
#define debug(x...)
#endif
// using namespace __gnu_pbds;
// template <typename T> using ordered_set =  tree<T, null_type, less<T>,
// rb_tree_tag, tree_order_statistics_node_update>;

int main() {
  IOS;
  ll n;
  cin >> n;
  ll a[n + 1], b[n + 1];
  loop(i, 1, n + 1) cin >> a[i];

  multiset<ll> s, pos;
  loop(i, 1, n + 1) {
    cin >> b[i];
    s.insert(b[i]);
  }

  vector<ll> common, ori;
  ll rot = 0;
  map<ll, ll> mp;
  loop(i, 1, n + 1) {
    if (s.find(a[i]) != s.end()) {
      pos.insert(i);
      s.erase(s.find(a[i]));
      ori.pb(a[i]), common.pb(a[i]);
      mp[a[i]]++;
      rot = max(rot, mp[a[i]]);
    }
  }
  debug(common, rot);
  rotate(common.begin(), common.begin() + rot, common.end());
  debug(common);
  ll cnt = 0, no;
  loop(i, 0, ori.size()) {
    if (ori[i] == common[i])
      cnt++, no = ori[i];
  }

  set<ll> replace;
  loop(i, 1, n + 1) {
    if (pos.find(i) == pos.end()) {
      b[i] = *(s.begin());
      s.erase(s.begin());

      if (a[i] != no && b[i] != no)
        replace.insert(i);
    }
  }

  debug(cnt);
  if (replace.size() < cnt) {
    cout << "No" << endl;
    return 0;
  }

  cout << "Yes" << endl;
  loop(i, 0, ori.size()) {
    if (ori[i] == common[i]) {
      assert(ori[i] == no);
      ll curr = *(replace.begin());
      replace.erase(replace.begin());
      swap(common[i], b[curr]);
    }

    // assert(ori[i] != common[i]);
  }

  ll curr = 0;
  loop(i, 1, n + 1) {
    if (pos.find(i) == pos.end())
      cout << b[i] << " ";
    else
      cout << common[curr++] << " ";
  }
}

Error: WA

Fixed submission:

#include "bits/stdc++.h"
// #include <ext/pb_ds/assoc_container.hpp> //required
// #include <ext/pb_ds/tree_policy.hpp>
#define ll long long
#define lb long double
#define pb push_back
#define loop(i, start, n) for (ll i = start; i < n; i++)
#define rloop(i, n, start) for (ll i = n; i >= start; i--)
#define mod 1000000007
#define endl "\\n"
#define INF 1e15
#define NEG_INF -1e15
#define pll pair<ll, ll>
#define x first
#define y second
#define all(x) x.begin(), x.end()
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
using namespace std;
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\\'' << x << '\\''; }
void __print(const char *x) { cerr << '\\"' << x << '\\"'; }
void __print(const string &x) { cerr << '\\"' << x << '\\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V> void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T> void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x)
    cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cerr << ", ";
  _print(v...);
}
#ifndef ONLINE_JUDGE
#define debug(x...)                                                            \\
  cerr << "[" << #x << "] = [";                                                \\
  _print(x)
#else
#define debug(x...)
#endif
// using namespace __gnu_pbds;
// template <typename T> using ordered_set =  tree<T, null_type, less<T>,
// rb_tree_tag, tree_order_statistics_node_update>;

int main() {
  IOS;
  ll n;
  cin >> n;
  ll a[n + 1], b[n + 1];
  loop(i, 1, n + 1) cin >> a[i];

  multiset<ll> s, pos;
  loop(i, 1, n + 1) {
    cin >> b[i];
    s.insert(b[i]);
  }

  vector<ll> common, ori;
  ll rot = 0;
  map<ll, ll> mp;
  loop(i, 1, n + 1) {
    if (s.find(a[i]) != s.end()) {
      pos.insert(i);
      s.erase(s.find(a[i]));
      ori.pb(a[i]), common.pb(a[i]);
      mp[a[i]]++;
      rot = max(rot, mp[a[i]]);
    }
  }
  debug(common, rot);
  rotate(common.begin(), common.begin() + rot, common.end());
  debug(common);
  ll cnt = 0, no;
  loop(i, 0, ori.size()) {
    if (ori[i] == common[i])
      cnt++, no = ori[i];
  }

  set<ll> replace;
  loop(i, 1, n + 1) {
    if (pos.find(i) == pos.end()) {
      if (a[i] != no)
        replace.insert(i);
      b[i] = *(s.begin());
      s.erase(s.begin());
    }
  }

  debug(cnt);
  if (replace.size() < cnt) {
    cout << "No" << endl;
    return 0;
  }

  cout << "Yes" << endl;
  loop(i, 0, ori.size()) {
    if (ori[i] == common[i]) {
      ll curr = *(replace.begin());
      replace.erase(replace.begin());
      swap(common[i], b[curr]);
    }

    assert(ori[i] != common[i]);
  }

  ll curr = 0;
  loop(i, 1, n + 1) {
    if (pos.find(i) == pos.end())
      cout << b[i] << " ";
    else
      cout << common[curr++] << " ";
  }
}