main :: IO ()
main = do
  n <- readLn :: IO Int
  cd <- map read . words <$> getLine :: IO [Int] 
  print $ f cd []

f :: [Int] -> [Int] -> Int
f xs acc =
  let m = maximum xs
      t = takeWhile (/=m) xs
      d = tail $ dropWhile (/=m) xs
      s = sum (t ++ d)
   in if sum acc + m > s then abs ((sum acc)+m -s) else f (t++d) (m:acc)

