main = do
  n <- getLine
  a <- (map read . words) <$> getLine
  putStrLn .show $ solve a

solve:: [Int] -> Int
solve a = solve' a aa bb where
  (bb:aa) = a

solve' a aa bb = cho (foldl (+) 0 a) bb aa

cho:: Int -> Int -> [Int] -> Int
cho t s [] = s
cho t s (a:as)
  | cd < nd = cd
  | otherwise = cho t (s + a) as
  where
    cd = abs $ t - s - s
    nd = abs $ cd - a - a
