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

#include "iostream"
#include "climits"
#include "list"
#include "queue"
#include "stack"
#include "set"
#include "functional"
#include "algorithm"
#include "string"
#include "map"
#include "unordered_map"
#include "unordered_set"
#include "iomanip"
#include "cmath"
#include "random"
#include "bitset"
#include "cstdio"
#include "numeric"
#include "ctime"

using namespace std;

const long long int MOD = 1000000007;
//const int MOD = 998244353;

//long long int N, M, K, H, W, L, R;
long long int N, M, K, H, W, L, R;


int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);

	cin >> N;
	L = 0;
	R = N;
	vector<long long int>v(N);
	vector<long double>lg;
	for (auto &i : v)cin >> i;
	for (auto i : v) {
		lg.push_back(log2((long double)i));
	}
	vector<long long int>r(N + 1);
	vector<long long int>l(N + 1);
	stack<int>rht;
	long double st = lg.back();
	for (int i = N - 2; i >= 0; i--) {
		r[i] = r[i + 1];
		while (st < lg[i]) {
			st += log2((long double)4);
			if(rht.empty())	r[i] += 2 * (N - i - 1);
			else {
				r[i] += 2 * (rht.top() - i);
				rht.pop();
			}
		}
		for (long double j = lg[i]+2; j <= lg[i + 1]; j += 2) {
			rht.push(i);
		}
		st = lg[i];
	}
	auto w = v;
	stack<int>lft;
	st = lg.front();
	lg.push_back(-1000);
	for (int i = 1; i <= N; i++) {
		l[i] = l[i - 1];
		while (st < lg[i]) {
			st += log2((long double)4);
			if (lft.empty())	l[i] += 2 * i;
			else {
				l[i] += 2 * (i - lft.top());
				lft.pop();
			}
		}
		for (long double j = lg[i]+2; j <= lg[i - 1]; j += 2) {
			lft.push(i);
		}
		st = lg[i];
	}
	//for (int i = 0; i <= N; i++) {
	//	cout << i << " " << l[i] << " " << r[i] << endl;
	//}
	long long int ans = r[0];
	for (int i = 1; i <= N; i++)ans = min(i + l[i - 1] + r[i],ans);
	cout << ans << endl;
	return 0;
}