import           System.IO
import           Control.Monad
import           Control.Applicative
import           Control.Monad.State

f i =
  (state $ \(x, n) -> (i, ((max x (i - n)), (min n i)))) :: State (Int, Int) Int
main = do
  n    <- readLn
  list <- replicateM n (readLn :: IO Int)
  let listS = sequence $ map f list
  print $ fst $ execState listS (minBound, maxBound)

