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 Haskell (judge verdict: Time Limit Exceeded)

import Data.Array
import Data.Array.ST
naive_rec::Array Int Int -> Int -> Int
naive_rec ar idx = if idx == 0 then 0 else maximum [(naive_rec ar j)+1|j<-[0..(idx-1)],(j==0)||(ar!j)<(ar!idx)]

naive_rec_solver::[Int]->Int->Int
naive_rec_solver ls size = let ar = listArray (1,size) ls in
                           maximum ( map (naive_rec ar) [0..size])


{-
STによる明示的な配列操作DP

まって、本当に再帰リストできない？lがあるものとして、これの前半しかつかわない。次は十分速いか？
-}

memo_ls::Array Int Int ->Int->[Int]
memo_ls ar size = 0:[ let prev = take k (zip (memo_ls ar size) [0..]) in
                      let cond = (\idx -> if idx == 0 then True else (ar!idx)<(ar!k)) in
                      maximum [l+1|(l,idx)<-prev,cond idx]
                    | k <-[1..size] ]

memo_ls_solver::[Int]->Int->Int
memo_ls_solver ls size = let ar = listArray (1,size) ls in
                         maximum (memo_ls ar size)

n_get::Int->IO [Int]
n_get 0 = return []
n_get n = do
  c<-getLine
  rest<-n_get (n-1)
  return ((read c):rest)

main::IO()
main=do
  nc<-getLine
  let n = read nc
  dat<-n_get n
  print (memo_ls_solver dat n)