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

// ヘッダー
#include <bits/stdc++.h>
using namespace std;

// 型定義
typedef long long ll;

// 定数
const ll INF = 1e+18;
const int MOD = 1e+9 + 7;

// デバッグ
#define CHOOSE(a) CHOOSE2 a
#define CHOOSE2(a0, a1, a2, a3, a4, x, ...) x
#define debug1(x1) cout << #x1 << ": " << x1 << endl
#define debug2(x1, x2)                                                         \\
  cout << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << endl
#define debug3(x1, x2, x3)                                                     \\
  cout << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": "      \\
       << x3 << endl
#define debug4(x1, x2, x3, x4)                                                 \\
  cout << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": "      \\
       << x3 << ", " #x4 << ": " << x4 << endl
#define debug5(x1, x2, x3, x4, x5)                                             \\
  cout << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": "      \\
       << x3 << ", " #x4 << ": " << x4 << ", " #x5 << ": " << x5 << endl
#ifdef _DEBUG
#define debug(...)                                                             \\
  CHOOSE((__VA_ARGS__, debug5, debug4, debug3, debug2, debug1, ~))(__VA_ARGS__)
#else
#define debug(...)
#endif

// REPマクロ
#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define REPD(i, n) for (ll i = n - 1; i >= 0; i--)
#define REP2(i, a, b) for (ll i = a; i < (ll)(b); i++)
#define REPD2(i, a, b) for (ll i = a; i > (ll)(b); i--)

// 多次元 vector 生成
template <class T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template <class T, class... Ts> auto make_vec(size_t a, Ts... ts) {
  return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}

// vectorの扱い
#define ALL(x) (x).begin(), (x).end() // sortなどの引数省略
#define SIZE(x) ((ll)(x).size())      // size
#define MAX(x) *max_element(ALL(x))   // 最大値
#define MIN(x) *min_element(ALL(x))   // 最小値

// 省略
using vi = vector<int>;
using vii = vector<vector<int>>;
using vl = vector<ll>;
using vll = vector<vector<ll>>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

int main() {
  ll N;
  cin >> N;
  vl A(N), B(N);
  map<ll, ll> mp, mpA, mpB;

  REP(i, N) {
    cin >> A[i];
    mp[A[i]]++;
    mpA[A[i]]++;
  }

  REP(i, N) {
    cin >> B[i];
    mp[B[i]]++;
    mpB[B[i]]++;
  }

  priority_queue<pll> que; // first:登場数の和 second:中身
  ll saidai = 0;
  REP2(i, 1, N + 1) {
    if (mp.count(i))
      saidai = max(saidai, mp[i]);
    if (mpB.count(i) && mpA.count(i)) {
      que.push(make_pair(mp[i], i));
    }
  }

  if (saidai > N) {
    cout << "No" << endl;
    return 0;
  }

  priority_queue<pll, vector<pll>, greater<pll>> pair_AB;
  queue<ll> listA;
  while (!que.empty()) {
    pll tmp = que.top();
    que.pop();
    ll wa1 = tmp.first;
    ll val1 = tmp.second;

    ll nokori1B = mpB[val1];

    if (!que.empty()) {
      tmp = que.top();
      que.pop();
      ll wa2 = tmp.first;
      ll val2 = tmp.second;

      ll nokori2A = mpA[val2];

      // debug2(val1,mpB[val1]);
      // debug2(val2,mpB[val2]);
      pair_AB.push(make_pair(val2, val1));
      wa1--;
      wa2--;
      mpA[val2]--;
      mpB[val1]--;

      nokori1B = mpB[val1];
      nokori2A = mpA[val2];

      if (nokori1B > 0) {
        que.push(make_pair(wa1, val1));
      }
      if (nokori2A > 0) {
        que.push(make_pair(wa2, val2));
      }

    } else {
      // Aの残りの選択肢を追加
      for (auto itr = mpA.begin(); itr != mpA.end(); itr++) {
        if (itr->first == val1)
          continue;
        REP(j, itr->second) { listA.push(itr->first); }
      }

      REP(j, nokori1B) {
        ll val2 = listA.front();
        listA.pop();
        pair_AB.push(make_pair(val2, val1));
        mpA[val2]--;
        mpB[val1]--;
      }

      REP(j, mpA[val1]) { listA.push(val1); }
    }
  }

  if (listA.empty()) {
    // Aの残りの選択肢を追加
    for (auto itr = mpA.begin(); itr != mpA.end(); itr++) {
      REP(j, itr->second) { listA.push(itr->first); }
    }
  }

  // 最後にBとAのペア作成
  for (auto itr = mpB.begin(); itr != mpB.end(); itr++) {
    REP(j, itr->second) {
      listA.push(itr->first);
      ll val2 = listA.front();
      listA.pop();
      pair_AB.push(make_pair(val2, itr->first));
    }
  }

  cout << "Yes" << endl;
  while (!pair_AB.empty()) {
    pll tmp = pair_AB.top();
    pair_AB.pop();
    cout << tmp.second;
    if (pair_AB.empty()) {
      cout << endl;
    } else {
      cout << " ";
    }
  }
}

Error: 0

Fixed submission:

// ヘッダー
#include <bits/stdc++.h>
using namespace std;

// 型定義
typedef long long ll;

// 定数
const ll INF = 1e+18;
const int MOD = 1e+9 + 7;

