#include<algorithm>
#include<iostream>
#include<fstream>
#define DX 100009
using namespace std;
fstream fin("scmax.in",ios::in),fout("scmax.out",ios::out);
pair<int,int> x[DX];
int st[DX],ls,ai[4*DX],t[4*DX],tatic[4*DX],maxim,rmaxim,tata;
void query(int nod,int st,int dr,int stq,int drq)
{
int m=(st+dr)/2,plod=nod*2;
if(dr<stq || drq<st) return ;
if(stq<=st && dr<=drq)
{
if(maxim<ai[nod])
{
maxim=ai[nod];
tata=t[nod];
}
return ;
}
if(st==dr) return ;
query(plod,st,m,stq,drq);
query(plod+1,m+1,dr,stq,drq);
}
void update(int nod,int st,int dr,int poz,int cucat)
{
int m=(st+dr)/2,plod=nod*2;
if(st==dr)
{
if(st==poz)
{
ai[nod]=cucat;
t[nod]=tata;
}
return ;
}
if(st<=poz && poz<=m) update(plod,st,m,poz,cucat);
if(m<poz && poz<=dr) update(plod+1,m+1,dr,poz,cucat);
ai[nod]=ai[plod];
t[nod]=t[plod];
if(ai[nod]<ai[plod+1])
{
ai[nod]=ai[plod+1];
t[nod]=t[plod+1];
}
}
void rec(int poz)
{
if(poz==0) return ;
rec(tatic[poz]);
fout<<x[poz].first<<" ";
}
int main()
{
int i,j,n,poz;
fin>>n;
for(i=1;i<=n;i++)
{
fin>>x[i].first;
x[i].second=i;
}
sort(x+1,x+n+1);
ls=0;
for(i=1;i<=n;i++)
{
if(x[i].first==x[i+1].first)
ls++;
else
{
for(j=i;j>=i-ls;j--)
{
maxim=0;
query(1,1,n,1,x[j].second-1);
tatic[j]=tata;
tata=j;
update(1,1,n,x[j].second,maxim+1);
}
ls=0;
}
}
fout<<ai[1]<<"\n";
rec(t[1]);
}