import Control.Monad
import Control.Applicative

maxProfit :: [Int] -> Int -> Int -> Int -> Int
maxProfit [x] _ maxProf _ = maxProf
maxProfit (x:xs) lowest maxProf m
    | x > lowest  = maxProfit xs lowest maxProf m
    | otherwise   =  maxProfit xs' x maxProf' m
    where maxProf' = max (foldr(max) 0 xs - x) maxProf
          xs'      = (filter ( \y -> y < x || y > m + maxProf') xs)
    
main = do
    s <- (drop 1.map read.lines) <$> getContents ::IO[Int]
    putStrLn $ show $ maxProfit s (s!!0) (-1000000000) (foldr (min) 0 s)