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

/*
    By Prabal Aggarwal
*/
#include <bits/stdc++.h>
#define fastIo                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL)
using namespace std;
#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cout << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define trace(...)
#endif
#define ll long long
#define MOD 1000000007
const int N = 100005;
int C[N];
void Rotate(int B[], int n, int k) {
  k = k % n;
  if (k == 0)
    return;
  int i = 0, j = n - 1;
  while (i < j) {
    swap(B[i], B[j]);
    i++, j--;
  }
  i = 0, j = k - 1;
  while (i < j) {
    swap(B[i], B[j]);
    i++, j--;
  }
  i = k, j = n - 1;
  while (i < j) {
    swap(B[i], B[j]);
    i++, j--;
  }
}
bool f(int A[], int B[], int n) {
  reverse(B, B + n);

  int x = -1;
  for (int i = 0; i < n; i++) {
    if (A[i] == B[i]) {
      x = A[i];
      break;
    }
  }
  if (x == -1)
    return true;

  vector<int> v;
  for (int i = 0; i < n; i++) {
    if (A[i] != B[i] && A[i] != x && B[i] != x) {
      v.push_back(i);
    }
  }

  for (int i = 0; i < n; i++) {
    if (A[i] == B[i]) {
      if (v.size() == 0)
        return false;
      int j = v.back();
      v.pop_back();
      swap(B[i], B[j]);
    }
  }

  return true;
}
int main() {
  fastIo;
  int n;
  cin >> n;

  int A[n];
  int B[n];
  for (int i = 0; i < n; i++)
    cin >> A[i];

  for (int i = 0; i < n; i++)
    cin >> B[i];

  if (f(A, B, n)) {
    cout << "Yes" << endl;
    for (int i = 0; i < n; i++)
      cout << B[i] << " ";
  } else {
    cout << "No";
  }

  return 0;
}

Error: WA

Fixed submission:

/*
    By Prabal Aggarwal
*/
#include <bits/stdc++.h>
#define fastIo                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL)
using namespace std;
#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cout << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define trace(...)
#endif
#define ll long long
#define MOD 1000000007
const int N = 100005;
int C[N];
bool f(int A[], int B[], int n) {
  map<int, int> MA, MB;
  for (int i = 0; i < n; i++) {
    MA[A[i]]++;
    MB[B[i]]++;
  }
  map<int, int>::iterator it;
  bool F = true;
  for (it = MA.begin(); it != MA.end(); it++) {
    int x = it->second;
    int y = it->first;
    y = MB[y];
    y = n - y;

    if (y < x) {
      F = false;
      break;
    }
  }

  if (!F)
    return F;

  for (int i = 0; i < n; i++) {
    int x = A[i];
    it = MB.begin();
    if (it->first == x)
      it++;
    C[i] = it->first;
    it->second--;
    if (it->second == 0) {
      MB.erase(it);
    }
  }
  return F;
}
int main() {
  fastIo;
  int n;
  cin >> n;

  int A[n];
  int B[n];
  for (int i = 0; i < n; i++)
    cin >> A[i];

  for (int i = 0; i < n; i++)
    cin >> B[i];

  if (f(A, B, n)) {
    cout << "Yes" << endl;
    for (int i = 0; i < n; i++)
      cout << C[i] << " ";
  } else {
    cout << "No";
  }

  return 0;
}