program maxxl;
var i,j,x,n,f:integer;
a,b,c:array [1..10000] of integer;
f1,f2:text;
begin
assign (f1,'maxxl.in');
assign (f2,'maxxl.out');
reset (f1);
rewrite (f2);
readln (f1,n);
for i:=1 to n do
begin
readln (f1,a[i]);
b[i]:=1;
c[i]:=0;
end;
for i:=n downto 1 do
begin
x:=0;
for j:=i+1 to n do
if (a[i]<=a[j]) and (b[j]>x) then begin
x:=b[j];
c[i]:=j;
b[i]:=x+1;
end;
end;
x:=b[1];f:=1;
for i:=2 to n do
if b[i]>x then begin
x:=b[i];
f:=i;
end;
writeln (f2,b[f]);
while f<>0 do
begin
write (f2,a[f],' ');
f:=c[f];
end;
close (f1);
close (f2);
end.