-- ALDS1_1_D Maximum Profit

import Data.Array

main = interact $ (flip (++) "\n") . show . (\(n:xs) -> greatestDiff (take n xs)) . map read . lines

leasts xs = leasts' xs (maxBound :: Int)
leasts' [] _ = []
leasts' (x:xs) g = if x < g then x:(leasts' xs x) else g:(leasts' xs g)

greatests :: [Int] -> [Int]
greatests = reverse . flip greatests' (minBound :: Int) . reverse
greatests' [] _ = []
greatests' (x:xs) l = if x > l then x:(greatests' xs x) else l:(greatests' xs l)

greatestDiff xs = let ls = leasts xs
                      gs = greatests xs
                  in  maximum [gs!!j - ls!!i | (j, i) <- zip [1..(length gs - 1)] [0..(length gs - 2)]]