program Lis_Mpq;
var
i,n,m,Max_Len,Max_Ans:longint;
a,f,Ans:array[0..100000] of longint;
procedure init;
var
i:longint;
begin
readln(n);
for i:=1 to n do readln(a[i]);
Max_Len:=0; Max_Ans:=0;
end;
procedure Lis;
var
i,Min,Max,Len:longint;
begin
Len:=0;
for i:=n downto 1 do
begin
Min:=1; Max:=Len;
while Min<=Max do
begin
m:=(Min+Max) div 2;
if a[i]<a[f[m]] then
Min:=M+1 else
Max:=M-1;
end;
f[Min]:=i;
if Min>Len then Len:=Min;
if Min>1 then Ans[i]:=f[Min-1] else Ans[i]:=0;
if Len>Max_Len then
begin
Max_Len:=Len;
Max_Ans:=i;
end;
end;
end;
procedure print;
var
i:longint;
begin
writeln('Max_Len=',Max_Len);
for i:=1 to Max_Len do
begin
write(a[Max_Ans],' ');
Max_Ans:=Ans[Max_Ans];
end;
writeln;
end;
begin
assign(input,'Lis.in'); reset(input);
assign(output,'Lis.out'); rewrite(output);
init;
Lis;
print;
close(input); close(output);
end.