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

//{{{
#pragma GCC diagnostic ignored "-Wunknown-pragmas"
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef vector<int> vi;
typedef pair<int, int> pii;
#define sz(x) (int)((x).size())
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define clr(a, b) memset(a, b, sizeof(a))
#ifdef LOCAL
#include "prettyprint.hpp"
#endif
// clang-format off
void _print() { cerr << "]\\033[0m\\n"; }
template <typename T> void __print(T x) { cerr << x; }
template <typename T, typename... V> void _print(T t, V... v) { __print(t); if (sizeof...(v)) cerr << ", "; _print(v...); }
#ifdef LOCAL
#define debug(x...) cerr << "\\033[1;34m[" << #x << "] = \\033[1;32m["; _print(x)
#define debug_arr(x...) cerr << "\\033[1;34m[" << #x << "] = \\033[1;32m" << (x) << "\\033[0m\\n"
#else
#define debug(x...)
#define debug_arr(x...)
#endif
// clang-format on
//}}}

const int N = 2e5 + 10;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;

int n;
int a[N], b[N];

int main() {
#ifdef LOCAL
  freopen("in", "r", stdin);
  // freopen("out", "w", stdout);
#endif

  while (cin >> n) {
    for (int i = 1; i <= n; ++i)
      cin >> a[i];
    for (int i = 1; i <= n; ++i)
      cin >> b[i];

    reverse(b + 1, b + 1 + n);

    int c = -1;
    int l = -1, r = -1;
    for (int i = 1; i <= n; i++) {
      if (a[i] == b[i]) {
        c = a[i];
        l = i;
        break;
      }
    }

    for (int i = n; i >= 1; i--) {
      if (a[i] == b[i] && a[i] == c) {
        r = i;
        break;
      }
    }

    if (l != -1) {
      for (int i = 1; i <= n; i++) {
        if (l > r)
          break;
        if (l <= i && i <= r)
          continue;
        if (a[i] == c || b[i] == c)
          continue;
        swap(b[i], b[l]);
        l++;
      }
    }

    if (l != -1 && l <= r) {
      // assert(0);
      cout << "No" << endl;
    } else {
      cout << "Yes" << endl;
      for (int i = 1; i <= n; ++i) {
        assert(b[i] != a[i]);
        cout << b[i] << " \\n"[i == n];
      }
    }
  }

  return 0;
}

Error: WA

Fixed submission:

//{{{
#pragma GCC diagnostic ignored "-Wunknown-pragmas"
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef vector<int> vi;
typedef pair<int, int> pii;
#define sz(x) (int)((x).size())
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define clr(a, b) memset(a, b, sizeof(a))
#ifdef LOCAL
#include "prettyprint.hpp"
#endif
// clang-format off
void _print() { cerr << "]\\033[0m\\n"; }
template <typename T> void __print(T x) { cerr << x; }
template <typename T, typename... V> void _print(T t, V... v) { __print(t); if (sizeof...(v)) cerr << ", "; _print(v...); }
#ifdef LOCAL
#define debug(x...) cerr << "\\033[1;34m[" << #x << "] = \\033[1;32m["; _print(x)
#define debug_arr(x...) cerr << "\\033[1;34m[" << #x << "] = \\033[1;32m" << (x) << "\\033[0m\\n"
#else
#define debug(x...)
#define debug_arr(x...)
#endif
// clang-format on
//}}}

const int N = 2e5 + 10;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;

int n;
int a[N], b[N];

int main() {
#ifdef LOCAL
  freopen("in", "r", stdin);
  // freopen("out", "w", stdout);
#endif

  while (cin >> n) {
    for (int i = 1; i <= n; ++i)
      cin >> a[i];

    for (int i = 1; i <= n; ++i) {
      cin >> b[i];
    }

    reverse(b + 1, b + 1 + n);

    int c = -1;
    int l = -1, r = -1;
    for (int i = 1; i <= n; i++) {
      if (a[i] == b[i]) {
        c = a[i];
        l = i;
      }
    }

    for (int i = n; i >= 1; i--) {
      if (a[i] == b[i] && a[i] == c) {
        r = i;
        break;
      }
    }

    if (l != -1) {
      for (int i = 1; i <= n; i++) {
        if (l > r)
          break;
        if (l <= i && i <= r)
          continue;
        if (a[i] == c || b[i] == c)
          continue;
        swap(b[i], b[l]);
        l++;
      }
    }

    if (l != -1 && l <= r) {
      // assert(0);
      cout << "No" << endl;
    } else {
      cout << "Yes" << endl;
      for (int i = 1; i <= n; ++i) {
        assert(b[i] != a[i]);
        cout << b[i] << " \\n"[i == n];
      }
    }
  }

  return 0;
}