import qualified Data.Vector.Unboxed as U
import Control.Monad
import qualified Data.IntMap as M

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 =
    let set' = M.filterWithKey (\k _ -> k<a) set in
        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.insert x (v+1) set



