Problem p02971 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

typedef long long ll;

#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)
#define fi first
#define se second
#define pb push_back
#define pll pair<ll, ll>
#define vi vector<int>
#define vb vector<bool>
#define vvi vector<vi>
#define vpll vector<pll>
#define vll vector<ll>
#define vvll vector<vll>
#define sz(x) (int)x.size()
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

void fastik() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
}

// ifstream &cin
void solve() {}

int main() {
  fastik();
  // ifstream cin("input.txt");
  /*ll t = 1;
  cin >> t;
  while (t--) {
      solve();
  }*/
  ll n;
  cin >> n;
  vll a(n, 0);
  multiset<ll> s;
  set<ll> ss;
  forn(i, n) {
    cin >> a[i];
    s.insert(a[i]);
    ss.insert(a[i]);
  }
  ll x;
  ll cnt;
  vll ans(n + 1, 0);
  forn(i, n) {
    if (ans[a[i]]) {
      cout << ans[a[i]] << '\n';
      continue;
    }
    for (auto it = ss.rbegin(); it != ss.rend(); ++it) {
      x = *it;
      if (x != a[i]) {
        cout << x << '\n';
        ans[a[i]] = x;
        break;
      } else if ((x == a[i] && s.count(a[i]) > 1)) {
        cout << x << '\n';
        ans[a[i]] = x;
        break;
      }
    }
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

typedef long long ll;

#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)
#define fi first
#define se second
#define pb push_back
#define pll pair<ll, ll>
#define vi vector<int>
#define vb vector<bool>
#define vvi vector<vi>
#define vpll vector<pll>
#define vll vector<ll>
#define vvll vector<vll>
#define sz(x) (int)x.size()
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

void fastik() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
}

// ifstream &cin
void solve() {}

int main() {
  fastik();
  // ifstream cin("input.txt");
  /*ll t = 1;
  cin >> t;
  while (t--) {
      solve();
  }*/
  ll n;
  cin >> n;
  vll a(n, 0);
  multiset<ll> s;
  set<ll> ss;
  forn(i, n) {
    cin >> a[i];
    s.insert(a[i]);
    ss.insert(a[i]);
  }
  ll x;
  ll cnt;
  vll ans(ll(2e+5) + 1, 0);
  forn(i, n) {
    if (ans[a[i]]) {
      cout << ans[a[i]] << '\n';
      continue;
    }
    for (auto it = ss.rbegin(); it != ss.rend(); ++it) {
      x = *it;
      if (x != a[i]) {
        cout << x << '\n';
        ans[a[i]] = x;
        break;
      } else if ((x == a[i] && s.count(a[i]) > 1)) {
        cout << x << '\n';
        ans[a[i]] = x;
        break;
      }
    }
  }

  return 0;
}