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)

{-# OPTIONS_GHC -O2 #-}
import Control.Applicative
import Data.Array.IO
import Control.Monad (mapM_)
import Data.IORef

main = do
  len <- (read :: String -> Int) <$> getLine
  xs <- map (read :: String -> Int) . lines <$> getContents
  arr <- newListArray (1,len) (zip xs [1,1..]) :: IO (IOArray Int (Int,Int))
  syorinaiyou arr len
  lengthLIS <- newIORef 0
  ans arr lengthLIS
  print =<< readIORef lengthLIS

ans :: IOArray Int (Int, Int) -> IORef Int -> IO ()
ans arr lengthLIS = do
  xs <- getElems arr
  writeIORef lengthLIS (maximum $ map snd xs)

syorinaiyou :: IOArray Int (Int, Int) -> Int -> IO ()
syorinaiyou arr len = do mapM_ (keisan arr) (reverse [1..len])
    where
      keisan :: IOArray Int (Int, Int) -> Int -> IO ()
      keisan arr hikaku_moto_Idx = do
          mapM_ (seisa' arr) [hikaku_moto_Idx..(len)]
        where
          seisa' :: IOArray Int (Int, Int) -> Int -> IO ()
          seisa' arr hikaku_saki_Idx = do
              (hikaku_moto,_) <- readArray arr hikaku_moto_Idx
              (hikaku_saki,_) <- readArray arr hikaku_saki_Idx
              if hikaku_moto < hikaku_saki then hikaku
                         else return ()
            where
              hikaku = do
                (_,hikaku_saki_LIS) <- readArray arr hikaku_saki_Idx
                (hikaku_moto,hikaku_moto_LIS_zantei) <- readArray arr hikaku_moto_Idx
                if hikaku_saki_LIS + 1 > hikaku_moto_LIS_zantei
                   then do
                     writeArray arr hikaku_moto_Idx (hikaku_moto, (hikaku_saki_LIS + 1))
                   else return ()