main = do
	n <- read <$> getLine :: IO Int
	sticks <- map read.words <$> getLine :: IO [Int]
	putStrLn.show $ cost (head sticks) (last sticks) (tail $ init sticks)

cost a b [] = a-b
cost a b sticks = if a < b
	then cost (a+head sticks) b (tail sticks)
	else cost a (b+last sticks) (init sticks)