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

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

#define int long long int
#define bp(x) __builtin_popcount(x)
#define ld long double
#define f(i, j, n) for (int i = j; i <= n; i++)
#define r(i, n, j) for (int i = n; i >= j; i--)
#define all(container) container.begin(), container.end()
#define sz(container) (int)container.size()
#define ff first
#define ss second
#define pii pair<int, int>
#define sp(x) setprecision(x)
#define endl "\\n"
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define T                                                                      \\
  int ttt;                                                                     \\
  cin >> ttt;                                                                  \\
  while (ttt--)
#define fast ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);

int power(int x, int y, int p) {
  int res = 1;
  x = x % p;
  if (x == 0)
    return 0;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}

int32_t main() {
  fast int n, x, ff = 0;
  cin >> n;
  int a[n + 1], b[n + 1];
  multiset<int> s, s2, s3;
  multiset<int>::iterator it;
  f(i, 1, n) cin >> a[i];
  f(i, 1, n) {
    cin >> x;
    s.insert(x);
    s2.insert(x);
    s3.insert(x);
  }
  f(i, 1, n) {
    it = s.upper_bound(a[i]);
    if (it != s.end() && *it != a[i]) {
      b[i] = *it;
      s.erase(it);
    } else if (*(s.begin()) != a[i]) {
      b[i] = *(s.begin());
      s.erase(s.begin());
    } else
      ff = 1;
  }
  if (ff) {
    ff = 0;
    f(i, 1, n) {
      it = s2.end();
      it--;
      if (*(s2.begin()) != a[i]) {
        b[i] = *(s2.begin());
        s2.erase(s2.begin());
      } else if (*it != a[i]) {
        b[i] = *it;
        s2.erase(it);
      } else
        ff = 1;
    }
    if (ff) {
      ff = 0;
      f(i, 1, n) {
        it = s3.end();
        it--;
        if (*it != a[i]) {
          b[i] = *it;
          s3.erase(it);
        } else if (*(s3.begin()) != a[i]) {
          b[i] = *(s3.begin());
          s3.erase(s3.begin());
        } else
          ff = 1;
      }
      if (ff)
        cout << "No";
      else {
        cout << "Yes\\n";
        f(i, 1, n) cout << b[i] << " ";
      }
    } else {
      cout << "Yes\\n";
      f(i, 1, n) cout << b[i] << " ";
    }
  } else {
    cout << "Yes\\n";
    f(i, 1, n) cout << b[i] << " ";
  }
}

Error: WA

Fixed submission:

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

#define int long long int
#define bp(x) __builtin_popcount(x)
#define ld long double
#define f(i, j, n) for (int i = j; i <= n; i++)
#define r(i, n, j) for (int i = n; i >= j; i--)
#define all(container) container.begin(), container.end()
#define sz(container) (int)container.size()
#define ff first
#define ss second
#define pii pair<int, int>
#define sp(x) setprecision(x)
#define endl "\\n"
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define T                                                                      \\
  int ttt;                                                                     \\
  cin >> ttt;                                                                  \\
  while (ttt--)
#define fast ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);

int power(int x, int y, int p) {
  int res = 1;
  x = x % p;
  if (x == 0)
    return 0;
  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
int n;
int a[200005], b[200005], c[200005];
bool solve1() {
  multiset<int> s;
  multiset<int>::iterator it;
  f(i, 1, n) s.insert(c[i]);
  bool ff = 1;
  f(i, 1, n) {
    it = s.upper_bound(a[i]);
    if (it != s.end() && *it != a[i]) {
      b[i] = *it;
      s.erase(it);
    } else if (*(s.begin()) != a[i]) {
      b[i] = *(s.begin());
      s.erase(s.begin());
    } else
      ff = 0;
  }
  return ff;
}
bool solve2() {
  multiset<int> s2;
  multiset<int>::iterator it;
  f(i, 1, n) s2.insert(c[i]);
  bool ff = 1;
  f(i, 1, n) {
    it = s2.end();
    it--;
    if (*(s2.begin()) != a[i]) {
      b[i] = *(s2.begin());
      s2.erase(s2.begin());
    } else if (*it != a[i]) {
      b[i] = *it;
      s2.erase(it);
    } else
      ff = 1;
  }
}
bool solve3() {
  multiset<int> s3;
  multiset<int>::iterator it;
  f(i, 1, n) s3.insert(c[i]);
  bool ff = 1;
  f(i, 1, n) {
    it = s3.end();
    it--;
    if (*it != a[i]) {
      b[i] = *it;
      s3.erase(it);
    } else if (*(s3.begin()) != a[i]) {
      b[i] = *(s3.begin());
      s3.erase(s3.begin());
    } else
      ff = 1;
  }
}

int32_t main() {
  fast cin >> n;
  f(i, 1, n) cin >> a[i];
  f(i, 1, n) cin >> c[i];
  bool flag = solve1();
  if (flag) {
    cout << "Yes\\n";
    f(i, 1, n) cout << b[i] << " ";
    exit(0);
  }
  flag = solve2();
  if (flag) {
    cout << "Yes\\n";
    f(i, 1, n) cout << b[i] << " ";
    exit(0);
  }
  flag = solve3();
  if (flag) {
    cout << "Yes\\n";
    f(i, 1, n) cout << b[i] << " ";
    exit(0);
  }
  cout << "No";
}