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: Wrong Answer)

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


public struct Pair
{
	public int Index;
	public int A;

	public Pair(int index, int a)
	{
		Index = index;
		A = a;
	}
}


public class E
{
	public static void Main()
	{
		int N = GetInt();
		int[] A = new int[N + 1];
		for(int i = 1; i <= N; i++)
			A[i] = GetInt();

		int ans = 0;
		Func<Pair, Pair, Pair> func = 
			(x, y) => x.A < y.A ? new Pair(x.Index, x.A) : new Pair(y.Index, y.A);
		var bit = new BinaryIndexedTree<Pair>(N, new Pair(-1, int.MaxValue), func);
		for(int i = 1; i <= N; i++)
			bit.Update(i, new Pair(i, int.MaxValue / 2));

		for(int i = 1; i <= N; i++)
		{
			var minPair = bit.Query(N);
			if(A[i] <= minPair.A)
			{
				ans++;
				bit.Update(minPair.Index, new Pair(minPair.Index, A[i]));
			}
			else
			{
				bit.Update(i, new Pair(i, A[i]));
			}
		}

		Console.WriteLine(ans);
	}
}


public class BinaryIndexedTree<T>
{
	private int N;
	private T Identity;
	private Func<T, T, T> Func;
	private T[] bit;

	public BinaryIndexedTree(int n, T identity, Func<T, T, T> func)
	{
		N = n;
		Identity = identity;
		Func = func;
			
		bit = new T[N + 1];  // 1-index
		for(int i = 0; i <= N; i++)
			bit[i] = Identity;
	}

	public void Update(int i, T x)
	{
		while(i <= N)
		{
			bit[i] = Func(bit[i], x);
			i += i & -i;
		}
	}

	public T Query(int i)
	{
		T s = Identity;
		while(i > 0)
		{
			s = Func(s, bit[i]);
			i -= i & -i;
		}
		return s;
	}

	/* bit must be monotonically increasing */
	public int LowerBound(T key, Comparison<T> comp, Func<T, T, T> inv)
	{
		if(comp(key, Identity) <= 0)
			return 0;

		int pow = 1;
		while(pow <= N)
			pow <<= 1;
		pow >>= 1;

		int x = 0;
		for(int k = pow; k > 0; k >>= 1)
		{
			if((x + k <= N) && (comp(bit[x + k], key) < 0))
			{
				key = inv(key, bit[x + k]);
				x += k;
			}
		}

		return x + 1;
	}
}


public static class MyIO
{
	private static string[] args = null;
	private static int num = -1;
	private static int used = -1;

	private static string getArg()
	{
		if(used == num)
		{
			args = Console.ReadLine().Split(' ');
			num = args.Length;
			used = 0;
		}
		return args[used++];
	}

	public static int GetInt(){ return int.Parse(getArg()); }
	public static long GetLong(){ return long.Parse(getArg()); }
	public static double GetDouble(){ return double.Parse(getArg()); }
	public static string GetString(){ return getArg(); }
}