Longest Increasing Subsequence 2

Given up to 1,000,000 numbers, compute the length of the longest strictly increasing subsequence.

Medium4Binary searchDynamic programmingInterviewNo attempts yetTime limit1sMemory limit512 MB

Problem

Given a sequence AA, write a program that computes the length of its longest increasing subsequence.

A subsequence of AA is what remains after deleting zero or more elements and keeping the rest in their original order. A subsequence is increasing when every element is strictly smaller than the element right after it. Two elements with the same value cannot sit next to each other in one increasing subsequence.

For example, if A={10,20,10,30,20,50}A = \{10, 20, 10, 30, 20, 50\}, the longest increasing subsequence is {10,20,30,50}\{10, 20, 30, 50\} and its length is 44.

Input

The first line contains the size NN of the sequence AA. (1N10000001 \le N \le 1\,000\,000)

The second line contains A1,A2,,ANA_1, A_2, \dots, A_N, separated by single spaces. (1Ai10000001 \le A_i \le 1\,000\,000)

Output

Print the length of the longest increasing subsequence of AA on the first line.