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 Fortran (gfortran v4.8.4) (judge verdict: Wrong Answer)

program negative_doubling
  implicit none
  integer :: n, a(200000,2) = 0, dp(200000,0:15,2) = 0, m, i, j, x, y
  real(8) :: b(200000,2) = 0.d0
  read(*,*) n
  read(*,*) a(1:n,1)
  a(1:n,2) = a(n:1:-1,1)
  b(1:n,:) = log4(real(a(1:n,:),8))
  do x = 0, 15
    dp(n,x,:) = 2*x
  end do
  do i = n-1, 1, -1
    do x = 0, 15
      do j = 1, 2
        y = max(x+ceiling(b(i,j)-b(i+1,j)),0)
        if (y <= 15) then
          dp(i,x,j) = dp(i+1,y,j)+2*x
        else
          dp(i,x,j) = dp(i+1,15,j)+2*x+2*(y-15)*(n-i-1)
        end if
      end do
    end do
  end do
  dp(1:n,:,2) = dp(n:1:-1,:,2)
  m = min(dp(1,0,1),dp(n,0,2)+n)
  do i = 2, n
    m = min(m,dp(i,0,1)+dp(i-1,0,2)+i-1)
  end do
  write(*,'(i0)') m
  stop
contains
  elemental real(8) function log4(x)
    implicit none
    real(8), intent(in) :: x
    log4 = log(x)/log(4.d0)
    return
  end function log4
end program negative_doubling