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: Accepted)

using System.Collections.Generic;
using System;

public class Hello
{
    public const int INF = 1000000001;
    public static void Main()
    {
        var n = int.Parse(Console.ReadLine().Trim());
        var a = new int[n];
        for (int i = 0; i < n; i++) a[i] = int.Parse(Console.ReadLine().Trim());
        Console.WriteLine(goDP(a));
    }
    public static int goDP(int[] a)
    {
        var n = a.Length;
        var dp = new int[n];
        for (int i = 0; i < n; i++) dp[i] = INF;
        for (int i = 0; i < n; i++)
        {
            var w = LowerBound(dp, a[i]);
            dp[w] = a[i];
        }
        return LowerBound(dp, INF);
    }
    public static int LowerBound<T>(T[] arr, int start, int end, T value, IComparer<T> comparer)
    {
        var low = start;
        var high = end;
        int mid;
        while (low < high)
        {
            mid = ((high - low) >> 1) + low;
            if (comparer.Compare(arr[mid], value) < 0) low = mid + 1;
            else high = mid;
        }
        return low;
    }
    public static int LowerBound<T>(T[] arr, T value) where T : IComparable
    {
        return LowerBound(arr, 0, arr.Length, value, Comparer<T>.Default);
    }
}