#include <fstream>
#define nmax 100009
using namespace std;
ifstream fin ("scmax.in");
ofstream fout ("scmax.out");
int n;
int a[nmax];
int lgmax[nmax];
int poz[nmax];
void citire();
void pd();
void afisare();
int main()
{
citire();
pd();
afisare();
return 0;
}
void citire()
{
int i;
fin>>n;
for (i=1; i<=n; i++)
fin>>a[i];
}
void pd()
{
int i,j;
lgmax[n]=1;
poz[n]=0;
for(i=n-1; i>=1; i--)
{
lgmax[i]=1;
poz[i]=0;
for (j=i+1; j<=n; j++)
if (a[i]<a[j])
if(lgmax[i]<1+lgmax[j])
{
lgmax[i]=1+lgmax[j];
poz[i]=j;
}
}
}
void afisare()
{
int i, lg=0,loc=0;
for (i=1; i<=n; i++)
if(lg<lgmax[i])
{
lg=lgmax[i];
loc=i;
}
fout<<lg<<'\n';
while (loc)
{fout<<a[loc]<<' ';
loc=poz[loc];
}
fout<<'\n';
fin.close();
fout.close();
}