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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(ri, n) for (int ri = (int)(n - 1); ri >= 0; ri--)
#define rep2(i, x, n) for (int i = (int)(x); i < (int)(n); i++)
#define rrep2(ri, x, n) for (int ri = (int)(n - 1); ri >= (int)(x); ri--)
#define repit(itr, x) for (auto itr = x.begin(); itr != x.end(); itr++)
#define rrepit(ritr, x) for (auto ritr = x.rbegin(); ritr != x.rend(); ritr++)
#define ALL(x) x.begin(), x.end()
using ll = long long;
using namespace std;

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  map<int, int> mpa, mpb;
  int enf = 0;
  rep(i, n) {
    cin >> a.at(i);
    mpa[a.at(i)]++;
    if (mpa[a.at(i)] > n / 2)
      enf = a.at(i);
  }
  vector<int> b(n);
  int enfb = 0;
  rep(i, n) {
    cin >> b.at(i);
    mpb[b.at(i)]++;
    if (mpb[b.at(i)] > n / 2)
      enfb = b.at(i);
  }
  if (enfb > enf) {
    if (mpa[enfb] > n - mpb[enfb]) {
      cout << "No" << endl;
      return 0;
    }
  } else {
    if (mpb.count(enf) > 0 && mpb[enf] > n - mpa[enf]) {
      cout << "No" << endl;
      return 0;
    }
  }
  reverse(ALL(b));
  int l = 0, r = 0, now = 0;
  rep(i, n) {
    if (now != a.at(i)) {
      l = 0, r = n - 1;
      now = a.at(i);
    }
    if (a.at(i) == b.at(i)) {
      bool f = false;
    BK:
      if (b.at(l) != a.at(i) && b.at(i) != a.at(l)) {
        swap(b.at(i), b.at(l));
        l++;
      } else if (b.at(r) != a.at(i) && b.at(i) != a.at(r)) {
        swap(b.at(i), b.at(r));
        r--;
      } else {
        if (r < l) {
          if (f == true) {
            cout << "No" << endl;
            return 0;
          }
          f = true;
          l = 0, r = n - 1;
        } else {
          l++;
          r--;
        }
        goto BK;
      }
    }
  }
  cout << "Yes" << endl;
  rep(i, n) {
    if (i != 0)
      cout << " ";
    cout << b.at(i);
  }
  cout << endl;

  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(ri, n) for (int ri = (int)(n - 1); ri >= 0; ri--)
#define rep2(i, x, n) for (int i = (int)(x); i < (int)(n); i++)
#define rrep2(ri, x, n) for (int ri = (int)(n - 1); ri >= (int)(x); ri--)
#define repit(itr, x) for (auto itr = x.begin(); itr != x.end(); itr++)
#define rrepit(ritr, x) for (auto ritr = x.rbegin(); ritr != x.rend(); ritr++)
#define ALL(x) x.begin(), x.end()
using ll = long long;
using namespace std;

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  map<int, int> mpa, mpb;
  int enf = 0;
  rep(i, n) {
    cin >> a.at(i);
    mpa[a.at(i)]++;
    if (mpa[a.at(i)] > n / 2)
      enf = a.at(i);
  }
  vector<int> b(n);
  int enfb = 0;
  rep(i, n) {
    cin >> b.at(i);
    mpb[b.at(i)]++;
    if (mpb[b.at(i)] > n / 2)
      enfb = b.at(i);
  }
  if (enfb > enf) {
    if (mpa[enfb] > n - mpb[enfb]) {
      cout << "No" << endl;
      return 0;
    }
  } else {
    if (mpb.count(enf) > 0 && mpb[enf] > n - mpa[enf]) {
      cout << "No" << endl;
      return 0;
    }
  }
  reverse(ALL(b));
  int l = 0, r = 0, now = 0;
  rep(i, n) {
    if (now != a.at(i)) {
      l = 0, r = n - 1;
      now = a.at(i);
    }
    if (a.at(i) == b.at(i)) {
      if (b.at(l) != a.at(i) && b.at(i) != a.at(l)) {
        swap(b.at(i), b.at(l));
        l++;
      } else if (b.at(r) != a.at(i) && b.at(i) != a.at(r)) {
        swap(b.at(i), b.at(r));
        r--;
      } else
        return 1;
    }
  }
  cout << "Yes" << endl;
  rep(i, n) {
    if (i != 0)
      cout << " ";
    cout << b.at(i);
  }
  cout << endl;

  return 0;
}