import Data.STRef
import Control.Monad
import Control.Monad.ST
 
main :: IO()
main = getLine >> getContents >>= print.(\(x:y:xs) -> solve (y - x) (min x y) xs).map read.lines
 
solve :: Int -> Int -> [Int] -> Int
solve a b xs = runST $ do
                a' <- newSTRef a
                b' <- newSTRef b
                forM_ xs $ \z -> do
                    x <- readSTRef a'
                    y <- readSTRef b'
                    if (z - y > x) then writeSTRef a' (z - y) else return ()
                    if (z < y)     then writeSTRef b' z       else return ()
                readSTRef a'