Problem:
Score : 500 points

Problem Statement

You are given a sequence with N integers: A = \{ A_1, A_2, \cdots, A_N \}.
For each of these N integers, we will choose a color and paint the integer with that color. Here the following condition must be satisfied:

If A_i and A_j (i < j) are painted with the same color, A_i < A_j.

Find the minimum number of colors required to satisfy the condition.

Constraints

1 \leq N \leq 10^5

0 \leq A_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
A_1
:
A_N

Output

Print the minimum number of colors required to satisfy the condition.

Sample Input 1

5
2
1
4
5
3

Sample Output 1

2

We can satisfy the condition with two colors by, for example, painting 2 and 3 red and painting 1, 4, and 5 blue.

Sample Input 2

4
0
0
0
0

Sample Output 2

4

We have to paint all the integers with distinct colors.

Solution in Haskell (GHC 7.10.3) (judge verdict: Time Limit Exceeded)

{-# LANGUAGE BangPatterns #-}
import Control.Monad
import Data.Maybe
import Debug.Trace
import Data.List
import qualified Data.Map.Strict as Map
import qualified Data.IntMap.Strict as IntMap
import qualified Data.Set as Set
import qualified Data.IntSet as IntSet
import Data.Functor
import Data.Array
-- import Data.Array.Unboxed
import Control.Monad.ST
import Data.Array.ST
import qualified Data.ByteString.Char8 as B
import Data.Maybe (fromJust)

readInt = fst . fromJust . B.readInt
readInts = map (fst . fromJust . B.readInt) . B.words <$> B.getLine :: IO [Int]
readIntegers = map (fst . fromJust . B.readInteger) . B.words <$> B.getLine :: IO [Integer]
readVInts = map (fst . fromJust . B.readInt) . B.lines <$> B.getContents

main :: IO ()
main = do
  n <- readLn :: IO Int
  -- as <- replicateM n $ readLn :: IO [Int]
  as <- readVInts
  print $ solve'' n as

solve'' n (a:as) = length $ foldl' go [a] as
  where
    go bs a
      | a <= mn = insert a bs
      | otherwise = replaceUpperBound (<a) a bs
      -- | otherwise = insert a $ deleteUpperBound (< a) bs
      where
        mn = head bs   

replaceUpperBound :: (a -> Bool) -> a -> [a] -> [a]
replaceUpperBound pred new (a:b:as) 
  | pred a && not (pred b) = new:b:as
  | not (pred a) = a:b:as
  | otherwise = a:(replaceUpperBound pred new (b:as))
replaceUpperBound pred new (a:[]) = if pred a then [new] else [a]
replaceUpperBound _ _ [] = []