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

#include <bits/stdc++.h>
#define fo(i, a, b) for (int i = a; i <= b; ++i)
#define fd(i, a, b) for (int i = a; i >= b; --i)
#define fl(i, a, b) for (int i = a; i < b; ++i)
#define fa(i, a) for (auto(i) : (a))
#define F first
#define S second
#define lb lower_bound
#define ub upper_bound
#define all(a) a.begin(), a.end()
#define vi vector<int>
#define ii pair<int, int>
#define pb push_back
using namespace std;
template <typename T> inline void read(T &x) {
  char c;
  bool nega = 0;
  while ((!isdigit(c = getchar())) && (c != '-'))
    ;
  if (c == '-') {
    nega = 1;
    c = getchar();
  }
  x = c - 48;
  while (isdigit(c = getchar()))
    x = x * 10 + c - 48;
  if (nega)
    x = -x;
}
template <typename T> inline void writep(T x) {
  if (x > 9)
    writep(x / 10);
  putchar(x % 10 + 48);
}
template <typename T> inline void write(T x) {
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  writep(x);
  putchar(' ');
}
template <typename T> inline void writeln(T x) {
  write(x);
  putchar('\\n');
}
template <typename R, typename D> inline void Min(R &a, D b) {
  if (a > b)
    a = b;
}
template <typename D, typename R> inline void Max(D &a, R b) {
  if (a < b)
    a = b;
}
const int N = 200005, M = 300005;
int n, a[N], b[N], c, q, d, T;
int main() {
  ios_base::sync_with_stdio(NULL);
  cin.tie(NULL);
  cout.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen(".inp", "r", stdin);
#endif
  cin >> n;
  fo(i, 1, n) cin >> a[i];
  fo(i, 1, n) cin >> b[i];
  sort(b + 1, b + 1 + n, greater<int>());
  fo(i, 1, n) if (a[i] == b[i]) {
    int tar = a[i];
    int cnt = 0;
    int j = i;
    int liml;
    fo(i, 1, n) if (a[i] == tar) {
      liml = i;
      break;
    }
    int limr;
    fd(i, n, 1) if (a[i] == tar) {
      limr = i;
      break;
    }
    while (a[i] == b[i]) {
      ++i;
      ++cnt;
    }
    int l = liml - 1;
    fo(i, 1, n) {
      if (b[i] == tar) {
        Min(l, i - 1);
        break;
      }
    }
    int r = n - limr;
    fd(i, n, limr) {
      if (b[i] == tar) {
        Min(r, n - i);
        break;
      }
    }
    if (l + r < cnt) {
      cout << "No";
      return 0;
    }
    cout << "Yes\\n";
    fo(k, 1, l) {
      swap(b[j], b[k]);
      ++j;
      if (j == i)
        break;
    }
    fd(k, n, n - r + 1) {
      swap(b[j], b[k]);
      ++j;
      if (j == i)
        break;
    }
    fo(k, 1, n) cout << b[k] << ' ';
    return 0;
  }
  cout << "Yes\\n";
  fo(i, 1, n) cout << b[i] << ' ';
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define fo(i, a, b) for (int i = a; i <= b; ++i)
#define fd(i, a, b) for (int i = a; i >= b; --i)
#define fl(i, a, b) for (int i = a; i < b; ++i)
#define fa(i, a) for (auto(i) : (a))
#define F first
#define S second
#define lb lower_bound
#define ub upper_bound
#define all(a) a.begin(), a.end()
#define vi vector<int>
#define ii pair<int, int>
#define pb push_back
using namespace std;
template <typename T> inline void read(T &x) {
  char c;
  bool nega = 0;
  while ((!isdigit(c = getchar())) && (c != '-'))
    ;
  if (c == '-') {
    nega = 1;
    c = getchar();
  }
  x = c - 48;
  while (isdigit(c = getchar()))
    x = x * 10 + c - 48;
  if (nega)
    x = -x;
}
template <typename T> inline void writep(T x) {
  if (x > 9)
    writep(x / 10);
  putchar(x % 10 + 48);
}
template <typename T> inline void write(T x) {
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  writep(x);
  putchar(' ');
}
template <typename T> inline void writeln(T x) {
  write(x);
  putchar('\\n');
}
template <typename R, typename D> inline void Min(R &a, D b) {
  if (a > b)
    a = b;
}
template <typename D, typename R> inline void Max(D &a, R b) {
  if (a < b)
    a = b;
}
const int N = 200005, M = 300005;
int n, a[N], b[N], c, q, d, T;
int main() {
  ios_base::sync_with_stdio(NULL);
  cin.tie(NULL);
  cout.tie(NULL);
#ifndef ONLINE_JUDGE
  freopen(".inp", "r", stdin);
#endif
  cin >> n;
  fo(i, 1, n) cin >> a[i];
  fo(i, 1, n) cin >> b[i];
  sort(b + 1, b + 1 + n, greater<int>());
  fo(i, 1, n) if (a[i] == b[i]) {
    int tar = a[i];
    int cnt = 0;
    int j = i;
    int liml;
    fo(i, 1, n) if (a[i] == tar) {
      liml = i;
      break;
    }
    int limr;
    fd(i, n, 1) if (a[i] == tar) {
      limr = i;
      break;
    }
    while (a[i] == b[i]) {
      ++i;
      ++cnt;
    }
    int l = 0;
    fo(i, 1, liml) {
      if (b[i] == tar) {
        l = i - 1;
        break;
      }
    }
    int r = 0;
    fd(i, n, limr) {
      if (b[i] == tar) {
        r = n - i;
        break;
      }
    }
    if (l + r < cnt) {
      cout << "No";
      return 0;
    }
    cout << "Yes\\n";
    fo(k, 1, l) {
      swap(b[j], b[k]);
      ++j;
      if (j == i)
        break;
    }
    fd(k, n, n - r + 1) {
      swap(b[i], b[k]);
      --i;
      if (j == i)
        break;
    }
    fo(k, 1, n) cout << b[k] << ' ';
    return 0;
  }
  cout << "Yes\\n";
  fo(i, 1, n) cout << b[i] << ' ';
  return 0;
}