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