import Data.List

main = do
	n <- read <$> getLine :: IO Int
	sticks <- map read.words <$> getLine :: IO [Int]
	putStrLn.show $ cost $ map sum $ tails sticks

cost :: [Int] -> Int
cost (total:tables) = minimum $ map (\v -> abs (total-2*v) ) tables