program cc;
const
fin='maxxl.in';fout='maxxl.out';
var
f,ff,qianqu,a:array[0..10000] of longint;
i,j,n,max,num:longint;
f1,f2:text;
begin
assign(f1,fin);reset(f1);
readln(f1,n);
for i:=1 to n do
readln(f1,f[i]);
max:=0;
for i:=1 to n do ff[i]:=1;
for i:=2 to n do
for j:=1 to i-1 do
if (f[j]<f[i])and(ff[j]>=ff[i]-1) then
begin
ff[i]:=ff[j]+1;
qianqu[i]:=j;
if ff[i]>max then
begin
max:=ff[i];num:=i;
end;
end;
assign(f2,fout);rewrite(f2);
writeln(f2,max);
j:=1;
repeat
a[j]:=f[num];
num:=qianqu[num];
inc(j);
until j>max;
for i:=max downto 2 do write(f2,a[i],' ');
write(f2,a[1]);
close(f1);
close(f2);
end.