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: Time Limit Exceeded)

#include<bits/stdc++.h>
#include<iostream>

using namespace std;

#define boost ios::sync_with_stdio(0);cin.tie(0);
#define forn(i,n) for ( int i=0 ; i < n ; i++ )
#define all(a) (a).begin(), (a).end()
#define lb lower_bound
#define ub upper_bound
#define F first
#define S second
#define MP make_pair
#define PB push_back
#define ll long long
#define endl '\n'
#define mod 1000000007

#define db(...) ZZ(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void ZZ(const char* name, Arg1&& arg1){std::cerr << name << " = " << arg1 << endl;}
template <typename Arg1, typename... Args>void ZZ(const char* names, Arg1&& arg1, Args&&... args)
{
	const char* comma = strchr(names + 1, ',');
	std::cerr.write(names, comma - names) << " = " << arg1;
	ZZ(comma, args...);
}

#define int ll


signed main()
{
	// freopen ("f.out","r",stdin);
 	//freopen ("f.out","w",stdout);
	boost
	//code
	int n;
	cin >> n;
	ll negcost[n];
	ll valonneg[n];
	ll cost[n];
	ll valoncost[n];
	vector<ll> v(n);
	forn(i,n) cin >> v[i];
	negcost[0] = 1;
	valonneg[0] = v[0]*(-2);
	for(int i=1;i<n;i++){
		negcost[i] = negcost[i-1] + 1;
		valonneg[i] = v[i]*(-2);
		int j=i-1;
		while(j>=0){
			if(valonneg[j]<=valonneg[j+1]) break;
			while(valonneg[j]>valonneg[j+1]){
				assert(valonneg[j]<0);
				valonneg[j] *= 4;
				negcost[i] += 2;
			}
			j--;
		}
	}
	// forn(i,n) db(valonneg[i]);
	cost[0] = 0;
	valoncost[0] = v[0];
	for(int i=1;i<n;i++){	
		ll here = v[i];
		ll count = cost[i-1];
		while(here<valoncost[i-1]){
			count += 2;
			here *= 4;
		}
		if(count<negcost[i-1]){
			cost[i] = count;
			valoncost[i] = here;
		}
		else{
			cost[i] = negcost[i-1];
			valoncost[i] = v[i];
		}
	}
	cout << cost[n-1] << endl;
	return 0;
}