// デバッグ
#define CHOOSE(a) CHOOSE2 a
#define CHOOSE2(a0, a1, a2, a3, a4, x, ...) x
#define debug1(x1) cout << #x1 << ": " << x1 << endl
#define debug2(x1, x2)                                                         \\
  cout << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << endl
#define debug3(x1, x2, x3)                                                     \\
  cout << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": "      \\
       << x3 << endl
#define debug4(x1, x2, x3, x4)                                                 \\
  cout << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": "      \\
       << x3 << ", " #x4 << ": " << x4 << endl
#define debug5(x1, x2, x3, x4, x5)                                             \\
  cout << #x1 << ": " << x1 << ", " #x2 << ": " << x2 << ", " #x3 << ": "      \\
       << x3 << ", " #x4 << ": " << x4 << ", " #x5 << ": " << x5 << endl
#ifdef _DEBUG
#define debug(...)                                                             \\
  CHOOSE((__VA_ARGS__, debug5, debug4, debug3, debug2, debug1, ~))(__VA_ARGS__)
#else
#define debug(...)
#endif

// REPマクロ
#define REP(i, n) for (ll i = 0; i < (ll)(n); i++)
#define REPD(i, n) for (ll i = n - 1; i >= 0; i--)
#define REP2(i, a, b) for (ll i = a; i < (ll)(b); i++)
#define REPD2(i, a, b) for (ll i = a; i > (ll)(b); i--)

// 多次元 vector 生成
template <class T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template <class T, class... Ts> auto make_vec(size_t a, Ts... ts) {
  return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}

// vectorの扱い
#define ALL(x) (x).begin(), (x).end() // sortなどの引数省略
#define SIZE(x) ((ll)(x).size())      // size
#define MAX(x) *max_element(ALL(x))   // 最大値
#define MIN(x) *min_element(ALL(x))   // 最小値

// 省略
using vi = vector<int>;
using vii = vector<vector<int>>;
using vl = vector<ll>;
using vll = vector<vector<ll>>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

int main() {
  ll N;
  cin >> N;
  vl A(N), B(N);
  map<ll, ll> mpA, mpB;
  priority_queue<pll, vector<pll>, greater<pll>> pai;
  REP(i, N) {
    cin >> A[i];
    mpA[A[i]]++;
  }

  REP(i, N) {
    cin >> B[i];
    mpB[B[i]]++;
  }

  ll sum_AB = 0;
  priority_queue<tuple<ll, ll, ll>> que; // 重なり度、Bの数、key
  REP2(i, 1, N + 1) {
    if (mpA.count(i) && mpB.count(i)) {
      ll kasanari = min(mpA[i], mpB[i]);
      sum_AB = max(sum_AB, mpA[i] + mpB[i]);
      que.push(make_tuple(mpA[i] + mpB[i], kasanari, i));
    }
  }

  bool ok = true;
  ll nokori = N;

  priority_queue<ll> queA;

  if (sum_AB > N) {
    cout << "No" << endl;
    return 0;
  }

  // 重なりの処理
  while (!que.empty()) {
    tuple<ll, ll, ll> b1 = que.top();
    ll kasanari1 = get<1>(b1);
    ll count1 = get<0>(b1);
    ll val1 = get<2>(b1);

    que.pop();
    if (!que.empty()) {
      tuple<ll, ll, ll> b2 = que.top();
      ll kasanari2 = get<1>(b2);
      ll count2 = get<0>(b2);
      ll val2 = get<2>(b2);
      que.pop();
      mpA[val1]--;
      mpB[val1]--;
      mpA[val2]--;
      mpB[val2]--;
      kasanari1--;
      count1--;
      kasanari2--;
      count2--;
      pai.push(make_pair(val1, val2));
      pai.push(make_pair(val2, val1));

      nokori--;
      if (kasanari1 != 0) {
        que.push(make_tuple(count1, kasanari1, val1));
      }
      if (kasanari2 != 0) {
        que.push(make_tuple(count2, kasanari2, val2));
      }
    } else {

      ll st = 0;
      for (auto itr = mpA.begin(); itr != mpA.end(); itr++) {
        ll val = itr->first;
        ll cnt = itr->second;
        if (val == val1) {
          st++;
          continue;
        }
        while (cnt > 0) {
          queA.push(val);
          cnt--;
        }
      }

      // debug1(kasanari1);
      // debug1(queA.size());
      REP(j, kasanari1) {
        ll val = queA.top();
        mpB[val1]--;
        mpA[val]--;
        pai.push(make_pair(val, val1));
        queA.pop();
      }
      REP(j, st) {
        queA.push(val1);
        mpA[val1]--;
      }
    }
  }

  // 残りの処理
  for (auto itr = mpA.begin(); itr != mpA.end(); itr++) {
    ll val = itr->first;
    ll cnt = itr->second;
    while (cnt > 0) {
      queA.push(val);
      cnt--;
    }
  }

  for (auto itr = mpB.begin(); itr != mpB.end(); itr++) {
    ll val1 = itr->first;
    ll count1 = itr->second;
    if (count1 == 0)
      continue;
    REP(j, count1) {
      ll val = queA.top();
      pai.push(make_pair(val, val1));
      // ans[pos[val]]=val1;
      queA.pop();
      // pos[val]++;
    }
  }

  cout << "Yes" << endl;
  while (!pai.empty()) {
    pll pe = pai.top();
    ll va = pe.second;
    pai.pop();
    cout << va;
    if (!pai.empty())
      cout << " ";
    else
      cout << endl;
  }
}