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: Accepted)

import Data.Array.ST
import Data.Array
import Data.STRef
import Control.Monad.ST
import Control.Monad

--lは含まない、rは含む。
bin_ser_STA2::Int->STArray s Int Int ->(Int,Int)-> ST s Int
bin_ser_STA2 v ar (l,r) = if (l+1) == r then return r else do
  let mid = div (l+r) 2
  v_m<-readArray ar mid
  let (l', r') = if  v > v_m then (mid,r) else (l,mid)
  ans<-bin_ser_STA2 v ar (l',r')
  return ans

test_bin_STA2::[Int]->Int->Int
test_bin_STA2 ls v = runST $ do
  sta<-thaw (listArray (1,length ls) ls)
  a<-bin_ser_STA2 v sta (1,length ls)
  return a

ins_write_STA::Int->Int->STArray s Int Int ->ST s ()
ins_write_STA v len ar = do
  idx<-bin_ser_STA2 v ar (0,len)
  writeArray ar idx v
  return ()

test_ins_STA2::[Int]->Int->Array Int Int
test_ins_STA2 ls v = runSTArray $ do
  sta<-thaw (listArray (1,length ls) ls)
  ins_write_STA v (length ls) sta
  return sta

add_STA::Int->Int->STArray s Int Int -> ST s ()
add_STA v len ar = do
  writeArray ar (len+1) v
  return ()

--更新関数：Aの新規値、今のLIS長（STAの有効最大idx）、STA、アクション
update::Int->STArray s Int Int -> STRef s Int -> ST s ()
update v ar len_pt = do
  len <- readSTRef len_pt
  if len == 0 then do
                writeArray ar 1 v
                writeSTRef len_pt 1
                return ()
              else do
                top<-readArray ar len
                if top < v then do
                            writeSTRef len_pt (len+1)
                            add_STA v len ar
                            return ()
                           else do
                            ins_write_STA v len ar
                            return ()

test_update::[Int]->Int->Int->Array Int Int
test_update ls len v = runSTArray $ do
  sta<-thaw (listArray (1,length ls) ls)
  len_pt<-newSTRef len
  update v sta len_pt
  return sta

solver::[Int] -> Int -> Int
solver ls size = runST $ do
  ar<-newArray (1,size) 0
  len_pt<-newSTRef 0
  forM_ ls (\v -> update v ar len_pt)
  len'<-readSTRef len_pt
  return len'


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

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