#include <iostream>
#include <fstream>
using namespace std;
ifstream fin ("scmax.in");
ofstream fout ("scmax.out");
const int NR = 100000;
int a[NR], l[NR], t[NR], n, f, Lmax;
void Rec (int ind)
{
if (ind == 0) return;
Rec (t[ind]);
fout << a[ind] <<" ";
}
int main()
{
fin >> n;
for (int i = 1; i <= n; i ++) {fin >> a[i]; l[i] = 1;}
for (int i = 1; i <= n; i ++)
{
for (int j = 1; j < i; j ++)
if (l[j] >= l[i] && a[j] < a[i])
{
l[i] = l[j] + 1;
t[i] = j;
}
if (Lmax < l[i])
{
Lmax = l[i];
f = i;
}
}
fout << Lmax <<"\n";
Rec(f);
return 0;
}