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

//@formatter:off
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < int(n); ++i)
#define rrep(i, n) for (int i = int(n) - 1; i >= 0; i--)
#define rep2(i, s, n) for (int i = int(s); i < int(n); ++i)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define pb push_back
#define eb emplace_back
#define vi vector<int>
#define vvi vector<vector<int>>
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vd vector<double>
#define vvd vector<vector<double>>
#define vs vector<string>
#define vc vector<char>
#define vvc vector<vector<char>>
#define vb vector<bool>
#define vvb vector<vector<bool>>
#define vp vector<P>
#define vvp vector<vector<P>>
using namespace std;
using ll = long long;
using P = pair<int, int>;
using LP = pair<ll, ll>;
template <class S, class T> istream &operator>>(istream &is, pair<S, T> &p) {
  return is >> p.first >> p.second;
}
template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> &p) {
  return os << '{' << p.first << "," << p.second << '}';
}
template <class T> istream &operator>>(istream &is, vector<T> &v) {
  for (T &t : v) {
    is >> t;
  }
  return is;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << '[';
  rep(i, v.size()) os << v[i] << (i == int(v.size() - 1) ? "" : ",");
  return os << ']';
}
void Yes(bool b) { cout << (b ? "Yes" : "No") << '\\n'; }
void YES(bool b) { cout << (b ? "YES" : "NO") << '\\n'; }
template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
const int inf = 1001001001;
const ll linf = 1001001001001001001;
//@formatter:on

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int n;
  cin >> n;
  vi a(n), b(n);
  cin >> a >> b;
  vi ca(n + 1), cb(n + 1);
  rep(i, n) {
    ca[a[i]]++;
    cb[b[i]]++;
  }
  rep(i, n) {
    if (ca[i + 1] + cb[i + 1] > n) {
      Yes(0);
      return 0;
    }
    ca[i + 1] += ca[i];
    cb[i + 1] += cb[i];
  }
  int mx = -inf;
  rep(i, n) chmax(mx, ca[i + 1] - cb[i]);
  vi ans(n);
  rep(i, n) {
    int pos = ((i + mx) % n + n) % n;
    ans[pos] = b[i];
  }
  Yes(1);
  rep(i, n) { cout << ans[i] << (i == n - 1 ? '\\n' : ' '); }
}

Error: 0

Fixed submission:

//@formatter:off
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < int(n); ++i)
#define rrep(i, n) for (int i = int(n) - 1; i >= 0; i--)
#define rep2(i, s, n) for (int i = int(s); i < int(n); ++i)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define pb push_back
#define eb emplace_back
#define vi vector<int>
#define vvi vector<vector<int>>
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vd vector<double>
#define vvd vector<vector<double>>
#define vs vector<string>
#define vc vector<char>
#define vvc vector<vector<char>>
#define vb vector<bool>
#define vvb vector<vector<bool>>
#define vp vector<P>
#define vvp vector<vector<P>>
using namespace std;
using ll = long long;
using P = pair<int, int>;
using LP = pair<ll, ll>;
template <class S, class T> istream &operator>>(istream &is, pair<S, T> &p) {
  return is >> p.first >> p.second;
}
template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> &p) {
  return os << '{' << p.first << "," << p.second << '}';
}
template <class T> istream &operator>>(istream &is, vector<T> &v) {
  for (T &t : v) {
    is >> t;
  }
  return is;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << '[';
  rep(i, v.size()) os << v[i] << (i == int(v.size() - 1) ? "" : ",");
  return os << ']';
}
void Yes(bool b) { cout << (b ? "Yes" : "No") << '\\n'; }
void YES(bool b) { cout << (b ? "YES" : "NO") << '\\n'; }
template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
const int inf = 1001001001;
const ll linf = 1001001001001001001;
//@formatter:on

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int n;
  cin >> n;
  vi a(n), b(n);
  cin >> a >> b;
  vi ca(n), cb(n);
  rep(i, n) {
    ca[--a[i]]++;
    cb[--b[i]]++;
  }
  vi v;
  vi rem;
  int mx = 0;
  int cnt = 0;
  rep(i, n) {
    if (ca[i] + cb[i] > n) {
      Yes(0);
      return 0;
    }
    int m = min(ca[i], cb[i]);
    rep(_, m) v.pb(i);
    chmax(mx, m);
    if (cb[i] > m)
      rep(_, cb[i] - m) rem.pb(i);
    if (ca[i] > m and cb[i] == 0)
      cnt++;
  }
  rep(_, cnt) {
    int now = rem.back();
    rem.pop_back();
    v.pb(now);
  }
  assert(v.size() + rem.size() == n);
  if (mx > v.size() / 2) {
    Yes(0);
    return 0;
  }
  vi temp;
  rep2(i, v.size() - v.size() / 2, v.size()) temp.pb(v[i]);
  rep(i, v.size() - v.size() / 2) temp.pb(v[i]);
  swap(temp, v);
  int vcnt = 0, rcnt = 0;
  vi ans;
  rep(i, n) if (ca[i]) {
    int m = min(ca[i], cb[i]);
    rep(_, m) ans.pb(v[vcnt++]);
    if (ca[i] > m) {
      rep(_, ca[i] - m) {
        if (cb[i])
          ans.pb(rem[rcnt++]);
        else
          ans.pb(v[vcnt++]);
      }
    }
  }
  Yes(1);
  rep(i, n) { cout << ans[i] + 1 << (i == n - 1 ? '\\n' : ' '); }
}