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

#include <bits/stdc++.h>
using namespace std;
#ifdef tabr
#include "library/debug.cpp"
#else
#define debug(...)
#endif

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int> c(n + 1), x(n + 1), y(n + 1);
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    c[a[i]]++;
    x[a[i]]++;
  }
  vector<int> b(n);
  for (int i = 0; i < n; i++) {
    cin >> b[i];
    c[b[i]]++;
    y[b[i]]++;
  }
  for (int i = 1; i <= n; i++) {
    if (c[i] > n) {
      cout << "No" << '\\n';
      return 0;
    }
  }
  cout << "Yes" << '\\n';
  vector<int> o(n);
  iota(o.begin(), o.end(), 1);
  sort(o.begin(), o.end(), [&](int i, int j) { return c[i] > c[j]; });
  int k = 0;
  a = b = vector<int>(n);
  int ia = 0, ib = 0;
  int ra = n, rb = n;
  for (int i : o) {
    if (k == n) {
      debug(i, ia, ib);
      debug(x[i], y[i]);
      for (int j = 0; j < x[i]; j++) {
        while (a[ia]) {
          ia++;
        }
        a[ia] = i;
      }
      for (int j = 0; j < y[i]; j++) {
        while (b[ib]) {
          ib++;
        }
        b[ib] = i;
      }
      debug("");
    } else if (k + c[i] <= n) {
      for (int j = k; j < k + x[i]; j++) {
        a[j] = i;
      }
      for (int j = k + x[i]; j < k + x[i] + y[i]; j++) {
        b[j] = i;
      }
      ra -= x[i];
      rb -= y[i];
      k += c[i];
    } else {
      int z = -1;
      debug(k);
      for (int j = 0; j <= n - k; j++) {
        if (n - k - j > y[i] || j > x[i]) {
          continue;
        }
        if (ra - n + k >= x[i] - j && rb - n + k >= y[i] - n + k + j) {
          z = j;
          break;
        }
      }
      debug(z);
      assert(z != -1);
      for (int j = 0; j < z; j++) {
        a[n - 1 - j] = i;
      }
      for (int j = z; j < n - k; j++) {
        b[n - 1 - j] = i;
      }
      x[i] -= z;
      y[i] -= n - k - z;
      for (int j = 0; j < x[i]; j++) {
        while (a[ia]) {
          ia++;
        }
        a[ia] = i;
      }
      for (int j = 0; j < y[i]; j++) {
        while (b[ib]) {
          ib++;
        }
        b[ib] = i;
      }
      k = n;
    }
    debug(a, b);
  }
  o = vector<int>(n);
  iota(o.begin(), o.end(), 0);
  sort(o.begin(), o.end(), [&](int i, int j) { return a[i] < a[j]; });
  for (int i : o) {
    cout << b[i] << " ";
  }
  cout << '\\n';
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#ifdef tabr
#include "library/debug.cpp"
#else
#define debug(...)
#endif

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int> c(n + 1), x(n + 1), y(n + 1);
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    c[a[i]]++;
    x[a[i]]++;
  }
  vector<int> b(n);
  for (int i = 0; i < n; i++) {
    cin >> b[i];
    c[b[i]]++;
    y[b[i]]++;
  }
  for (int i = 1; i <= n; i++) {
    if (c[i] > n) {
      cout << "No" << '\\n';
      return 0;
    }
  }
  cout << "Yes" << '\\n';
  vector<int> o(n);
  iota(o.begin(), o.end(), 1);
  sort(o.begin(), o.end(), [&](int i, int j) { return c[i] > c[j]; });
  int k = 0;
  a = b = vector<int>(n);
  int ia = 0, ib = 0;
  for (int i : o) {
    if (k == n) {
      for (int j = 0; j < x[i]; j++) {
        while (a[ia]) {
          ia++;
        }
        a[ia] = i;
      }
      for (int j = 0; j < y[i]; j++) {
        while (b[ib]) {
          ib++;
        }
        b[ib] = i;
      }
    } else if (k + c[i] <= n) {
      for (int j = k; j < k + x[i]; j++) {
        a[j] = i;
      }
      for (int j = k + x[i]; j < k + x[i] + y[i]; j++) {
        b[j] = i;
      }
      k += c[i];
    } else {
      for (int j = n - 1; j >= 0; j--) {
        if (!y[i]) {
          break;
        }
        while (b[j]) {
          j--;
        }
        b[j] = i;
        y[i]--;
      }
      for (int j = n - 1; j >= 0; j--) {
        if (!x[i]) {
          break;
        }
        while (a[j] || b[j] == i) {
          j--;
        }
        a[j] = i;
        x[i]--;
      }
      k = n;
    }
  }
  o = vector<int>(n);
  iota(o.begin(), o.end(), 0);
  sort(o.begin(), o.end(), [&](int i, int j) { return a[i] < a[j]; });
  for (int i : o) {
    cout << b[i] << " ";
  }
  cout << '\\n';
  return 0;
}