#include <cstdio>
#define NMAX 100005
#define INF (1<<30)+10
using namespace std;
FILE* fin = fopen("scmax.in","r");
FILE* fout = fopen("scmax.out","w");
int a[NMAX],best[NMAX], poz[NMAX],lg;
//best[i] = cel mai mic element cu care se poate termina un subsir crescator de lg i
//poz[i] = pozitia pe care a fost plasat a[i] in best
int CautaBinar(int x)
{
int st, fi, mij;
st = 0; fi = lg+1;
while(fi-st>1)
{
mij = (st+fi)/2;
if(best[mij] >= x)
fi = mij;
else
st = mij;
}
return fi;
}
int main()
{
int N;
fscanf(fin,"%d",&N);
for(int i = 1; i<=N; i++)
{
fscanf(fin,"%d",&a[i]);
best[i] = INF;
}
lg = 1;
best[1] = a[1];
poz[1] = 1;
best[0] = -1;
for(int i = 2; i<=N; i++)
{
if(a[i] > best[lg])
{
lg++;
best[lg] = a[i];
}
else
{
int pp = CautaBinar(a[i]);
best[pp] = a[i];
}
}
fprintf(fout,"%d\n",lg);
for(int i = 1; i<=lg; i++)
fprintf(fout,"%d ",best[i]);
fprintf(fout,"\n");
return 0;
}