import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; public class Main { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); String[] sa = br.readLine().split(" "); int[] a = new int[n]; for (int i = 0; i < n; i++) { a[i] = Integer.parseInt(sa[i]); } br.close(); int[] dp = new int[n + 1]; List> list = new ArrayList<>(n + 1); list.add(new ArrayList<>()); for (int i = 1; i <= n; i++) { dp[i] = Integer.MAX_VALUE; list.add(new ArrayList<>()); } for (int i = 0; i < n; i++) { int idx = Arrays.binarySearch(dp, a[i]); if (idx < 0) idx = ~idx; dp[idx] = a[i]; list.get(idx).add(i); } int lis = Arrays.binarySearch(dp, Integer.MAX_VALUE - 1); if (lis < 0) { lis = ~lis; lis--; } List list2 = list.get(lis); int l = 0; int r = list2.size() - 1; List ans = new ArrayList<>(); int cnt = 0; if (r == 0) { ans.add(a[list2.get(0)]); cnt++; } for (int i = lis - 1; i > 0; i--) { List cur = list.get(i + 1); List next = list.get(i); int max = a[cur.get(l)]; int min = a[cur.get(r)]; int nr = -1; int nl = -1; for (int j = next.size() - 1; j >= 0; j--) { if (nr == -1) { if (next.get(j) < cur.get(r) && a[next.get(j)] < min) { nr = j; nl = nr; } } else { if (a[next.get(j)] < max) { nl = j; } else { break; } } } if (nl == nr) { ans.add(a[next.get(nl)]); cnt++; } l = nl; r = nr; } StringBuilder sb = new StringBuilder(); if (cnt > 0) { Collections.reverse(ans); for (int i : ans) { sb.append(i).append(' '); } sb.deleteCharAt(sb.length() - 1); } System.out.println(cnt); System.out.println(sb.toString()); } }