
import qualified Data.Vector.Unboxed as U
import qualified Data.Vector as V
import qualified Data.ByteString.Char8 as C
import qualified Data.Array.IO as A
import Debug.Trace
import Data.List
import Control.Monad
import Data.Ord
import qualified Data.IntSet as S
import qualified Data.IntMap as M

r :: Int -> IO (U.Vector Int)
r n = U.unfoldrN n (C.readInt . C.dropWhile (==' ')) <$> C.getLine

r' :: IO [Int]
r'  =   unfoldr    (C.readInt . C.dropWhile (==' ')) <$> C.getLine

rp :: IO (Int,Int)
rp  = toTup . unfoldr    (C.readInt . C.dropWhile (==' ')) <$> C.getLine

toTup [a,b] = (a,b)

main :: IO ()
main = do
    n <- readLn
    as <- replicateM n readLn :: IO [Int]
    let res = solve as (M.singleton (-1) 0)
    print res



solve :: [Int] -> M.IntMap Int -> Int
solve  []    set = M.foldl (+) 0 set
solve (a:as) set = --traceShow (a:as,set) $
    let set' = M.filterWithKey (\k _ -> k<a) set in
    --traceShow set' $
        if M.null set'
            then solve as (insOrInc set a)
            else    let (k,v) = M.findMax set'
                    in solve as (insOrInc (delOrDec set k) a)



delOrDec :: M.IntMap Int -> Int -> M.IntMap Int
delOrDec set x = M.update (\n -> if n>1 then Just (n-1) else Nothing) x set

insOrInc :: M.IntMap Int -> Int -> M.IntMap Int
insOrInc set x = case M.lookup x set of
            Nothing ->  M.insert x 1 set
            Just v ->  M.update (\n -> Just (n+1)) x set



