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

{-# 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) = go (IntMap.singleton a 1) (IntSet.singleton a) as
  where
    go :: IntMap.IntMap Int -> IntSet.IntSet -> [Int] -> Int
    go mp _ [] = sum $ IntMap.elems mp
    go mp bs (a:as)
      | a <= mn = go (IntMap.insertWith (+) a 1 mp) (IntSet.insert a bs) as
      | otherwise = go (IntMap.insertWith (+) a 1 newmp) (IntSet.insert a newbs) as
      where
        mn = fromJust $ IntSet.lookupGT (-1) bs
        m = fromJust $ IntSet.lookupLT a bs
        newmp = IntMap.adjust (subtract 1) m mp
        newbs = if newmp IntMap.! m > 0 
          then bs
          else IntSet.delete m bs