#include int main() { int i, N, P[300001]; scanf("%d", &N); for (i = 1; i <= N; i++) scanf("%d", &(P[i])); int k = 0, l, r, m, dp[300001] = {}, lis[300001] = {}; for (i = 1; i <= N; i++) { l = 0; r = k; while (l < r) { m = (l + r + 1) / 2; if (dp[m] > P[i]) r = m - 1; else l = m; } if (l == k) k++; dp[l+1] = P[i]; lis[i] = l + 1; } int last[300001] = {}, ans = 0, flag[300001] = {}, num[300001] = {}; for (i = N, last[k] = N + 1; i >= 1; i--) { if (P[i] < last[lis[i]]) { num[lis[i]]++; flag[i] = 1; if (P[i] > last[lis[i]-1]) last[lis[i]-1] = P[i]; } } for (i = 1; i <= k; i++) if (num[i] == 1) ans++; printf("%d\n", ans); for (i = 1; i <= N; i++) if (num[lis[i]] == 1 && flag[i] != 0) printf("%d ", P[i]); printf("\n"); fflush(stdout); return 0; }