import qualified Data.Map as Map
import Data.List

(|>) :: a -> (a -> b) -> b
infixl 1 |>
x |> f = f x

($>) :: (Functor m) => m a -> (a -> b) -> m b
infixl 1 $>
x $> f = fmap f x

calc :: Int -> (Int, Int) -> Int
calc x (num, len)
    | len >= num = x + len - num
    | otherwise  = x + len

main = do
    getLine
    ax <- getLine $> words $> map read :: IO [Int]
    let hi = Map.empty :: Map.Map Int Int
    let xs = ax |> foldl' (\h k -> Map.insertWith (+) k 1 h) hi |> Map.toList
    xs |> foldl' calc 0 |> print

