import Control.Monad
import Control.Applicative

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