//#include <iostream>
#include <fstream>
using namespace std;
#define LE 100666
int A[LE];
int my_binary_search(int left,int right,int Best[],int value)
{
if (right==0) return 0;
while (left<right)
{
int mid=(left+right+1)/2;
if (A[Best[mid]]<=value) left=mid;
else right=mid-1;
}
if (right==1&&A[Best[right]]>value) return 0;
return right;
}
int Best[LE],L[LE],last_value[LE];
ifstream f("scmax.in");
ofstream g("scmax.out");
#define cout g
void Write(int pos)
{
if (pos==0) return;
Write(last_value[pos]);
cout<<A[pos]<<" ";
}
int main()
{
int n,i;
f>>n;
for(i=1; i<=n; ++i) f>>A[i]; ///reading
int right=0; ///
for(i=1; i<=n; ++i)
{
int pos=my_binary_search(1,right,Best,A[i]-1);
if (pos!=0) last_value[i]=Best[pos];
++pos;
L[i]=pos;
if (pos>right)
Best[++right]=i;
else
{
if (A[Best[pos]]>A[i])
Best[pos]=i;
}
}
int result=0,ind=0;
for(i=1; i<=n; ++i)
if (L[i]>result)
result=L[i],ind=i;
cout<<result<<'\n';
Write(ind);
return 0;
}