import java.util.*;
import java.lang.*;
import java.io.*;

class LISSubsequence {

    private void findLIS(int[] inputArr) {
        List[] listOfSubs = new ArrayList[inputArr.length];    //Max different subsequences in an array would be N
        //To store the max value of each of the subsequences found yet
        List<Integer> maxValList = new ArrayList<Integer>();
        listOfSubs[0] = new ArrayList<Integer>();
        listOfSubs[0].add(inputArr[0]);    //Add the first element of the array to the list
        maxValList.add(inputArr[0]);
        for (int i=1;i<inputArr.length;i++) {
            boolean flag = false;
            int iter=0;
            //Compare inputArr[i] with the maxVal of each subsequence
            for (int j=0; j<maxValList.size(); j++) {
                if (inputArr[i]>maxValList.get(j)) {
                    maxValList.set(j, inputArr[i]); //Update the maxVal in the corresponding position in the list
                    listOfSubs[j].add(inputArr[i]);
                    flag = true;
                }
                iter = j;
            }
            //If inputArr[i] is not greater than any previous values add it to a new list
            if (!flag) {
                maxValList.add(inputArr[i]);
                listOfSubs[iter+1] = new ArrayList<Integer>();
                listOfSubs[iter+1].add(inputArr[i]);
            }
        }

        int max=0, iter=0, index=0;
        for (List<Integer> lst : listOfSubs) {
            if (lst!=null && lst.size() > max) {
                max = lst.size();
                index=iter;
            }
            iter++;
        }

        //Print the longest increasing subsequence found
        System.out.println("The Longest Increasing Subsequence is of length " + listOfSubs[index].size() +
                " and is as follows:");
        for (int i=0;i<listOfSubs[index].size();i++) {
            System.out.print(listOfSubs[index].get(i) + " ");
        }
    }

    public static void main (String[] args) throws IOException {
        LISSubsequence lis = new LISSubsequence();
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        //int T = Integer.parseInt(br.readLine());
        int T=1;
        while (T-- > 0) {
            int N = Integer.parseInt(br.readLine());
            //feed elements into the array
            int[] inputArr = new int[N];
            //String[] strArr = (br.readLine()).split(" ");
            for (int i=0;i<N;i++) {
                //inputArr[i] =  Integer.parseInt(strArr[i]);
                inputArr[i] =  Integer.parseInt(br.readLine());
            }
            lis.findLIS(inputArr);
        }
    }
}