{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE ExplicitForAll #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE MultiWayIf #-}
{-# LANGUAGE Unsafe #-}
{-# LANGUAGE RecordWildCards #-}

import Data.Bits
import Data.List
import qualified Data.ByteString.Char8 as BS
import qualified Data.ByteString.Lazy.Char8 as BSL
import Data.IntMap.Strict (IntMap)
import qualified Data.IntMap.Strict as IMS
import Data.IntSet (IntSet)
import qualified Data.IntSet as IS
import qualified Data.Array.IArray as A
import qualified Data.Array.MArray.Safe as A
import qualified Data.Array.MArray as A
import Data.Array (Array)
import Data.Array.Unboxed (UArray)
import Data.Array.IArray (IArray)
import Data.Array.MArray.Safe (MArray)
import Data.Array.IO.Safe (IOArray, IOUArray)
import Data.Array.ST.Safe (STArray, STUArray, runSTArray, runSTUArray)
import qualified Data.Vector as V
import qualified Data.Vector.Unboxed as VU
import qualified Data.Vector.Unboxed.Mutable as VUM
import Control.Monad
import Control.Monad.ST
-- import Control.Monad.ST.Safe
import Control.Applicative
import Data.Maybe
import Data.Tuple
import Data.Ord
import Control.Monad.State.Strict
import Data.Int
import System.IO
import Data.Function
import GHC.Magic (oneShot)

main ::  IO ()
main = do
  Just (n,_) <- BS.readInt <$> BS.getLine
  as <- VU.unfoldrN n (BS.readInt . BS.dropWhile (<'-')) <$> BS.getLine
  print $ query as

query :: VU.Vector Int -> Int
query as = VU.minimum totalWork
  where
    n = VU.length as
    totalWork = VU.zipWith (+) incWork decWork
    incWork :: VU.Vector Int
    incWork = VU.reverse $ (VU.replicate 2 0 VU.++)
      $ (`evalState` (0, [])) $ (`VU.imapM` (VU.reverse incDiff)) $ \i x -> do
      (!acc, !ls) <- get
      if x < 0 then do
        put (acc, (i+1,-x):ls)
        return acc
      else do
        let (!acc1,!newls) = consume x (i+1) ls
        put (acc+acc1,newls)
        return $! acc+acc1
    decWork = (VU.fromListN 2 [0,1] VU.++)
      $ (`evalState` (1,[])) $ (`VU.imapM` decDiff) $ \i x -> do
      (!acc, !ls) <- get
      if x < 0 then do
        put (acc+1, (i+1,-x):ls)
        return $! acc+1
      else do
        let (!acc1, !newls) = consume x (i+1) ls
        put (acc+acc1+1, newls)
        return $! acc+acc1+1
    incDiff = VU.map (*2) $ VU.zipWith surpassExp4 as (VU.tail as)
    decDiff = VU.map (*2) $ VU.zipWith surpassExp4 (VU.tail as) as
    
-- minimum e with x <= 4^e * y. Assumes x,y > 0.
surpassExp4 x y = (surpassExponent x y + 1) `shiftR` 1

consume :: Int -> Int -> [(Int, Int)] -> (Int, [(Int, Int)])
consume = loop 0
  where
    loop !acc !consumed !i0 ((!i1,power):rest)
      | consumed < power = (acc + consumed * (i0-i1), (i1,power-consumed):rest)
      | otherwise = loop (acc + consumed * (i0-i1)) (consumed-power) i1 rest
    loop !acc !consumed !i0 []
      = (acc + consumed * i0, [])

-- minimum e with x <= 2^e * y. Assumes 0 < x,y < 2^62.
surpassExponent :: Int -> Int -> Int
surpassExponent x y
  | xShifted <= yShifted = bitDiff
  | otherwise            = bitDiff + 1
  where
    !xlz = countLeadingZeros x
    !ylz = countLeadingZeros y
    !bitDiff = ylz - xlz
    !xShifted = x `shiftL` (xlz - 2)
    !yShifted = y `shiftL` (ylz - 2)

Measured memory_bytes: 32124