import Data.IORef
import Control.Monad
import System.IO.Unsafe
 
main :: IO()
main = print.unsafePerformIO.solve.map read.lines.unsafePerformIO $ getContents

solve :: [Int] -> IO Int
solve (_:x:y:xs) = do
                    let a = unsafePerformIO $ newIORef (y - x)
                        b = unsafePerformIO $ newIORef (min x y)
                    forM_ xs $ \z -> do
                      a' <- readIORef a
                      b' <- readIORef b
                      when (z - b' > a') $ writeIORef a (z - b')
                      when (z < b')      $ writeIORef b z
                    readIORef a