Problem p02557 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define fi first
#define se second
typedef pair<ll, ll> P;
using VP = vector<P>;
using VVP = vector<VP>;
using VI = vector<ll>;
using VVI = vector<VI>;
using VVVI = vector<VVI>;
const int inf = 1e9 + 7;
const ll INF = 1LL << 61;
const ll mod = 1e9 + 7;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int i, j;
  ll n;
  cin >> n;
  ll a[n], b[n];
  for (i = 0; i < n; i++)
    cin >> a[i];
  for (i = 0; i < n; i++)
    cin >> b[i];
  map<ll, ll> ma, mb;
  for (i = 0; i < n; i++) {
    ma[a[i]]++;
    mb[b[i]]++;
  }
  for (auto it = ma.begin(); it != ma.end(); ++it) {
    if (it->second + mb[it->first] > n) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
  ll poa = 0;
  ll pob = 0;
  ll p = 0;
  for (auto it = ma.begin(); it != ma.end(); ++it) {
    poa += it->second;
    chmax(p, poa - pob);
    pob += mb[it->first];
  }
  VI v(n, -1);
  ll r = p;
  for (auto it = ma.begin(); it != ma.end(); ++it) {
    for (j = 0; j < mb[it->first]; j++) {
      v[r] = it->first;
      r++;
      r %= n;
    }
  }
  r = 0;
  for (auto it = mb.begin(); it != mb.end(); ++it) {
    if (ma[it->first] > 0)
      continue;
    for (j = 0; j < it->second; j++) {
      while (v[r] > 0)
        r++;
      v[r] = it->first;
    }
  }
  for (i = 0; i < n; i++) {
    cout << v[i];
    if (i != n - 1)
      cout << " ";
    else
      cout << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define fi first
#define se second
typedef pair<ll, ll> P;
using VP = vector<P>;
using VVP = vector<VP>;
using VI = vector<ll>;
using VVI = vector<VI>;
using VVVI = vector<VVI>;
const int inf = 1e9 + 7;
const ll INF = 1LL << 61;
const ll mod = 1e9 + 7;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int i, j;
  ll n;
  cin >> n;
  ll a[n], b[n];
  for (i = 0; i < n; i++)
    cin >> a[i];
  for (i = 0; i < n; i++)
    cin >> b[i];
  map<ll, ll> ma, mb;
  for (i = 0; i < n; i++) {
    ma[a[i]]++;
    mb[b[i]]++;
  }
  for (auto it = ma.begin(); it != ma.end(); ++it) {
    if (it->second + mb[it->first] > n) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
  ll poa = 0;
  ll pob = 0;
  ll p = 0;
  for (auto it = ma.begin(); it != ma.end(); ++it) {
    poa += it->second;
    chmax(p, poa - pob);
    pob += mb[it->first];
  }
  VI v(n, -1);
  ll r = p % n;
  for (auto it = ma.begin(); it != ma.end(); ++it) {
    for (j = 0; j < mb[it->first]; j++) {
      v[r] = it->first;
      r++;
      r %= n;
    }
  }
  r = 0;
  for (auto it = mb.begin(); it != mb.end(); ++it) {
    if (ma[it->first] > 0)
      continue;
    for (j = 0; j < it->second; j++) {
      while (v[r] > 0)
        r++;
      v[r] = it->first;
    }
  }
  for (i = 0; i < n; i++) {
    cout << v[i];
    if (i != n - 1)
      cout << " ";
    else
      cout << endl;
  }
}