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