#include <bits/stdc++.h>
#define nrmax 100007
using namespace std;
int n, a[nrmax];
int dp[nrmax], m;
void Citire()
{
int i;
ifstream fin ("scmax.in");
fin >> n;
for (i=1; i<=n; i++)
fin >> a[i];
fin.close();
}
void CautBin(int x)
{
if (x>dp[m])
{
dp[++m]=x;
return;
}
if (x<dp[1])
{
dp[1]=x;
return;
}
int st, dr;
st=1;
dr=m;
int mij;
int poz=1;
while (st<=dr)
{
mij=(st+dr)/2;
if (dp[mij]>=x)
{
poz=mij;
dr=mij-1;
}
else /// dp[m]<x
st=mij+1;
}
dp[poz]=x;
}
void Rezolvare()
{
int i;
dp[1]=a[1];
m=1;
for (i=2; i<=n; i++)
CautBin(a[i]);
ofstream fout ("scmax.out");
fout << m << "\n";
for (i=1; i<=m; i++)
fout << dp[i] << " ";
fout << "\n";
fout.close();
}
int main()
{
Citire();
Rezolvare();
return 0;
}