program dulun;
type
shuzu=array[1..10000]of integer;
var
s,m,l:shuzu;
i,j,n,a,b,c,max,k:integer;
f1,f2:text;
begin
assign(f1,'maxxl.in');reset(f1);
assign(f2,'maxxl.out');rewrite(f2);
readln(f1,n);
for i:=1 to n do
begin
readln(f1,s[i]);
m[i]:=1;
l[i]:=0;
end;
for i:=n-1 downto 1 do
begin
max:=0;
for j:=i+1 to n do
begin
if (s[i]<=s[j])and(m[j]>max) then begin
max:=m[j]; end;
if (m[j]=max)and(s[j]>s[i]) then begin m[i]:=max+1;
l[i]:=j; end
end;
end;
j:=n;for i:=1 to n do begin
if m[i]>=m[j] then j:=i;end;
writeln(f2,m[j]);
repeat
write(f2,s[j],' ');
j:=l[j];
until j<=0;
close(f2);
end.