#include<fstream>
#include<iostream>
#include<cstring>
using namespace std;
ifstream fin("scmax.in");
ofstream fout("scmax.out");
const int NMax = 100005;
const int oo = 1<<30;
int N,ND;
int A[NMax],D[NMax],Pre[NMax];
void Read()
{
fin>>N;
for(int i = 1 ; i <= N ; ++i)
fin>>A[i];
}
void Solve()
{
for(int i = 1 ; i <= N ; ++i)
{
int st = 1,dr = ND + 1,pos = ND + 1;
while(st <= dr)
{
int mid = (st + dr) / 2;
if(A[D[mid]] < A[i]) st = mid + 1;
else
{
dr = mid - 1;
pos = mid;
}
}
ND = max(ND,pos);
D[pos] = i;
Pre[D[pos]] = D[pos - 1];
}
fout<<ND<<"\n";
}
void Print(int i)
{
if(i)
{
Print(Pre[i]);
fout<<A[i]<<" ";
}
}
int main()
{
Read();
Solve();
Print(D[ND]);
fin.close();
fout.close();
return 0;
}