import Data.List (uncons)
import Data.Maybe (fromJust)

solve [_] _ _ d = d
solve (a:as) l r d = solve as (l+a) (r-a) $ if n < d then n else d
  where n = abs ((l+a) - (r-a))

main :: IO ()
main = do
  _ <- read <$> getLine :: IO Int
  (a,as) <- fromJust.uncons.map read.words <$> getLine :: IO (Integer,[Integer])
  print $ solve as a (sum as) $ abs (sum as-a)
