#include<bits/stdc++.h>
#define fast cin.tie(NULL); cout.tie(NULL); ios_base::sync_with_stdio(false);
#define ll long long
#define mp(x,y) make_pair(x,y)
#define pb(x) push_back(x)
using namespace std;
const int N = 1e5 + 121;
int i,n,kol,l,r,m;
int a[N],b[N],t[N];
int main()
{
fast;
cin>>n;
for(i=1; i<=n; ++i) cin>>a[i];
for(i=n; i>0; --i)
{
if(kol)
{
l=1;
r=kol;
while(l+1<r)
{
m=(r+l) >> 1;
if (t[m]<=a[i]) r=m; else l=m;
}
if (t[r]>a[i])
{
b[i]=t[r];
kol=r+1;
t[kol]=a[i];
} else
if (t[l]>a[i])
{
b[i]=t[l];
kol=l+1;
t[kol]=a[i];
} else
{
kol=1;
t[1]=a[i];
}
} else
{
kol=1;
t[1]=a[i];
}
}
for(i=1; i<n; ++i) cout<<b[i]<<' '; cout<<b[n]<<'\n';
return 0;
}