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

#include <algorithm>
#include <cmath>
#include <cstring>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

int main() {
  ios_base::sync_with_stdio(false);
  int n;
  cin >> n;
  map<int, int> a;
  for (int i = 0; i < n; ++i) {
    int j;
    cin >> j;
    a[j]++;
  }

  map<int, int> b;
  for (int i = 0; i < n; ++i) {
    int j;
    cin >> j;
    b[j]++;
  }

  set<pair<int, int>, greater<pair<int, int>>> s;
  for (auto &it : a) {
    if (b.count(it.first) > 0) {
      int cnt = it.second;
      s.insert({cnt + b[it.first], it.first});
    }
  }

  vector<pair<int, int>> res;
  while (!s.empty()) {
    auto it = begin(s);
    int cnt = it->first;
    int num = it->second;
    if (cnt > n) {
      cout << "No\\n";
      return 0;
    }
    if (s.size() > 1) {
      auto it2 = ++begin(s);
      int num2 = it2->second;
      int cnt2 = it2->first;
      a[num]--;
      b[num2]--;
      res.emplace_back(num, num2);
      s.erase(it);
      s.erase(it2);
      if (a[num] != 0) {
        s.insert({cnt - 1, num});
      } else {
        a.erase(num);
      }

      if (b[num2] != 0) {
        s.insert({cnt2 - 1, num2});
      } else {
        b.erase(num2);
      }
    } else {
      auto it2 = begin(b);
      if (it2->first == num) {
        ++it2;
      }
      int num2 = it2->first;
      a[num]--;
      b[num2]--;
      res.emplace_back(num, num2);
      s.erase(it);
      if (a[num] != 0) {
        s.insert({cnt - 1, num});
      } else {
        a.erase(num);
      }

      if (b[num2] == 0) {
        b.erase(num2);
      }
    }
  }

  int c = res.size();
  for (auto &it : a) {
    for (int i = 0; i < it.second; ++i) {
      res.emplace_back(it.first, 0);
    }
  }

  for (auto &it : b) {
    for (int i = 0; i < it.second; ++i) {
      res[c].second = it.first;
      ++c;
    }
  }

  sort(begin(res), end(res));
  cout << "Yes\\n";
  for (int i = 0; i < n; ++i) {
    cout << res[i].second << ' ';
  }
  cout << endl;

  return 0;
}


Error: WA

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstring>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

int main() {
  ios_base::sync_with_stdio(false);
  int n;
  cin >> n;
  map<int, int> a;
  for (int i = 0; i < n; ++i) {
    int j;
    cin >> j;
    a[j]++;
  }

  map<int, int> b;
  for (int i = 0; i < n; ++i) {
    int j;
    cin >> j;
    b[j]++;
  }

  set<pair<int, int>, greater<pair<int, int>>> s;
  for (auto &it : a) {
    if (b.count(it.first) > 0) {
      int cnt = it.second;
      s.insert({cnt + b[it.first], it.first});
    }
  }

  vector<pair<int, int>> res;
  while (!s.empty()) {
    auto it = begin(s);
    int cnt = it->first;
    int num = it->second;
    if (cnt > n) {
      cout << "No\\n";
      return 0;
    }
    if (s.size() > 1) {
      auto it2 = ++begin(s);
      int num2 = it2->second;
      int cnt2 = it2->first;
      a[num]--;
      b[num2]--;
      res.emplace_back(num, num2);
      s.erase(it);
      s.erase(it2);
      if (a[num] != 0) {
        s.insert({cnt - 1, num});
      } else {
        a.erase(num);
      }

      if (b[num2] != 0) {
        s.insert({cnt2 - 1, num2});
      } else {
        b.erase(num2);
      }
    } else {
      auto it2 = begin(b);
      if (it2->first == num) {
        ++it2;
      }
      int num2 = it2->second;
      int cnt2 = it2->first;
      a[num]--;
      b[num2]--;
      res.emplace_back(num, num2);
      s.erase(it);
      if (a[num] != 0) {
        s.insert({cnt - 1, num});
      } else {
        a.erase(num);
      }

      if (b[num2] == 0) {
        b.erase(num2);
      }
    }
  }

  int c = res.size();
  for (auto &it : a) {
    for (int i = 0; i < it.second; ++i) {
      res.emplace_back(it.first, 0);
    }
  }

  for (auto &it : b) {
    for (int i = 0; i < it.second; ++i) {
      res[c].second = it.first;
      ++c;
    }
  }

  sort(begin(res), end(res));
  cout << "Yes\\n";
  for (int i = 0; i < n; ++i) {
    cout << res[i].second << ' ';
  }
  cout << endl;

  return 0;
}