import bisect n = int(input()) P = list(map(int,input().split())) inf = 10**10 min_lis = [inf]*(n+1) min_id = [] def lis(P): dp = [inf]*(n+1) id = [] for i,p in enumerate(P): ind = bisect.bisect_left(dp,p) dp[ind] = p id.append(ind) m = max(id) ans = [] for i in range(n)[::-1]: if id[i] == m: ans.append(P[i]) m -= 1 return ans[::-1] ans_min = lis(P) ans_max = lis([-p for p in P][::-1]) ans_max = [-p for p in ans_max][::-1] ans = [] for mi,ma in zip(ans_min,ans_max): if mi == ma: ans.append(mi) print(len(ans)) print(*ans)