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;
using System.Linq;

namespace DPL_1D_1{
    public class Program{
        public static void Main(string[] args){
            var input = Console.ReadLine();
            var lis = new int[100000];
            lis[0] = int.MaxValue;
            var length = 1;
            while((input = Console.ReadLine()) != null){
                var a = int.Parse(input);
                if(lis[length - 1] < a){
                    lis[length++] = a;
                    continue;
                }

                var left = 0;
                var right = length;
                while(left < right) {
                    var mid = (left + right) / 2;
                    if(lis[mid] == a) break;
                    if(a < lis[mid]) right = mid;
                    else left = mid + 1;
                }
                lis[(left + right) / 2] = a;
            }
            Console.WriteLine(length);
        }
    }
}