// Arup Guha
// 11/17/2019
// Solution to 2017 HiQ Challenge Open Problem: Greedily Increasing Sequence

import java.util.*;

public class greedilyincreasing {

	public static void main(String[] args) {
	
		Scanner stdin = new Scanner(System.in);
		int n = stdin.nextInt();
		
		// Add 1st item into the list, store last currently in list.
		ArrayList<Integer> res = new ArrayList<Integer>();
		res.add(stdin.nextInt());
		int cur = res.get(0);
	
		// Read the rest.
		for (int i=1; i<n; i++) {
			int next = stdin.nextInt();
			
			// Add this if it's the biggest we've seen so far.
			if (next > cur) {
				res.add(next);
				cur = next;
			}
		}
		
		// Ta da!
		System.out.println(res.size());
		for (int i=0; i<res.size(); i++)
			System.out.print(res.get(i)+" ");
		System.out.println();
	}
}