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

#include <bits/stdc++.h>

/*#pragma GCC optimize("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")*/
#define mp make_pair
#define pb push_back
#define pll pair<LL, LL>
#define pii pair<int, int>
#define y second
#define x first
#define LL long long
#define sqr(x) ((x) * (x))
#define pi acosl(-1)
#define MEM(x) memset(x, 0, sizeof(x))
#define MEMS(x) memset(x, -1, sizeof(x))
using namespace std;

void solve() {
  int n;
  scanf("%d", &n);
  int a[200005];
  int b[200005];
  for (int i = 0; i < n; i++)
    scanf("%d", &a[i]);
  for (int i = 0; i < n; i++)
    scanf("%d", &b[i]);
  map<int, int> m2;
  map<int, int> m1;
  for (int i = 0; i < n; i++)
    m1[a[i]]++, m2[b[i]]++, m2[a[i]]++;
  set<pii> s;
  for (auto it : m2) {
    if (it.y != m1[it.x])
      s.insert(mp(it.y, it.x));
  }
  for (auto it : m2) {
    if (it.y > n) {
      printf("No\\n");
      return;
    }
  }
  printf("Yes\\n");

  vector<int> v;
  for (int i = 0; i < n; i++) {
    pii p = *s.rbegin();
    s.erase(p);
    if (p.y == a[i]) {
      pii pp = *s.rbegin();
      s.erase(pp);
      s.insert(p);
      v.pb(pp.y);
      m2[pp.y]--;
      if (m2[pp.y] != m1[pp.y]) {
        s.insert(mp(m2[pp.y], pp.y));
      }
    } else {
      p.x--;
      v.pb(p.y);
      m2[p.y]--;
      if (m2[p.y] != m1[p.y]) {
        s.insert(mp(m2[p.y], p.y));
      }
    }
    s.erase(mp(m2[a[i]], a[i]));
    m2[a[i]]--;
    m1[a[i]]--;
    s.insert(mp(m2[a[i]], a[i]));
    /*  for(auto it:s){
          printf("%d %d\\n",it.x,it.y);
      }
      for(auto it:v)printf("%d ",it);
      printf("\\n");*/
  }
  for (auto it : v)
    printf("%d ", it);
  printf("\\n");
}
int main() {
  int t = 1;
  int T = 1;
  while (t--) {
    solve();
  }
}
/*
100
5
5 4
4 2
4 2 3
5 4 2 3 1
*/


Error: WA

Fixed submission:

#include <bits/stdc++.h>

/*#pragma GCC optimize("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")*/
#define mp make_pair
#define pb push_back
#define pll pair<LL, LL>
#define pii pair<int, int>
#define y second
#define x first
#define LL long long
#define sqr(x) ((x) * (x))
#define pi acosl(-1)
#define MEM(x) memset(x, 0, sizeof(x))
#define MEMS(x) memset(x, -1, sizeof(x))
using namespace std;

void solve() {
  int n;
  scanf("%d", &n);
  int a[200005];
  int b[200005];
  for (int i = 0; i < n; i++)
    scanf("%d", &a[i]);
  for (int i = 0; i < n; i++)
    scanf("%d", &b[i]);
  map<int, int> m2;
  map<int, int> m1;
  for (int i = 0; i < n; i++)
    m1[a[i]]++, m2[b[i]]++, m2[a[i]]++;
  set<pii> s;
  for (auto it : m2) {
    if (it.y != m1[it.x])
      s.insert(mp(it.y, it.x));
  }
  for (auto it : m2) {
    if (it.y > n) {
      printf("No\\n");
      return;
    }
  }
  printf("Yes\\n");

  vector<int> v;
  for (int i = 0; i < n; i++) {
    pii p = *s.rbegin();
    s.erase(p);
    if (p.y == a[i]) {
      pii pp = *s.rbegin();
      s.erase(pp);
      s.insert(p);
      v.pb(pp.y);
      m2[pp.y]--;
      if (m2[pp.y] != m1[pp.y]) {
        s.insert(mp(m2[pp.y], pp.y));
      }
    } else {
      p.x--;
      v.pb(p.y);
      m2[p.y]--;
      if (m2[p.y] != m1[p.y]) {
        s.insert(mp(m2[p.y], p.y));
      }
    }
    s.erase(mp(m2[a[i]], a[i]));
    m2[a[i]]--;
    s.insert(mp(m2[a[i]], a[i]));
  }
  for (auto it : v)
    printf("%d ", it);
  printf("\\n");
}
int main() {
  int t = 1;
  int T = 1;
  while (t--) {
    solve();
  }
}
/*
100
5
5 4
4 2
4 2 3
5 4 2 3 1
*/