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

#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x, to) for (x = 0; x < (to); x++)
#define FORR(x, arr) for (auto &x : arr)
#define FORR2(x, y, arr) for (auto &[x, y] : arr)
#define ITR(x, c) for (__typeof(c.begin()) x = c.begin(); x != c.end(); x++)
#define ALL(a) (a.begin()), (a.end())
#define ZERO(a) memset(a, 0, sizeof(a))
#define MINUS(a) memset(a, 0xff, sizeof(a))
//-------------------------------------------------------

int N;
int A[202020], B[202020];
int NA[202020], NB[202020];
void solve() {
  int i, j, k, l, r, x, y;
  string s;

  cin >> N;
  FOR(i, N) {
    cin >> A[i];
    NA[A[i]]++;
  }
  FOR(i, N) {
    cin >> B[i];
    NB[B[i]]++;
  }

  int ma = 0;
  for (i = 1; i <= N; i++) {
    if (NA[i] + NB[i] > N)
      return _P("No\\n");
    NA[i] += NA[i - 1];
    NB[i] += NB[i - 1];
    ma = max(ma, NA[i] - NB[i - 1]);
  }

  cout << "Yes" << endl;
  FOR(i, N) cout << B[(i + N - ma) % N] << " ";
  cout << endl;
}

int main(int argc, char **argv) {
  string s;
  int i;
  if (argc == 1)
    ios::sync_with_stdio(false), cin.tie(0);
  FOR(i, argc - 1) s += argv[i + 1], s += '\\n';
  FOR(i, s.size()) ungetc(s[s.size() - 1 - i], stdin);
  cout.tie(0);
  solve();
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x, to) for (x = 0; x < (to); x++)
#define FORR(x, arr) for (auto &x : arr)
#define FORR2(x, y, arr) for (auto &[x, y] : arr)
#define ITR(x, c) for (__typeof(c.begin()) x = c.begin(); x != c.end(); x++)
#define ALL(a) (a.begin()), (a.end())
#define ZERO(a) memset(a, 0, sizeof(a))
#define MINUS(a) memset(a, 0xff, sizeof(a))
//-------------------------------------------------------

int N;
int A[202020], B[202020], R[202020];
vector<int> P[202020], P2[202020];
int NA[202020], NB[202020];
void solve() {
  int i, j, k, l, r, x, y;
  string s;

  cin >> N;
  FOR(i, N) {
    cin >> A[i];
    NA[A[i]]++;
    P[A[i]].push_back(i);
  }
  FOR(i, N) {
    cin >> B[i];
    NB[B[i]]++;
  }

  vector<pair<int, int>> Q;
  priority_queue<pair<int, int>> cand;

  for (i = 1; i <= N; i++) {
    if (NA[i])
      Q.push_back({NA[i], i});
    if (NA[i])
      cand.push({NA[i] + NB[i], i});
    if (NA[i] + NB[i] > N)
      return _P("No\\n");
  }
  sort(ALL(Q));
  reverse(ALL(Q));
  FORR(q, Q) {
    x = q.second;
    while (P[x].size() && cand.size()) {
      while (cand.size()) {
        y = cand.top().second;
        cand.pop();
        if (x == y)
          continue;
        if (NB[y] == 0)
          continue;
        R[P[x].back()] = y;
        P[x].pop_back();
        NB[y]--;
        if (NB[y])
          cand.push({NA[y] + NB[y], y});
        break;
      }
    }
    while (P[x].size()) {
      assert(P[0].size());
      R[P[x].back()] = P[0].back();
      P[x].pop_back();
      P[0].pop_back();
    }
    FOR(y, NB[x]) P[0].push_back(x);
  }

  cout << "Yes" << endl;
  FOR(i, N) cout << R[i] << " ";
  cout << endl;
}

int main(int argc, char **argv) {
  string s;
  int i;
  if (argc == 1)
    ios::sync_with_stdio(false), cin.tie(0);
  FOR(i, argc - 1) s += argv[i + 1], s += '\\n';
  FOR(i, s.size()) ungetc(s[s.size() - 1 - i], stdin);
  cout.tie(0);
  solve();
  return 0;
}