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

// Code By CXY07
#include <bits/stdc++.h>
using namespace std;

// #define FILE
// #define int long long
// #define LL long long
#define pii pair<int, int>
#define mp make_pair
#define pb push_back
#define fst first
#define scd second
#define inv(x) ksm((x), mod - 2)
#define lowbit(x) ((x) & (-(x)))

const int MAXN = 2e5 + 10;
const int INF = 2e9;
const double PI = acos(-1);
const double eps = 1e-6;
// const int mod = 1e9 + 7;
// const int mod = 998244353;
// const int G = 3;

int n, cnt, L, R, v, pos;
int a[MAXN], b[MAXN];

template <typename T> inline void read(T &a) {
  a = 0;
  char c = getchar();
  int f = 1;
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    a = a * 10 + (c ^ 48);
    c = getchar();
  }
  a *= f;
}

signed main() {
#ifdef FILE
  freopen(".in", "r", stdin);
  freopen(".out", "w", stdout);
#endif
  read(n);
  for (int i = 1; i <= n; ++i)
    read(a[i]);
  for (int i = 1; i <= n; ++i) {
    read(b[i]);
    b[i] *= -1;
  }
  sort(b + 1, b + n + 1);
  for (int i = 1; i <= n; ++i)
    b[i] *= -1;
  for (int i = 1; i <= n; ++i) {
    if (a[i] == b[i]) {
      if (!L)
        L = i;
      R = i;
      cnt++;
    }
  }
  if (!cnt) {
    puts("Yes");
    for (int i = 1; i <= n; ++i)
      printf("%d ", b[i]);
    return 0;
  }
  assert(b[L] == b[R]);
  int ban = b[L];
  pos = L;
  for (int i = 1; i < L; ++i) {
    if (b[i] == ban || a[i] == ban)
      continue;
    if (pos > R)
      break;
    swap(b[i], b[pos++]);
    if (pos > R)
      break;
  }
  for (int i = R + 1; i <= n; ++i) {
    if (b[i] == ban || a[i] == ban)
      continue;
    if (pos > R)
      break;
    swap(b[i], b[pos++]);
    if (pos > R)
      break;
  }
  bool flag = true;
  for (int i = 1; i <= n; ++i)
    if (a[i] == b[i]) {
      flag = false;
      break;
    }
  if (flag) {
    puts("Yes");
    for (int i = 1; i <= n; ++i)
      printf("%d ", b[i]);
    return 0;
  }
  puts("No");
  return 0;
}


Error: WA

Fixed submission:

// Code By CXY07
#include <bits/stdc++.h>
using namespace std;

// #define FILE
// #define int long long
// #define LL long long
#define pii pair<int, int>
#define mp make_pair
#define pb push_back
#define fst first
#define scd second
#define inv(x) ksm((x), mod - 2)
#define lowbit(x) ((x) & (-(x)))

const int MAXN = 2e5 + 10;
const int INF = 2e9;
const double PI = acos(-1);
const double eps = 1e-6;
// const int mod = 1e9 + 7;
// const int mod = 998244353;
// const int G = 3;

int n, cnt, L, R, v, pos;
int a[MAXN], b[MAXN];

template <typename T> inline void read(T &a) {
  a = 0;
  char c = getchar();
  int f = 1;
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    a = a * 10 + (c ^ 48);
    c = getchar();
  }
  a *= f;
}

signed main() {
#ifdef FILE
  freopen(".in", "r", stdin);
  freopen(".out", "w", stdout);
#endif
  read(n);
  for (int i = 1; i <= n; ++i)
    read(a[i]);
  for (int i = 1; i <= n; ++i) {
    read(b[i]);
    b[i] *= -1;
  }
  sort(b + 1, b + n + 1);
  for (int i = 1; i <= n; ++i)
    b[i] *= -1;
  for (int i = 1; i <= n; ++i) {
    if (a[i] == b[i]) {
      if (!L)
        L = i;
      R = i;
      cnt++;
    }
  }
  if (!cnt) {
    puts("Yes");
    for (int i = 1; i <= n; ++i)
      printf("%d ", b[i]);
    return 0;
  }
  assert(b[L] == b[R]);
  int ban = b[L];
  pos = L;
  for (int i = 1; i < L; ++i) {
    if (b[i] == ban)
      break;
    if (pos > R)
      break;
    swap(b[i], b[pos++]);
    if (pos > R)
      break;
  }
  for (int i = R + 1; i >= n; ++i) {
    if (b[i] == ban)
      continue;
    if (pos > R)
      break;
    swap(b[i], b[pos++]);
    if (pos > R)
      break;
  }
  bool flag = true;
  for (int i = 1; i <= n; ++i)
    if (a[i] == b[i]) {
      flag = false;
      break;
    }
  if (flag) {
    puts("Yes");
    for (int i = 1; i <= n; ++i)
      printf("%d ", b[i]);
    return 0;
  }
  puts("No");
  assert(pos <= R);
  return 0;
}