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++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#define _CRT_SECURE_NO_WARNINGS
#pragma comment(linker, "/STACK:108777216")
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <cmath>
#include <vector>
#include <deque>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <bitset>
#include <utility>
#include <algorithm>
#include <ctime>
using namespace std;

int const MAX_N = 256 * 1024;
long long const LL_INF = 1000000000000000000LL;

int n, s[MAX_N];
unsigned long long ms[MAX_N];

long long solve(unsigned long long * ms, int ms_len) {
	long long ans = 0;
	if (ms_len <= 0)
		return ans;

	unsigned long long last_dg_2 = 0;
	unsigned long long last_ms = ms[0];

	for (int i = 1; i < ms_len; i++)
		if (last_dg_2 <= 0) {
			if (ms[i] >= last_ms) {
				last_ms = ms[i];
				continue;
			}

			long long ad = 0;
			long long val_save = ms[i];

			while (ms[i] < last_ms) {
				ans += 2;
				ad += 2;
				ms[i] *= 4LL;
			}

			if (ms[i] > 1000000000LL) {
				last_dg_2 = ad;
				last_ms = val_save;
			}
			else {
				last_dg_2 = 0;
				last_ms = ms[i];
			}
		}
		else {
			unsigned long long save_msi = ms[i];

			if (ms[i] >= last_ms) {
				unsigned long long x = last_ms;
				long long rem_2 = last_dg_2;
				while (ms[i] >= x && rem_2 > 0) {
					x *= 2;
					rem_2--;
				}
				if (ms[i] >= x) {
					last_dg_2 = 0;
					last_ms = ms[i];
					continue;
				}
			}

			// ms[i] < last_ms * 2^last_dg_2
			long long at_least = last_dg_2 - 32;
			at_least = max(at_least, 0LL);

			ans += at_least;
			long long new_dg_2 = at_least;

			long long rem_2 = last_dg_2 - at_least;   // <= 30
			unsigned long long x = last_ms;
			while (rem_2 > 0) { x *= 2; rem_2--; }

			while (ms[i] < x) {
				ans += 2;
				new_dg_2 += 2;
				ms[i] *= 4LL;
			}

			last_dg_2 = new_dg_2;
			last_ms = save_msi;
		}

	return ans;
}

long long calc(int x) {
	// >= for indexes [0...x-1]   + rotate
	int ms_len = 0;
	for (int i = x - 1; i >= 0; i--)
		ms[ms_len++] = s[i];
	long long ans = solve(ms, ms_len);
	ans += x; // rotate

	// <= for indexes [x...n)
	ms_len = 0;
	for (int i = x; i < n; i++)
		ms[ms_len++] = s[i];
	ans += solve(ms, ms_len);

	return ans;
}

int main() {
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);

	scanf("%d", &n);
	for (int i = 0; i < n; i++) scanf("%d", &s[i]);
	
	long long mn = LL_INF;

	if (n < 6000) {
		for (int i = 0; i <= n; i++)
			mn = min(mn, calc(i));
		cout << mn;
		return 0;
	}

	int down = 0, up = n+1;
	while (up > down + 3) {
		int t1 = down + (up - down) / 3;
		int t2 = up - (up - down) / 3;
		long long ans1 = calc(t1);
		long long ans2 = calc(t2);
		mn = min(mn, ans1);
		mn = min(mn, ans2);

		if (ans1 < ans2)
			up = t2;
		else
			down = t1;
	}
	for (int i = down - 10; i <= down + 10; i++)
		if (i >= 0 && i <= n)
			mn = min(mn, calc(i));
	cout << mn;
	return 0;
}