program maxxl;
Type
fxz=array[1..10000] of integer;
Var
f1,f2:text;
i,j,max,n:longint;
ans,next,city:fxz;
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,city[i]);ans[i]:=1;end;
for i:=n-1 downto 1 do
begin
max:=0;
for j:=i+1 to n do
begin
if (city[j]>=city[i])and(ans[j]>max) then
begin next[i]:=j;max:=ans[j];end;
end;
ans[i]:=ans[i]+max;
end;
max:=0;j:=0;
for i:=1 to n do
if ans[i]>max then begin max:=ans[i];j:=i;end;
writeln(f2,ans[j]);
write(f2,city[j],' ');
while next[j]<>0 do
begin
j:=next[j];
write(f2,city[j],' ');
end;
close(f1);close(f2);
end.