Problem:
Score : 500 points

Problem Statement

You are given a sequence with N integers: A = \{ A_1, A_2, \cdots, A_N \}.
For each of these N integers, we will choose a color and paint the integer with that color. Here the following condition must be satisfied:

If A_i and A_j (i < j) are painted with the same color, A_i < A_j.

Find the minimum number of colors required to satisfy the condition.

Constraints

1 \leq N \leq 10^5

0 \leq A_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
A_1
:
A_N

Output

Print the minimum number of colors required to satisfy the condition.

Sample Input 1

5
2
1
4
5
3

Sample Output 1

2

We can satisfy the condition with two colors by, for example, painting 2 and 3 red and painting 1, 4, and 5 blue.

Sample Input 2

4
0
0
0
0

Sample Output 2

4

We have to paint all the integers with distinct colors.

Solution in C# (Mono 4.6.2.0) (judge verdict: Accepted)

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

class TEST{
	static void Main(){
		Sol mySol =new Sol();
		mySol.Solve();
	}
}

class Sol{
	public void Solve(){
		
		var ST = new SegTreeMax(N);
		for(int i=0;i<N;i++){
			ST.Update(i, A[i]);
		}
		int cnt = 0;
		for(int i=0;i<N;i++){
			if(ST.At(i) == -1) continue;
			int now = i;
			while(true){
//Console.WriteLine("now:{0}, i:{1}",now, i);
				if(ST.QueryMax(now, N) == A[now]){
					cnt++;
					ST.Update(now, -1);
					break;
				}
				int l = now + 1, r = N;
				while(r - l > 1){
					int c = (r + l) / 2;
					if(ST.QueryMax(l, c) > A[now]){
						r = c;
					} else {
						l = c;
					}
				}
				ST.Update(now, -1);
				now = l;
			}
		}
		Console.WriteLine(cnt);
		
		
	}
	int N;
	int[] A;
	public Sol(){
		N = ri();
		A = new int[N];
		for(int i=0;i<N;i++) A[i] = ri();
	}

	static String rs(){return Console.ReadLine();}
	static int ri(){return int.Parse(Console.ReadLine());}
	static long rl(){return long.Parse(Console.ReadLine());}
	static double rd(){return double.Parse(Console.ReadLine());}
	static String[] rsa(char sep=' '){return Console.ReadLine().Split(sep);}
	static int[] ria(char sep=' '){return Array.ConvertAll(Console.ReadLine().Split(sep),e=>int.Parse(e));}
	static long[] rla(char sep=' '){return Array.ConvertAll(Console.ReadLine().Split(sep),e=>long.Parse(e));}
	static double[] rda(char sep=' '){return Array.ConvertAll(Console.ReadLine().Split(sep),e=>double.Parse(e));}
}


class SegTreeMax{
	//segment Tree 
	// 0-origin
	protected long[] Data;	
	protected long Inf;	
	protected int N;		 
	protected int n;		
	
	public SegTreeMax(int n_){
		N = n_; Inf = -(long)1e18;
		n = 1;
		while(n < n_) n *= 2;
		Data = new long[2 * n - 1];
		for(int i=0; i < 2 * n - 1; i++) Data[i] = Inf;
	}
	
	//	   0
	//	 1   2
	//	3 4 5 6
	public void Update(int k,long a){
		k += n - 1;	
		Data[k] = a;
		while(k > 0){
			k = (k - 1) / 2;
			Data[k] = Math.Max(Data[k * 2 + 1], Data[k * 2 + 2]);
		}
	}
	
	
	public long QueryMax(int a, int b){
		return Query(a, b, 0, 0, n);
	}
	//RMQ; [a,b)
	public long Query(int a,int b,int k,int l,int r){
		if(r <= a || b <= l) return Inf;
		if(a <= l && r <= b) return Data[k];
		
		var vl = Query(a, b, k * 2 + 1, l, (l + r) / 2);
		var vr = Query(a ,b, k * 2 + 2, (l + r) / 2, r);
		return Math.Max(vl, vr);
	}
	
	public long At(int idx){
		return Data[idx + n - 1];
	}
	
	public long Max{
		get{ return Data[0];}
	}
	
	
	public void Dump(){
		Console.WriteLine();
		int h = 0;
		int cnt = 0;
		for(int i=0;i<Data.Length;i++){
			Console.Write("{0} ", Data[i]);
			cnt++;
			if(cnt == 1 << h){
				cnt = 0;
				h++;
				Console.WriteLine();
			}
		}
	}
	
}