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

#include <bits/stdc++.h>
using namespace std;
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define FRE                                                                    \\
  freopen("input.txt", "r", stdin);                                            \\
  freopen("output.txt", "w", stdout);
#define int long long
#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define trace(...)
#endif
#define endl "\\n"
const int oo = INT_MAX;
const int MOD = 1e9 + 7;
const int sz = 1e5 + 5;

int32_t main() {
  IOS;
  int tc = 1;
  // cin >> tc;
  while (tc--) {
    int n, ind = 0;
    cin >> n;
    vector<int> a(n), b(n);
    for (auto &el : a) {
      cin >> el;
    }
    for (auto &el : b) {
      cin >> el;
    }
    reverse(b.begin(), b.end());
    int common = -1, l = -1, r = -1;
    for (int i = 0; i < n; ++i) {
      if (a[i] == b[i]) {
        common = a[i];
        l = i;
        break;
      }
    }
    for (int i = 0; i < n; ++i) {
      if (a[i] == b[i] && a[i] == common) {
        r = i;
      }
    }
    if (l != -1 && r != -1) {
      for (int i = 0; i < n && l <= r; ++i) {
        if (a[i] == common || b[i] == common)
          continue;
        swap(b[i], b[l++]);
      }
      if (l <= r)
        return 0 * printf("No\\n");
    }
    cout << "Yes" << endl;
    for (auto el : b)
      cout << el << " ";
  }
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define FRE                                                                    \\
  freopen("input.txt", "r", stdin);                                            \\
  freopen("output.txt", "w", stdout);
#define int long long
#define TRACE
#ifdef TRACE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << " : " << arg1 << std::endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define trace(...)
#endif
#define endl "\\n"
const int oo = INT_MAX;
const int MOD = 1e9 + 7;
const int sz = 1e5 + 5;

int32_t main() {
  IOS;
  int tc = 1;
  // cin >> tc;
  while (tc--) {
    int n, ind = 0;
    cin >> n;
    vector<int> a(n), b(n);
    map<int, pair<int, int>> cnt;
    map<int, unordered_set<int>> index;
    for (auto &el : a) {
      cin >> el;
      cnt[el].first++;
      index[el].insert(ind++);
    }
    for (auto &el : b) {
      cin >> el;
      cnt[el].second++;
    }
    bool flag = 0;
    for (auto el : cnt) {
      if (min(el.second.first, el.second.second) > n / 2) {
        flag = 1;
        break;
      }
    }
    vector<int> ans(n);
    if (flag)
      return 0 * printf("No\\n");
    int safe = 0;
    vector<bool> vis(n, false);
    int cnt_ = 0;
    set<int> st;
    for (int i = 0; i < n; ++i)
      st.insert(i);
    for (auto el : b) {
      flag = 0;
      for (auto _i : st) {
        if (index[el].find(_i) != index[el].end())
          continue;
        else
          safe = _i;
      }
      ans[safe] = el;
      st.erase(st.find(safe));
    }
    cout << "Yes" << endl;
    for (auto el : ans)
      cout << el << " ";
  }
}