Problem p02971 (C++)

Buggy submission (status: Runtime Error):
// stringray

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

/*#ifndef __LOCAL__
#pragma comment(linker,"/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#endif*/

using namespace std;
using namespace __gnu_pbds;

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ost;

typedef pair<int, int> pii;
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<pii> vpii;
typedef vector<vi> vvi;
typedef vector<set<int>> vsi;

#define endl "\n"
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define pb emplace_back
#define F first
#define S second
#define fori(i, n) for (int i = 1; i <= n; ++i)
#define forn(i, n) for (int i = 0; i < n; ++i)
#define sz(x) int(x.size())
#define error(x) cout << #x << " " << x << endl
#define IOS                                                                    \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(nullptr);                                                            \
  cout.tie(0)

const int N = 100 * 1000;
const int mod = 1e9 + 7;

int min3(int a, int b, int c) { return min(a, min(b, c)); }
int max3(int a, int b, int c) { return max(a, max(b, c)); }
int min4(int a, int b, int c, int d) { return min(min(a, b), min(c, d)); }
int max4(int a, int b, int c, int d) { return max(max(a, b), max(c, d)); }

template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}
template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
  for (T &x : vec)
    is >> x;
  // for(int i=0;i<vec.size();++i)is>>x[i];
  return is;
}

template <typename T> ostream &operator<<(ostream &os, vector<T> &vec) {
  for (int i = 0; i < vec.size(); ++i) {
    os << vec[i] << (i + 1 == vec.size() ? "" : " ");
  }
  return os;
}

ll mpow(ll a, ll b) {
  ll ret = 1;
  while (b > 0) {
    if (b & 1)
      ret = (ret * a) % mod;
    (a = a * a) %= mod;
    b >>= 1LL;
  }
  return ret;
}

// #define int int64_t
int a[N + 2];
int pref[N + 2];
int suff[N + 2];
void solve() {
  int n;
  cin >> n;
  fori(i, n) cin >> a[i];
  fori(i, n) {
    pref[i] = a[i];
    if (i > 1)
      pref[i] = max(pref[i - 1], a[i]);
  }
  for (int i = n; i >= 1; --i) {
    suff[i] = a[i];
    if (i != n)
      suff[i] = max(suff[i], suff[i + 1]);
  }
  fori(i, n) {
    if (i == 1)
      cout << suff[2] << endl;
    else if (i == n)
      cout << pref[n - 1] << endl;
    else
      cout << max(pref[i - 1], suff[i + 1]) << endl;
  }
}
int32_t main() {
  IOS;
  solve();
  return 0;
}


Fixed submission:
// stringray

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

/*#ifndef __LOCAL__
#pragma comment(linker,"/stack:200000000")
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#endif*/

using namespace std;
using namespace __gnu_pbds;

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    ost;

typedef pair<int, int> pii;
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<pii> vpii;
typedef vector<vi> vvi;
typedef vector<set<int>> vsi;

#define endl "\n"
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define pb emplace_back
#define F first
#define S second
#define fori(i, n) for (int i = 1; i <= n; ++i)
#define forn(i, n) for (int i = 0; i < n; ++i)
#define sz(x) int(x.size())
#define error(x) cout << #x << " " << x << endl
#define IOS                                                                    \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(nullptr);                                                            \
  cout.tie(0)

const int N = 100 * 1000;
const int mod = 1e9 + 7;

int min3(int a, int b, int c) { return min(a, min(b, c)); }
int max3(int a, int b, int c) { return max(a, max(b, c)); }
int min4(int a, int b, int c, int d) { return min(min(a, b), min(c, d)); }
int max4(int a, int b, int c, int d) { return max(max(a, b), max(c, d)); }

template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}
template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
  for (T &x : vec)
    is >> x;
  // for(int i=0;i<vec.size();++i)is>>x[i];
  return is;
}

template <typename T> ostream &operator<<(ostream &os, vector<T> &vec) {
  for (int i = 0; i < vec.size(); ++i) {
    os << vec[i] << (i + 1 == vec.size() ? "" : " ");
  }
  return os;
}

ll mpow(ll a, ll b) {
  ll ret = 1;
  while (b > 0) {
    if (b & 1)
      ret = (ret * a) % mod;
    (a = a * a) %= mod;
    b >>= 1LL;
  }
  return ret;
}

// #define int int64_t
int a[2 * N + 2];
int pref[2 * N + 2];
int suff[2 * N + 2];
void solve() {
  int n;
  cin >> n;
  fori(i, n) cin >> a[i];
  fori(i, n) {
    pref[i] = a[i];
    if (i > 1)
      pref[i] = max(pref[i - 1], a[i]);
  }
  for (int i = n; i >= 1; --i) {
    suff[i] = a[i];
    if (i != n)
      suff[i] = max(suff[i], suff[i + 1]);
  }
  fori(i, n) {
    if (i == 1)
      cout << suff[2] << endl;
    else if (i == n)
      cout << pref[n - 1] << endl;
    else
      cout << max(pref[i - 1], suff[i + 1]) << endl;
  }
}
int32_t main() {
  IOS;
  solve();
  return 0;
}