#include <bits/stdc++.h>
using namespace std;
constexpr int MAXN = (1 << 20);
constexpr int MAXLOG = 20;
struct SparseTable
{
int sparse[MAXN][MAXLOG];
int prec_lg2[MAXN];
int n;
SparseTable(const vector<int> &a)
: n { 0 }
{
memset(sparse, 0, sizeof(sparse));
memset(prec_lg2, 0, sizeof(prec_lg2));
init(a);
}
int operation(const int a , const int b){
return min(a , b);
}
void init(const vector<int> &a)
{
n = a.size();
for(int i = 2 ; i < 2 * n ; ++i)
{
prec_lg2[i] = prec_lg2[i >> 1] + 1;
}
for(int i = 0 ; i < n ; ++i)
{
sparse[i][0] = a[i];
}
for(int j = 1 ; (1 << j) <= n ; ++j)
{
for(int i = 0; i < n ; ++i)
{
sparse[i][j] = operation(sparse[i][j - 1], sparse[i + (1 << (j - 1))][j - 1]);
}
}
}
int query(int l, int r) //[l,r]
{
int k = prec_lg2[r - l + 1];
return operation(sparse[l][k], sparse[r - (1 << k) + 1][k]);
}
};
int main() {
int n;
while(cin >> n)
{
assert(n > 1);
vector<int> v(n);
for(auto& el : v)
cin >> el;
SparseTable sparse_table(v);
cout << "0 0 ";
auto bin_search = [&sparse_table,&v](int r_ind , int cur_value)
{
int l = 0 , r = r_ind - 1;
if(v[r] < cur_value)
return r;
if(sparse_table.query(0 , r_ind - 1) >= cur_value)
return -1;
while(r - l > 1)
{
int mid = (r + l) / 2;
if(sparse_table.query(mid , r_ind - 1) < cur_value)
{
l = mid;
}
else
{
r = mid;
}
}
return l;
};
for(int cur = 2 ; cur < n ; ++cur)
{
int i = bin_search(cur , v[cur]);
if(i == -1)
{
cout << 0 << " ";
continue;
}
int j = bin_search(i , v[cur]);
cout << (j == -1 ? 0 : v[j]) << " ";
}
cout << endl;
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7Cgpjb25zdGV4cHIgaW50IE1BWE4gPSAoMSA8PCAyMCk7CmNvbnN0ZXhwciBpbnQgTUFYTE9HID0gM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