import qualified Data.ByteString.Char8 as BSC8
import qualified Data.Vector.Unboxed   as VU
import qualified Control.Arrow         as Arrow

type Parser a = BSC8.ByteString -> Maybe (a, BSC8.ByteString)
parseInt :: Parser Int
parseInt = fmap (Arrow.second BSC8.tail) . BSC8.readInt
parseM :: Int -> IO (VU.Vector Int)
parseM m = VU.unfoldrN m parseInt <$> BSC8.getLine

main :: IO ()
main = do
  m <- readLn :: IO Int
  xs <- parseM m
  print $ sum $ solver m xs

solver :: Int -> VU.Vector Int -> [Int]
solver n xs = step 0 0 []
  where
    step i tmp ys
      | i == n        = ys
      | current > tmp = step (i + 1) current ys
      | otherwise     = step (i + 1) tmp ((tmp - current) : ys)
      where
        current = xs VU.! i
