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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, srt, end) for (long long i = (srt); i < (long long)(end); i++)
#define VL vector<ll>
#define VS vector<string>
#define VB vector<bool>
#define VP vector<pair<ll, ll>>
#define VVL vector<vector<ll>>
#define VVP vector<vector<pair<ll, ll>>>
#define PL pair<ll, ll>
#define ALL(v) (v).begin(), (v).end()
ll d1[4] = {1, -1, 0, 0};
ll d2[4] = {0, 0, 1, -1};

int main() {
  ll n;
  cin >> n;
  VL a(n), b(n);
  vector<ll> ma(n, 0);
  rep(i, 0, n) cin >> a[i];
  rep(i, 0, n) cin >> b[i];
  rep(i, 0, n) { ma[a[i]]++; }
  VP cp(n);
  rep(i, 0, n) {
    cp[i].first = a[i];
    cp[i].second = i;
  }
  sort(a.begin(), a.end(), [&](ll l, ll r) {
    if (ma[l] > ma[r])
      return true;
    else if (ma[l] == ma[r] && l < r)
      return true;
    else
      return false;
  });
  sort(cp.begin(), cp.end(), [&](PL lp, PL rp) {
    ll l = lp.first;
    ll r = rp.first;
    if (ma[l] > ma[r])
      return true;
    else if (ma[l] == ma[r] && l < r)
      return true;
    else
      return false;
  });
  sort(b.begin(), b.end(), [&](ll l, ll r) {
    if (ma[l] > ma[r])
      return true;
    else if (ma[l] == ma[r] && l < r)
      return true;
    else
      return false;
  });

  // rep(i, 0, n) cerr << cp[i].first << (i == n-1 ? "\\n" : " ");
  // rep(i, 0, n) cerr << a[i] << (i == n-1 ? "\\n" : " ");
  // cerr << "a" << endl;

  VL ans(n);

  ll srt = -1;
  rep(i, 0, n) {
    if (a[0] != b[i]) {
      srt = i;
      break;
    }
  }
  if (srt == -1) {
    cout << "No" << endl;
    return 0;
  }
  rep(i, 0, n) ans[i] = b[(i + srt) % n];
  rep(i, 0, n) {
    if (a[i] == ans[i]) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
  // rep(i, 0, n) cerr << ans[i] << (i == n-1 ? "\\n" : " ");
  VL true_ans(n);
  rep(i, 0, n) true_ans[cp[i].second] = ans[i];
  rep(i, 0, n) cout << true_ans[i] << (i == n - 1 ? "\\n" : " ");
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, srt, end) for (long long i = (srt); i < (long long)(end); i++)
#define VL vector<ll>
#define VS vector<string>
#define VB vector<bool>
#define VP vector<pair<ll, ll>>
#define VVL vector<vector<ll>>
#define VVP vector<vector<pair<ll, ll>>>
#define PL pair<ll, ll>
#define ALL(v) (v).begin(), (v).end()
ll d1[4] = {1, -1, 0, 0};
ll d2[4] = {0, 0, 1, -1};

int main() {
  ll n;
  cin >> n;
  VL a(n), b(n);
  map<ll, ll> ma;
  rep(i, 0, n) cin >> a[i];
  rep(i, 0, n) cin >> b[i];
  rep(i, 0, n) { ma[a[i]]++; }
  VP cp(n);
  rep(i, 0, n) {
    cp[i].first = a[i];
    cp[i].second = i;
  }
  sort(a.begin(), a.end(), [&](ll l, ll r) {
    if (ma[l] > ma[r])
      return true;
    else if (l < r)
      return true;
    else
      return false;
  });
  sort(cp.begin(), cp.end(), [&](PL lp, PL rp) {
    ll l = lp.first;
    ll r = rp.first;
    if (ma[l] > ma[r])
      return true;
    else if (ma[l] == ma[r] && l < r)
      return true;
    else
      return false;
  });
  sort(b.begin(), b.end(), [&](ll l, ll r) {
    if (ma[l] > ma[r])
      return true;
    else if (ma[l] == ma[r] && l < r)
      return true;
    else
      return false;
  });

  // rep(i, 0, n) cerr << cp[i].first << (i == n-1 ? "\\n" : " ");
  // rep(i, 0, n) cerr << a[i] << (i == n-1 ? "\\n" : " ");
  // cerr << "a" << endl;

  VL ans(n);

  ll srt = -1;
  rep(i, 0, n) {
    if (a[0] != b[i]) {
      srt = i;
      break;
    }
  }
  if (srt == -1) {
    cout << "No" << endl;
    return 0;
  }
  rep(i, 0, n) ans[i] = b[(i + srt) % n];
  rep(i, 0, n) {
    if (a[i] == ans[i]) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;
  // rep(i, 0, n) cerr << ans[i] << (i == n-1 ? "\\n" : " ");
  VL true_ans(n);
  rep(i, 0, n) true_ans[cp[i].second] = ans[i];
  rep(i, 0, n) cout << true_ans[i] << (i == n - 1 ? "\\n" : " ");
  return 0;
}