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, d(2,200000) = 0, dp(2,200000,0:15) = 0, m, i, j, x, y
  integer(8) :: a(200000) = 0_8, p, q
  read(*,*) n
  read(*,*) a(1:n)
  do j = 1, 2
    do i = 1, n-1
      p = a(i)
      q = a(i+1)
      if (p <= q) then
        do while (4_8*p <= q)
          d(j,i) = d(j,i)-1
          p = 4_8*p
        end do
      else
        do while (p > q)
          d(j,i) = d(j,i)+1
          q = 4_8*q
        end do
      end if
    end do
    a(1:n) = a(n:1:-1)
  end do
  do x = 0, 15
    dp(:,n,x) = 2*x
  end do
  do j = 1, 2
    do i = n-1, 1, -1
      do x = 0, 15
        y = max(x+d(j,i),0)
        if (y <= 15) then
          dp(j,i,x) = dp(j,i+1,y)+2*x
        else
          dp(j,i,x) = dp(j,i+1,15)+2*x+2*(y-15)*(n-i)
        end if
      end do
    end do
  end do
  dp(2,1:n,:) = dp(2,n:1:-1,:)
  m = min(dp(1,1,0),dp(2,n,0)+n)
  do i = 2, n
    m = min(m,dp(1,i,0)+dp(2,i-1,0)+i-1)
  end do
  write(*,'(i0)') m
  stop
end program negative_doubling