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

#include <bits/stdc++.h>
using namespace std;
///////////////////////////////////////////
const long long int INF = 1LL << 60;
const long long int Mod = 1000000007;
const long long int sMod = 998244353;
using ll = long long int;
using ci = const int;
using vi = vector<int>;
using Vi = vector<long long int>;
using P = pair<int, int>;
using PLL = pair<ll, ll>;
using matrix = vector<vector<ll>>;
#define pb(x) push_back(x)
#define mp(x, y) make_pair(x, y)
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define rp(i, N) for (ll i = 0; i < (ll)N; i++)
#define repi(i, a, b) for (ll i = ll(a); i < ll(b); ++i)
template <class T> bool chmax(T &former, const T &b) {
  if (former < b) {
    former = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &former, const T &b) {
  if (b < former) {
    former = b;
    return true;
  }
  return false;
}
template <class T> T sqar(T x) { return x * x; } // sqrt(x)は平方根;
#define Sort(v)                                                                \\
  std::sort(v.begin(), v.end(),                                                \\
            std::greater<decltype(v[0])>()) // 降順でVをソート
#define p_queue(v) priority_queue<v, vector<v>, greater<v>>
template <class T> inline void princ(T x) { cout << x << " "; };
template <class T> inline void print(T x) { cout << x << "\\n"; };
template <class T> inline void Yes(T condition) {
  if (condition)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}
template <class T> inline void YES(T condition) {
  if (condition)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}
///////////////////////////////////////////////////////////////////////////////////

void solve() {
  ll n;
  cin >> n;
  Vi c(n + 1), d(n + 1);
  Vi a(n), b(n);
  rp(i, n) cin >> a[i];
  rp(i, n) cin >> b[i];
  Vi af(n + 1, 0), bf(n + 1, 0);
  rp(i, n) {
    af[a[i]]++;
    bf[b[i]]++;
    // print("a");
  }
  rp(i, n + 1) {
    if (af[i] + bf[i] > n) {
      print("No");
      return;
    }
  }
  rp(i, n + 1) {
    c[i] = upper_bound(all(a), i) - a.begin();
    d[i] = upper_bound(all(b), i) - b.begin();
  }
  ll m = -1000000;
  // for(auto cc:c){
  //     princ(cc);
  // }
  // print("");
  // for(auto dd:d){
  //     princ(dd);
  // }
  // print("");
  repi(i, 1, n + 1) { chmax(m, c[i] - d[i - 1]); }
  if (m == n)
    m = 0;
  print("Yes");
  rp(i, n) { princ(b[(i - m) < 0 ? (i - m) % n + n : (i - m) % n]); }
  cout << endl;
  return;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  std::cout << std::fixed << std::setprecision(30);
  solve();
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
// #include "atcoder/all"
using namespace std;
// using namespace atcoder;
///////////////////////////////////////////
const long long int INF = 1LL << 60;
const long long int Mod = 1000000007;
const long long int sMod = 998244353;
using ll = long long int;
using ci = const int;
using vi = vector<int>;
using Vi = vector<long long int>;
using P = pair<int, int>;
using PLL = pair<ll, ll>;
using matrix = vector<vector<ll>>;
#define pb(x) push_back(x)
#define mp(x, y) make_pair(x, y)
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define rp(i, N) for (ll i = 0; i < (ll)N; i++)
#define repi(i, a, b) for (ll i = ll(a); i < ll(b); ++i)
template <class T> bool chmax(T &former, const T &b) {
  if (former < b) {
    former = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &former, const T &b) {
  if (b < former) {
    former = b;
    return true;
  }
  return false;
}
template <class T> T sqar(T x) { return x * x; } // sqrt(x)は平方根;
#define Sort(v)                                                                \\
  std::sort(v.begin(), v.end(),                                                \\
            std::greater<decltype(v[0])>()) // 降順でVをソート
#define p_queue(v) priority_queue<v, vector<v>, greater<v>>
template <class T> inline void princ(T x) { cout << x << " "; };
template <class T> inline void print(T x) { cout << x << "\\n"; };
template <class T> inline void Yes(T condition) {
  if (condition)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
}
template <class T> inline void YES(T condition) {
  if (condition)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;
}
///////////////////////////////////////////////////////////////////////////////////

void solve() {
  ll n;
  cin >> n;
  Vi a(n), b(n);
  rp(i, n) cin >> a[i];
  rp(i, n) cin >> b[i];
  Vi af(n + 1, 0), bf(n + 1, 0);
  vector<PLL> ap, bp;
  rp(i, n) {
    af[a[i]]++;
    bf[b[i]]++;
  }
  rp(i, n + 1) {
    if (af[i] + bf[i] > n) {
      print("No");
      return;
    }
  }
  rp(i, n + 1) {
    if (af[i] > 0)
      ap.pb(mp(af[i], i));
    if (bf[i] > 0)
      bp.pb(mp(bf[i], i));
  }
  Sort(ap);
  Sort(bp);
  vector<PLL> ans;
  ll bpos = 0;
  ll base = 0;
  repi(i, 0, (ll)ap.size()) {
    bpos = base;
    while (bp[base].fi == 0) {
      base++;
      bpos++;
    }
    while (bp[bpos].se == ap[i].se)
      bpos++;
    rp(j, ap[i].fi) {
      ans.pb(mp(ap[i].se, bp[bpos].se));
      bp[bpos].fi--;
      while (bp[bpos].fi == 0) {
        bpos++;
      }
      while (bp[bpos].se == ap[i].se)
        bpos++;
    }
  }
  print("Yes");
  sort(all(ans));
  rp(i, n) princ(ans[i].se);
  cout << endl;
  return;
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  std::cout << std::fixed << std::setprecision(30);
  solve();
  return 0;
}