import java.util.Arrays;

/**
 * Created by wigos on 04/05/2016.
 */
public class Main {

    public static int input[] = new int[] {
            5, 8, 11, 13, 9, 4, 1, 2, 0, 3, 7, 10, 12, 6
    };

    public static void main(String[] args) {
        int[] posledniPrvek = new int[input.length];
        int[] idxPosledniPrvek = new int[input.length];
        int[] predchudce = new int[input.length];

        for(int i = 0 ; i < input.length ; i++) {
            posledniPrvek[i] = Integer.MAX_VALUE;
        }

        int maxIdx = -1;

        for(int i = 0 ; i < input.length ; i++) {
            int index = Arrays.binarySearch(posledniPrvek, input[i]);

            if(index == -1) {
                if(posledniPrvek[0] > input[i]) {
                    posledniPrvek[0] = input[i];
                    idxPosledniPrvek[0] = i;
                    predchudce[i] = -1;

                    maxIdx = Math.max(maxIdx, 0);
                }
            } else {
                index = -(index + 2);
                if(posledniPrvek[index + 1] > input[i]) {
                    posledniPrvek[index + 1] = input[i];
                    idxPosledniPrvek[index + 1] = i;
                    predchudce[i] = idxPosledniPrvek[index];

                    maxIdx = Math.max(maxIdx, index + 1);
                }
            }
        }

        int current = idxPosledniPrvek[maxIdx];
        while(current != -1) {
            System.out.printf("%2d ", input[current]);
            current = predchudce[current];
        }
        System.out.println();
    }

}
