// Arup Guha
// 2/10/2026
// Solution to Kattis Problem: Increasing Sequence

import java.util.*;

public class increasingsubsequence {
	
	public static void main(String[] args) {
		
		Scanner stdin = new Scanner(System.in);
		int n = stdin.nextInt();

		// Process all cases.
		while (n != 0) {
			
			// Read in values.
			int[] vals = new int[n];
			for (int i=0; i<n; i++)
				vals[i] = stdin.nextInt();
			
			// Get result.
			ArrayList<Integer> res = lis(vals);
			
			// Output the result.
			System.out.print(res.size());
			for (int i=0; i<res.size(); i++)
				System.out.print(" "+res.get(i));
			System.out.println();
			
			// Get next case.
			n = stdin.nextInt();
		}
	}

	// Returns the first lexicographical LIS of values.
	public static ArrayList<Integer> lis(int[] values) {
	
		// Set up.
		int n = values.length;
		int[] dp = new int[n];
		int[] prev = new int[n];
		Arrays.fill(prev, -1);
		dp[0] = 1;
		int max = 1;
		
		// Solve problem for sequences ending at index i.
		for (int i=1; i<n; i++) {
		
			dp[i] = 1;
			
			// Must be overwritten.
			int bestAns = values[i];
			
			// values[j] will be last term before values[i].
			for (int j=0; j<i; j++) {
			
				// Not an option.
				if (values[j] >= values[i]) continue;
				
				// Update if strictly better, or if same length but lexicographically first.
				if (dp[j]+1 > dp[i] || (dp[j]+1 == dp[i] && values[j]<bestAns)) {
				
					// Update my answer at index i.
					dp[i] = dp[j]+1;
					
					// Store best previous value I am building off of.
					bestAns = values[j];
					
					// Update where my previous value is.
					prev[i] = j;
				}
			}
			
			max = Math.max(max, dp[i]);
		} // endi
		
		// Store answer here.
		ArrayList<Integer> res = null ;
		
		for (int i=0; i<n; i++) {
			
			if (dp[i] != max) continue;
			
			// Build the potential answer.
			ArrayList<Integer> tmp = build(values, prev, i);
			
			// If this one's better, update.
			if (res == null || beat(tmp, res))
				res = tmp;
		}
		
		return res;
	}
	
	// Builds the LIS ending at index idx.
	public static ArrayList<Integer> build(int[] values, int[] prev, int idx) {
		
		// Add last item.
		ArrayList<Integer> res = new ArrayList<Integer>();
		res.add(values[idx]);
		
		// Use prev array to jump backwards and build sequence in reverse.
		while (prev[idx] != -1) {
			idx = prev[idx];
			res.add(values[idx]);
		}
		
		// Reverse it back into order and return.
		Collections.reverse(res);
		return res;
	}
	
	// Returns true iff a is lexicographically before b.
	public static boolean beat(ArrayList<Integer> a, ArrayList<Integer> b) {
		
		for (int i=0; i<a.size(); i++) {
			if (a.get(i) < b.get(i)) return true;
			if (a.get(i) > b.get(i)) return false;
		}
		
		return false;
	}
}