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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

using ll = long long;
using Pll = pair<ll, ll>;
using Pii = pair<int, int>;

constexpr int INF = 1 << 30;
constexpr ll LINF = 1LL << 60;
constexpr ll MOD = 1000000007;
constexpr long double EPS = 1e-10;
constexpr int dyx[4][2] = {{0, 1}, {-1, 0}, {0, -1}, {1, 0}};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  cin >> n;
  vector<int> a(n), b(n);
  vector<int> count_a(n + 1, 0), count_b(n + 1, 0);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    ++count_a[a[i]];
  }
  for (int i = 0; i < n; ++i) {
    cin >> b[i];
    ++count_b[b[i]];
  }

  for (int i = 1; i <= n; ++i) {
    if (count_b[i] > n - count_a[i]) {
      cout << "No" << endl;
      return 0;
    }
    count_a[i] += count_a[i - 1];
    count_b[i] += count_b[i - 1];
  }

  int x = 0;
  for (int i = 1; i <= n; ++i) {
    x = max(x, count_a[i] - count_b[i - 1]);
  }

  cout << "Yes\\n";
  for (int i = 0; i < n; ++i) {
    if (i)
      cout << " ";
    cout << b[(i - x + n) % n];
  }
  cout << "\\n";
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

using ll = long long;
using Pll = pair<ll, ll>;
using Pii = pair<int, int>;

constexpr int INF = 1 << 30;
constexpr ll LINF = 1LL << 60;
constexpr ll MOD = 1000000007;
constexpr long double EPS = 1e-10;
constexpr int dyx[4][2] = {{0, 1}, {-1, 0}, {0, -1}, {1, 0}};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  cin >> n;
  vector<int> a(n), b(n);
  vector<int> count_a(n + 1, 0), count_b(n + 1, 0);
  vector<Pii> count_diff(n + 1);
  for (int i = 0; i <= n; ++i) {
    count_diff[i].first = 0;
    count_diff[i].second = i;
  }
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    ++count_a[a[i]];
    ++count_diff[a[i]].first;
  }
  for (int i = 0; i < n; ++i) {
    cin >> b[i];
    ++count_b[b[i]];
    --count_diff[b[i]].first;
  }

  for (int i = 1; i <= n; ++i) {
    if (count_b[i] > n - count_a[i]) {
      cout << "No" << endl;
      return 0;
    }
  }

  priority_queue<Pii, vector<Pii>, greater<Pii>> que;
  for (int i = 1; i <= n; ++i)
    if (count_b[i])
      que.push(count_diff[i]);

  vector<int> ans(n);
  for (int i = 0; i < n; ++i) {
    Pii p = que.top();
    if (p.second != a[i]) {
      ans[i] = p.second;
      --count_b[p.second];
      if (!count_b[p.second])
        que.pop();
    } else {
      que.pop();
      Pii q = que.top();
      ans[i] = q.second;
      --count_b[q.second];
      if (!count_b[q.second])
        que.pop();
      que.push(p);
    }
  }

  cout << "Yes" << endl;
  for (int i = 0; i < n; ++i) {
    if (i)
      cout << " ";
    cout << ans[i];
  }
  cout << endl;
}