import sys input = lambda: sys.stdin.readline().rstrip() ii = lambda: int(input()) mi = lambda: map(int, input().split()) li = lambda: list(mi()) INF = 2 ** 63 - 1 mod = 998244353 import bisect n = ii() a = li() def LIS1(a): dp = [INF] * n ind = [-1] * n for i in range(n): now = bisect.bisect_left(dp, a[i]) dp[now] = a[i] ind[i] = now k = max(ind) + 1 ans = [0] * k k -= 1 for i in range(n - 1, -1, -1): if ind[i] == k: ans[k] = i k -= 1 return ans def LDS(a): b = [-i for i in a[::-1]] dp = [INF] * n ind = [-1] * n for i in range(n): now = bisect.bisect_left(dp, b[i]) dp[now] = b[i] ind[i] = now k = max(ind) + 1 ans = [0] * k k -= 1 for i in range(n - 1, -1, -1): if ind[i] == k: ans[k] = n - i - 1 k -= 1 return ans ans1 = set(LIS1(a)) ans2 = set(LDS(a)) ans = ans1 & ans2 ans = list(sorted(ans)) print(len(ans)) print(*[a[i] for i in ans])