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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> PP;
#define MOD 1000000007
// #define MOD 998244353
#define INF 2305843009213693951
// #define INF 810114514
#define PI 3.141592653589
#define setdouble setprecision
#define REP(i, n) for (ll i = 0; i < (n); ++i)
#define OREP(i, n) for (ll i = 1; i <= (n); ++i)
#define RREP(i, n) for (ll i = (n)-1; i >= 0; --i)
#define all1(i) begin(i), end(i)
#define GOODBYE                                                                \\
  do {                                                                         \\
    cout << "-1" << endl;                                                      \\
    return 0;                                                                  \\
  } while (false)
#define MM << " " <<
#define Endl endl
#define debug false
#define debug2 false

int main(void) {
  ll N, a, b, M = 0;
  vector<ll> A, B, As(214514, 0), Bs(214514, 0);

  cin >> N;
  REP(i, N) {
    cin >> a;
    M = max(M, a + 1);
    A.push_back(a);
    As[a]++;
  }
  REP(i, N) {
    cin >> b;
    M = max(M, b + 1);
    B.push_back(b);
    Bs[b]++;
  }

  REP(i, M) {
    if (As[i] + Bs[i] > N) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;

  reverse(B.begin(), B.end());

  // この時点でかぶってるのは一つの区間のみ
  ll T = -1;
  REP(i, N) {
    if (A[i] == B[i]) {
      T = A[i];
      break;
    }
  }

  // かぶりがないのでオワオワリ
  if (T == -1) {
    REP(i, N) {
      cout << B[i];
      if (i == N - 1) {
        cout << endl;
      } else {
        cout << " ";
      }
    }
    return 0;
  }

  // 以降かぶりがある
  // Bのかぶりを先頭にうつしちゃう
  vector<ll> C;
  REP(i, N) {
    if (B[i] == T && A[i] <= T) {
      C.push_back(B[i]);
      B[i] = INF;
    }
  }
  REP(i, N) {
    if (B[i] != INF) {
      C.push_back(B[i]);
    }
  }
  B = C;

  // REP(i,N){cout << B[i] << " ";}cout << Endl;

  ll ia = N - 1;
  REP(i, N) {
    if (A[i] == B[i]) {
      swap(A[ia], A[i]);
      ia--;
    }
  }

  vector<pair<ll, ll>> Ans;
  REP(i, N) {
    Ans.push_back({A[i], B[i]});
    if (A[i] == B[i]) {
      assert(false);
    }
  }
  sort(Ans.begin(), Ans.end());

  REP(i, N) {
    cout << Ans[i].second;
    if (i == N - 1) {
      cout << endl;
    } else {
      cout << " ";
    }
  }

  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> PP;
#define MOD 1000000007
// #define MOD 998244353
#define INF 2305843009213693951
// #define INF 810114514
#define PI 3.141592653589
#define setdouble setprecision
#define REP(i, n) for (ll i = 0; i < (n); ++i)
#define OREP(i, n) for (ll i = 1; i <= (n); ++i)
#define RREP(i, n) for (ll i = (n)-1; i >= 0; --i)
#define all1(i) begin(i), end(i)
#define GOODBYE                                                                \\
  do {                                                                         \\
    cout << "-1" << endl;                                                      \\
    return 0;                                                                  \\
  } while (false)
#define MM << " " <<
#define Endl endl
#define debug false
#define debug2 false

int main(void) {
  ll N, a, b, M = 0;
  vector<ll> A, B, As(214514, 0), Bs(214514, 0);

  cin >> N;
  REP(i, N) {
    cin >> a;
    M = max(M, a + 1);
    A.push_back(a);
    As[a]++;
  }
  REP(i, N) {
    cin >> b;
    M = max(M, b + 1);
    B.push_back(b);
    Bs[b]++;
  }

  REP(i, M) {
    if (As[i] + Bs[i] > N) {
      cout << "No" << endl;
      return 0;
    }
  }
  cout << "Yes" << endl;

  reverse(B.begin(), B.end());

  // この時点でかぶってるのは一つの区間のみ
  ll T = -1;
  REP(i, N) {
    if (A[i] == B[i]) {
      T = A[i];
      break;
    }
  }

  // かぶりがないのでオワオワリ
  if (T == -1) {
    REP(i, N) {
      cout << B[i];
      if (i == N - 1) {
        cout << endl;
      } else {
        cout << " ";
      }
    }
    return 0;
  }

  // 以降かぶりがある
  // Bのかぶりを先頭にうつしちゃう
  ll ib = 0;
  REP(i, N) {
    if (B[i] > T || A[i] == B[i]) {
      ib = i + 1;
    } else {
      break;
    }
  }
  reverse(B.begin(), B.begin() + ib);

  // REP(i,N){cout << B[i] << " ";}cout << Endl;

  ll ia = N - 1;
  REP(i, N) {
    if (A[i] == B[i]) {
      swap(A[ia], A[i]);
      ia--;
    }
  }

  vector<pair<ll, ll>> Ans;
  REP(i, N) {
    Ans.push_back({A[i], B[i]});
    if (A[i] == B[i]) {
      assert(false);
    }
  }
  sort(Ans.begin(), Ans.end());

  REP(i, N) {
    cout << Ans[i].second;
    if (i == N - 1) {
      cout << endl;
    } else {
      cout << " ";
    }
  }

  return 0;
}