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

#define _USE_MATH_DEFIMES
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

const int MOD = 1'000'000'007;
const int INF = 1'000'000'000; // 1e9
const int NIL = -1;
const long long LINF = 1'000'000'000'000'000'000; // 1e18
const long double EPS = 1E-10;

template <class T, class S> inline bool chmax(T &a, const S &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T, class S> inline bool chmin(T &a, const S &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  int N;
  std::cin >> N;
  std::vector<int> A(N), B(N), C(N + 1), D(N + 1);
  for (int i(0); i < N; ++i) {
    std::cin >> A[i];
    ++C[A[i]];
  }
  for (int i(0); i < N; ++i) {
    std::cin >> B[i];
    ++D[B[i]];
  }
  for (int i(1); i <= N; ++i) {
    if (C[i] + D[i] > N) {
      std::cout << "No" << std::endl;
      return 0;
    }
    C[i] += C[i - 1];
    D[i] += D[i - 1];
  }
  std::cout << "Yes\\n";

  int x(0);
  for (int i(1); i <= N; ++i) {
    chmax(x, C[i] - D[i - 1]);
  }
  for (int i(0); i < N; ++i) {
    if (i)
      std::cout << " ";
    std::cout << B[(i + N - x) % N];
  }
  std::cout << std::endl;
  return 0;
}


Error: WA

Fixed submission:

#define _USE_MATH_DEFIMES
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

const int MOD = 1'000'000'007;
const int INF = 1'000'000'000; // 1e9
const int NIL = -1;
const long long LINF = 1'000'000'000'000'000'000; // 1e18
const long double EPS = 1E-10;

template <class T, class S> inline bool chmax(T &a, const S &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T, class S> inline bool chmin(T &a, const S &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  int N;
  std::cin >> N;
  std::vector<int> A(N), B(N);
  std::map<int, int> cntA, cntB;
  std::priority_queue<int> pq, ap;
  for (int i(0); i < N; ++i) {
    std::cin >> A[i];
    ++cntA[A[i]];
    pq.emplace(A[i]);
  }
  for (int i(0); i < N; ++i) {
    std::cin >> B[i];
    ++cntB[B[i]];
  }
  for (auto [i, j] : cntA) {
    if (j > N - cntB[i]) {
      std::cout << "No" << std::endl;
      return 0;
    }
  }
  std::cout << "Yes\\n";
  for (int i(0); i < N; ++i) {
    if (i)
      std::cout << " ";
    if (!ap.empty() && ap.top() != A[i]) {
      while (!ap.empty()) {
        pq.emplace(ap.top());
        ap.pop();
      }
    }
    if (pq.top() == A[i]) {
      while (!pq.empty() && pq.top() == A[i]) {
        ap.emplace(pq.top());
        pq.pop();
      }
    }
    std::cout << pq.top();
    pq.pop();
  }
  std::cout << std::endl;
  return 0;
}