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

//#include <ext/pb_ds/tree_policy.hpp>
//#include <ext/pb_ds/assoc_container.hpp>
#include <bits/stdc++.h>
#include <map>
#include <vector>
#include <set>
#include <algorithm>
#include <queue>
#include <stack>
#include <list>
#include <string>
#include <iostream>
#include <cmath>
#include <sstream>
#include <cstdlib>
#include <cctype>
#include <fstream>
#include <numeric>
#include <iterator>
#include <cstdio>
#include <cstring>

using namespace std;
//using namespace __gnu_pbds;

#define ll long long
#define ull unsigned long long
#define fastio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define fread(p) freopen(p,"r",stdin);
#define fwrite(p) freopen(p,"w",stdout);
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pb push_back
#define F first
#define S second
#define nl "\n"
#define mem(s,k) memset(s, k, sizeof(s))
#define sf scanf
#define pf printf
#define PI 2*acos(0.0)
#define ALL(p) (p.begin(),p.end())
#define mod(a,b) ((((a)%(b))+(b))%(b))
#define time_start clock_t start = clock();
#define time_end clock_t finish = clock(); cout << (double)(finish-start)/CLOCKS_PER_SEC << endl;
const double EPS = 1e-9;

////--- Graph Moves---////
//const ll xx[] = {0,0,1,-1};
//const ll yy[] = {1,-1,0,0};
//const ll xx_king[] = {0,0,1,-1,1,1,-1,-1};
//const ll yy_king[] = {1,-1,0,0,1,-1,1,-1};
//const ll xx_knight[]= {-2,-1,1,2,2,1,-1,-2};
//const ll yy_knight[]= {1,2,2,1,-1,-2,-2,-1};
////------------------////

ll POW(ll b,ll p) {ll Ans=1; while(p){if(p&1)Ans=(Ans*b);b=(b*b);p>>=1;}return Ans;}
ll BigMod(ll b,ll p,ll Mod) {ll Ans=1; while(p){if(p&1)Ans=(Ans*b)%Mod;b=(b*b)%Mod;p>>=1;}return Ans%Mod;}
ll ModInverse(ll b,ll Mod) {return BigMod(b,Mod-2,Mod);}

string tostr(ll n) {stringstream rr;rr<<n;return rr.str();}
//template<typename T> using orderedSet = tree< T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

////----------- Bit Manipulation -----------------//
//int bitOn(int N,int pos){return N=N OR (1<<pos);}
//int bitOff(int N,int pos){return N= N &~(1<<pos);}
//bool bitCheck(int N,int pos){return (bool)(N & (1<<pos));}
////---------------------------------------------//

////------------ Debugging -----------------------//
#define debug(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); deb(_it, args); }
void deb(istream_iterator<string> it) {cout<<endl;}
template<typename T, typename... Args>
void deb(istream_iterator<string> it, T a, Args... args) {
    cerr << *it << " = " << a << ",   ";
    deb(++it, args...);
}

#define ekhane(k) cerr << "Ekhane asche-" << k << "\n";
#define printa(a,L,R) for(int i=L;i<R;i++) cout<<a[i]<<(i==R-1?'\n':' ')
#define printv(a) printa(a,0,a.size())
#define print2d(a,r,c) for(int i=0;i<r;i++) for(int j=0;j<c;j++) cout<<a[i][j]<<(j==c-1?'\n':' ')
////-----------------------------------------------//

const int MAXN = 200000+5;
ll lg[MAXN+1];
ll st[MAXN][32];

ll mx(ll L, ll R)
{
    ll j = lg[R - L + 1];
    return max(st[L][j], st[R - (1 << j) + 1][j]);
}
int main()
{

    lg[1] = 0;
    for (ll i = 2; i <= MAXN; i++)
        lg[i] = lg[i/2] + 1;

    ll N;
    cin >> N;
    ll a[N+2];
    for(ll i=0; i<N; i++)
        cin >> a[i];

    for (ll i = 0; i < N; i++)
        st[i][0] = a[i];

    for (ll j = 1; j <= 30; j++)
        for (ll i = 0; i + (1 << j) <= N; i++)
            st[i][j] = max(st[i][j-1], st[i + (1 << (j - 1))][j - 1]);

    cout << mx(1,N-1) << nl;
    for(ll i=1; i<N-1; i++){
        cout << max(mx(0,i-1),mx(i+1,N-1)) << nl;
    }
    cout << mx(0,N-2) << nl;
}


Measured memory_bytes: 54784