import bisect import bisect #i番目までの増加列の長さを返す def LIS(lis): c=len(lis) dp=[lis[0]] this=0 ansl=[] for i in range(c): a=lis[i] if a>dp[-1]: dp.append(a) this=len(dp)-1 else: x=bisect.bisect_left(dp,a) this=x dp[x]=a ansl.append(this) return ansl N=int(input()) P=list(map(int,input().split())) L=LIS(P) this=0 K=list() K2=list() for i in range(len(L)): if L[i]==this: K.append(i) this+=1 this=max(L) for i in range(len(L)-1,-1,-1): if L[i]==this: K2.append(i) this-=1 K2=set(K2) ansl=list() for i in K: if i in K2: ansl.append(P[i]) print(len(ansl)) print(*ansl)