Longest Increasing Subsequence 3

Given a sequence of up to 10^6 integers, find the length of the longest strictly increasing subsequence.

Medium5Dynamic programmingBinary searchNo attempts yetTime limit3sMemory 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 you get by picking some elements of AA and keeping their original order. An increasing subsequence is one whose values grow from left to right, so the same value may not appear twice in it.

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

Input

The first line contains the size NN of the sequence AA. (1N1061 \le N \le 10^6)

The second line contains the integers A1,A2,,ANA_1, A_2, \dots, A_N of the sequence AA, separated by spaces. (109Ai109-10^9 \le A_i \le 10^9)

Output

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