{-# OPTIONS_GHC -O2 #-}
import Control.Applicative
import Data.Array.IO
import Control.Monad (mapM_)
import Data.IORef
import qualified Data.Set as Set (fromList,insert,empty,dropWhileAntitone)

data Set a    = Bin {-# UNPACK #-} !Size !a !(Set a) !(Set a)
              | Tip

type Size     = Int

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
        arrtolist <- getElems arr
        let set = Set.fromList [Set.insert (arrtolist !! (x-1)) Set.empty | x <- [hikaku_moto_Idx..(len)]]
        print set
        (hikaku_moto,_) <- readArray arr hikaku_moto_Idx
        let set' = Set.dropWhileAntitone ((>hikaku_moto) . fst) set
        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 ()