Problem:
Score : 800 points

Problem Statement

There are N positive integers A_1, A_2, ..., A_N.
Takahashi can perform the following operation on these integers any number of times:

Choose 1 \leq i \leq N and multiply the value of A_i by -2.

Notice that he multiplies it by minus two.

He would like to make A_1 \leq A_2 \leq ... \leq A_N holds.
Find the minimum number of operations required. If it is impossible, print -1.

Constraints

1 \leq N \leq 200000

1 \leq A_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the answer.

Sample Input 1

4
3 1 4 1

Sample Output 1

3

One possible solution is:

Choose i=4 and multiply the value of A_4 by -2. A_1, A_2, A_3, A_4 are now 3, 1, 4, -2.

Choose i=1 and multiply the value of A_1 by -2. A_1, A_2, A_3, A_4 are now -6, 1, 4, -2.

Choose i=4 and multiply the value of A_4 by -2. A_1, A_2, A_3, A_4 are now -6, 1, 4, 4.

Sample Input 2

5
1 2 3 4 5

Sample Output 2

0

A_1 \leq A_2 \leq ... \leq A_N holds before any operation is performed.

Sample Input 3

8
657312726 129662684 181537270 324043958 468214806 916875077 825989291 319670097

Sample Output 3

7

Solution in Haskell (GHC 7.10.3) (judge verdict: Accepted)

{-# 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
    totalWork = VU.zipWith (+) incWork decWork
    incWork :: VU.Vector Int
    incWork = VU.reverse $ VU.cons 0 $ VU.scanl' (+) 0 $ (`evalState` Nil)
      $ (`VU.imapM` VU.reverse incDiff) $ \i x -> state $ \ !ls ->
      if x < 0 then (0,) $! Cons (i+1) (-x) ls else consume x (i+1) ls
    decWork = VU.cons 0 $ VU.scanl' (+) 1 $ VU.map (+1) $ (`evalState` Nil)
      $ (`VU.imapM` decDiff) $ \i x -> state $ \ !ls ->
      if x < 0 then (0,) $! Cons (i+1) (-x) ls else consume x (i+1) ls
    incDiff = VU.map (*2) $ VU.zipWith surpassExp4 as (VU.tail as)
    decDiff = VU.map (*2) $ VU.zipWith surpassExp4 (VU.tail as) as

data Stack = Nil | Cons {-# UNPACK #-} !Int {-# UNPACK #-} !Int !Stack

{-# INLINE consume #-}
consume :: Int -> Int -> Stack -> (Int, Stack)
consume = loop 0
  where
    loop !acc !consumed !i0 (Cons i1 power rest)
      | consumed < power
        = (acc + consumed * (i0-i1),) $! Cons i1 (power-consumed) rest
      | otherwise = loop (acc + consumed * (i0-i1)) (consumed-power) i1 rest
    loop !acc !consumed !i0 Nil
      = (acc + consumed * i0,) $! Nil

-- minimum e with x <= 4^e * y. Assumes x,y > 0.
surpassExp4 x y = (surpassExponent x y + 1) `shiftR` 1

-- 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)