Problem:
Longest Increasing Subsequence

For a given sequence A = {a0, a1, ... , an-1}, find the length of the longest increasing subsequnece (LIS) in A.

An increasing subsequence of A is defined by a subsequence {ai0, ai1, ... , aik} where 0 ≤ i0 < i1 < ... < ik < n and ai0 < ai1 < ... < aik.

Input

n
a0
a1
:
an-1

In the first line, an integer n is given. In the next n lines, elements of A are given.

Output

The length of the longest increasing subsequence of A.

Constraints

1 ≤ n ≤ 100000

0 ≤ ai ≤ 109

Sample Input 1

5
5
1
3
2
4

Sample Output 1

3

Sample Input 2

3
1
1
1

Sample Output 2

1

Solution in C# (judge verdict: Runtime Error)

using System;
using System.Collections.Generic;
using System.Linq;

static class LIS {
    struct D : IComparable<D> {
        public int value;
        public int index;
        public int CompareTo(D o) {
            return value.CompareTo(o.value);
        }
    }

    public static List<int> Do(List<int> xs) {
        var lis = new List<D>();
        lis.Add(new D { value=xs[0], index=0 });

        var prev = new int[xs.Count];
        for (int i = 1; i < xs.Count; i++) {
            var d = new D { value=xs[i], index=i };
            int p = lis.BinarySearch(d);
            if (p < 0) {
                int m = ~p;
                if (m == lis.Count) lis.Add(d);
                else lis[m] = d;

                if (m == 0) prev[i] = -1;
                else prev[i] = lis[m-1].index;
            }
        }

        var ret = new List<int>();
        int index = lis.Last().index;
        while (index != -1) {
            ret.Add(xs[index]);
            index = prev[index];
        }
        ret.Reverse();
        return ret;
    }
}

class Program {
    static string ReadLine() { return Console.ReadLine(); }
    static int ReadInt() { return int.Parse(ReadLine()); }
    static int[] ReadInts() { return ReadLine().Split().Select(int.Parse).ToArray(); }
    static string[] ReadStrings() { return ReadLine().Split(); }

    static void Main() {
        int n = ReadInt();
        var xs = new List<int>();
        for (int i = 0; i < n; i++) {
            xs.Add(ReadInt());
        }

        var ans = LIS.Do(xs);
        Console.WriteLine(ans.Count);
    }
}