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 C# (Mono 4.6.2.0) (judge verdict: Runtime Error)

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading.Tasks;
using static System.Console;
using static System.Math;
class Z { static void Main() => new K(); }
class K
{
	int F => int.Parse(Str);
	long FL => long.Parse(Str);
	int[] G => Strs.Select(int.Parse).ToArray();
	long[] GL => Strs.Select(long.Parse).ToArray();
	string Str => ReadLine();
	string[] Strs => Str.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
	const int MOD = 1000000007;
	public K()
	{
		SetOut(new StreamWriter(OpenStandardOutput()) { AutoFlush = false });
		Solve();
		Out.Flush();
	}
	void Solve()
	{
		var N = F;
		var A = G;
		var rat = new int[N - 1];
		// rat[i]: A[i] <= A[i+1] => 2^n A[i] > A[i+1] となる最小の n (>=1) にたいし, rat[i] = 1-n
		//         A[i] > A[i+1] => A[i] <= 2^n A[i+1] となる最小の n (>=1) にたいし, rat[i] = n
		for (var i = 0; i < N - 1; i++)
			if (A[i] <= A[i + 1])
			{
				var a = A[i];
				var b = A[i + 1];
				while (a <= b) { a *= 2; rat[i]++; }
				rat[i] = 1 - rat[i];
			}
			else
			{
				var a = A[i];
				var b = A[i + 1];
				while (a > b) { b *= 2; rat[i]++; }
			}
		var re = new long[N - 1];
		for (var i = 0; i < N - 1; i++) re[i] = (rat[i] + 1) / 2;
		var ro = new long[N - 1];
		for (var i = 0; i < N - 1; i++) ro[i] = (-rat[N - i - 2] + 1) / 2;
		var se = GiveSum(re);
		var so = GiveSum(ro);
		var min = long.MaxValue;
		for (var n = 0; n < N; n++) min = Min(min, n + 2 * so[N - 1 - n] + 2 * se[n]);
		WriteLine(min);
	}
	long[] GiveSum(long[] r)
	{
		var N = r.Length;
		var depth = -r.Min();
		var dp = new long[N + 2, depth + 1];
		for (var d = 0; d <= depth; d++) dp[N, d] = d;
		for (var n = N - 1; n >= 0; n--)
		{
			for (var d = 0; d <= depth; d++)
			{
				var e = Max(d + r[n], 0);
				dp[n, d] = d + dp[n + 1, Min(e, depth)] + Max(e - depth, 0) * (N - n);
			}
		}
		var sum = new long[N + 2];
		for (var i = 0; i <= N + 1; i++) sum[i] = dp[i, 0];
		return sum;
	}
}