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

#include <bits/stdc++.h>
#pragma GCC optimization("Ofast")
#pragma GCC optimization("unroll-loops")
using namespace std;
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, n) for (int i = 1; i < n; i++)
#define rev(i, n) for (int i = n - 1; i >= 0; i--)
#define all(v) v.begin(), v.end()
#define P pair<int, int>
#define len(s) (int)s.size()

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
constexpr int mod = 1e9 + 7;
constexpr long long inf = 3e18;

int N;
int A[200005], B[200005];
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> N;
  rep(i, N) cin >> A[i];
  rep(i, N) cin >> B[i];
  reverse(B, B + N);
  int cur = 0;
  while (cur < N) {
    int i = cur;
    while (A[i] == B[i])
      i++;
    if (i - cur > N / 2) {
      cout << "No\\n";
      return 0;
    }
    if (i == cur) {
      cur++;
      continue;
    }
    int now = 0, now2 = cur;
    while (now < N && now2 < i) {
      if (now == cur)
        now = i;
      if (B[now] == B[now2]) {
        now++;
        continue;
      }
      if (A[now] == B[now2]) {
        now++;
        continue;
      }
      swap(B[now++], B[now2++]);
    }
    if (now2 != i) {
      cout << "No\\n";
      return 0;
    }
    break;
  }
  cout << "Yes\\n";
  rep(i, N) cout << B[i] << " ";
  cout << "\\n";
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#pragma GCC optimization("Ofast")
#pragma GCC optimization("unroll-loops")
using namespace std;
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define REP(i, n) for (int i = 1; i < n; i++)
#define rev(i, n) for (int i = n - 1; i >= 0; i--)
#define all(v) v.begin(), v.end()
#define P pair<int, int>
#define len(s) (int)s.size()

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
constexpr int mod = 1e9 + 7;
constexpr long long inf = 3e18;

int N, cnt1[200005], cnt2[200005];
int A[200005], B[200005];
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> N;
  rep(i, N) {
    cin >> A[i];
    cnt1[A[i]]++;
  }
  rep(i, N) {
    cin >> B[i];
    cnt2[B[i]]++;
  }
  vector<int> ansA, ansB, ansA2, ansB2;
  rep(i, N + 1) {
    if (cnt1[i] > cnt2[i])
      rep(j, cnt1[i] - cnt2[i]) ansA2.push_back(i);
    else
      rep(j, cnt2[i] - cnt1[i]) ansB2.push_back(i);
    cnt1[i] = min(cnt1[i], cnt2[i]);
  }
  int cur = 0;
  vector<int> rem;
  rep(i, N + 1) {
    chmax(cur, i + 1);
    while (cnt1[i] && cur <= N) {
      if (cur == i) {
        cur++;
        continue;
      }
      if (cnt1[cur] >= cnt1[i]) {
        rep(j, cnt1[i]) {
          ansA.push_back(i);
          ansA.push_back(cur);
          ansB.push_back(cur);
          ansB.push_back(i);
        }
        cnt1[cur] -= cnt1[i];
        cnt1[i] = 0;
        break;
      } else {
        rep(j, cnt1[cur]) {
          ansA.push_back(i);
          ansA.push_back(cur);
          ansB.push_back(cur);
          ansB.push_back(i);
        }
        cnt1[i] -= cnt1[cur];
        cnt1[cur] = 0;
        cur++;
      }
    }
    if (cnt1[i])
      rep(j, cnt1[i]) rem.push_back(i);
  }
  ansA.insert(ansA.end(), all(ansA2));
  ansB.insert(ansB.end(), all(ansB2));
  if (len(rem) > len(ansA)) {
    cout << "No\\n";
    return 0;
  } else if (len(rem)) {
    int pp = rem[0];
    vector<P> ansBB, ansBB2;
    rep(i, len(ansB)) {
      if (ansB[i] != rem[0])
        ansBB.push_back({ansA[i], ansB[i]});
      else
        ansBB2.push_back({ansA[i], ansB[i]});
    }

    rep(i, len(rem)) swap(rem[i], ansBB[i].second);
    ansA.clear();
    ansB.clear();
    rep(i, len(ansBB)) {
      ansA.push_back(ansBB[i].first);
      ansB.push_back(ansBB[i].second);
    }
    rep(i, len(ansBB2)) {
      ansA.push_back(ansBB2[i].first);
      ansB.push_back(ansBB2[i].second);
    }
    rep(i, len(rem)) {
      ansA.push_back(pp);
      ansB.push_back(rem[i]);
    }
  }
  vector<P> ans;
  rep(i, N) { ans.push_back({ansA[i], ansB[i]}); }
  sort(all(ans));
  for (auto i : ans) {
    if (i.first == i.second) {
      cout << "No\\n";
      return 0;
    }
  }
  cout << "Yes\\n";
  for (auto i : ans)
    cout << i.second << " ";
  cout << "\\n";
}