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

#include <bits/stdc++.h>
#define ll long long int
#define M 1000000007
#define mod 998244353
#define mp(x, y) make_pair(x, y)
#define pb(x) push_back(x)
#define pi pair<ll, ll>
#define endl "\\n"
using namespace std;
const ll N = 1000001;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  ll n;
  cin >> n;
  vector<ll> ar(n), br(n), ans(n);
  vector<ll> idx, idx1;
  for (ll i = 0; i < n; ++i) {
    cin >> ar[i];
  }
  for (ll i = 0; i < n; ++i) {
    cin >> br[i];
    ans[i] = br[i];
    if (ar[i] == br[i]) {
      idx.pb(i);
    } else {
      idx1.pb(i);
    }
  }
  if (idx.size() > 0) {
    ll len = 0;
    for (ll i = 0; i < idx.size(); ++i) {
      ll j = i + 1;
      while (j < idx.size() && br[idx[j]] == br[idx[i]]) {
        ++j;
      }
      --j;
      len = max(len, j - i + 1);
      i = j;
    }
    // cout<<"!!"<<len<<endl;
    for (ll i = 0; i < idx.size(); ++i) {
      ans[idx[i]] = br[idx[(i + len) % idx.size()]];
    }
  }
  bool check = 1;
  for (ll i = 0; i < n; ++i) {
    if (ar[i] == ans[i]) {
      check = 0;
    }
  }
  if (check) {
    cout << "Yes" << endl;
    for (ll i = 0; i < n; ++i) {
      cout << ans[i] << " ";
    }
  } else {
    // cout<<"!!";
    // for(auto x:ans){cout<<x<<" ";}
    // cout<<endl;
    idx.clear();
    for (ll i = 0; i < n; ++i) {
      if (ar[i] == ans[i]) {
        idx.pb(i);
      }
    }
    if (ans[idx.front()] != ans[idx.back()]) {
      exit(1);
    }
    ll j = 0;
    for (auto i : idx1) {
      if (ar[i] != ans[idx[j]]) {
        swap(ans[i], ans[idx[j]]);
        ++j;
      }
      if (j == idx.size()) {
        break;
      }
    }
    check = 1;
    for (ll i = 0; i < n; ++i) {
      if (ar[i] == ans[i]) {
        check = 0;
      }
    }
    if (check) {
      cout << "Yes" << endl;
      for (ll i = 0; i < n; ++i) {
        cout << ans[i] << " ";
      }
    } else {
      cout << "No";
    }
  }

  return (0);
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define ll long long int
#define M 1000000007
#define mod 998244353
#define mp(x, y) make_pair(x, y)
#define pb(x) push_back(x)
#define pi pair<ll, ll>
#define endl "\\n"
using namespace std;
const ll N = 1000001;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  ll n;
  cin >> n;
  ll ar[n], br[n], ans[n];
  vector<ll> idx;
  for (ll i = 0; i < n; ++i) {
    cin >> ar[i];
  }
  for (ll i = 0; i < n; ++i) {
    cin >> br[i];
    ans[i] = br[i];
    if (ar[i] == br[i]) {
      idx.pb(i);
    }
  }
  if (idx.size() > 0) {
    ll len = 0;
    for (ll i = 0; i < idx.size(); ++i) {
      ll j = i + 1;
      while (j < idx.size() && br[idx[j]] == br[idx[i]]) {
        ++j;
      }
      --j;
      len = max(len, j - i + 1);
      i = j;
    }
    // cout<<"!!"<<len<<endl;
    for (ll i = 0; i < idx.size(); ++i) {
      ans[idx[i]] = br[idx[(i + len) % n]];
    }
  }
  bool check = 1;
  for (ll i = 0; i < n; ++i) {
    if (ar[i] == ans[i]) {
      check = 0;
    }
  }
  if (check) {
    cout << "Yes" << endl;
    for (auto x : ans) {
      cout << x << " ";
    }
  } else {
    cout << "No";
  }

  return (0);
}