step_sum::[Int] -> [Int]
step_sum [] = []
step_sum (x:xs) = x:(map (x+) (step_sum xs) )




--今のカウント手数、前の値、食べるリスト
check::Int -> Int -> [Int] -> Int
check st _   [] = st
check st pre xs = case ((pre > 0),((head xs) > 0)) of
                  (True,True)  -> let dec = (head xs)+1 in check (dec+st) (-1) (map (\x-> x-dec) (tail xs))
                  (True,False) -> check st (head xs) (tail xs)
                  (False,True) -> check st (head xs) (tail xs)
                  (False,False)-> let inc = 1-(head xs) in check (inc+st)   1  (map (\x-> x+inc) (tail xs))

solver::[Int]->Int
solver xs = let steps = step_sum xs in
            check 0 (head steps) (tail steps)

main::IO()
main=do
  _<-getLine
  datc<-getLine
  print (solver (map read (words datc)))