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

#include <iostream>
#include <string>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <unordered_map>
#include <map>
#include <set>
#include <algorithm>
#include <queue>
#include <stack>
#include <functional>
#include <bitset>
#include <assert.h>
#include <unordered_map>
#include <fstream>
#include <time.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef pair<ll,ll> P;
typedef pair<int,int> pii;
typedef vector<P> vpl;
typedef tuple<ll,ll,ll> tapu;
#define rep(i,n) for(int i=0; i<(n); i++)
#define REP(i,a,b) for(int i=(a); i<(b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = 1<<30;
const ll linf = 1LL<<62;
const int MAX = 5020000;
ll dy[8] = {1,-1,0,0,1,-1,1,-1};
ll dx[8] = {0,0,1,-1,1,-1,-1,1};
const double pi = acos(-1);
const double eps = 1e-7;
template<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){
	if(a>b){
		a = b; return true;
	}
	else return false;
}
template<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){
	if(a<b){
		a = b; return true;
	}
	else return false;
}
template<typename T> inline void print(T &a){
    rep(i,a.size()) cout << a[i] << " ";
    cout << "\n";
}
template<typename T1,typename T2> inline void print2(T1 a, T2 b){cout << a << " " << b << "\n";}
template<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){
	cout << a << " " << b << " " << c << "\n";
}
template<typename T1,typename T2,typename T3,typename T4> inline void print4(T1 a, T2 b, T3 c, T4 d){
	cout << a << " " << b << " " << c << " " << d << "\n";
}
ll pcount(ll x) {return __builtin_popcountll(x);}
const int mod = 1e9 + 7;
//const int mod = 998244353;

int main(){
	int n; cin >> n;
	vl a(n); rep(i,n) cin >> a[i];
	int M = 62;
	vvl minus(n,vl(M,linf)), plus(n+1,vl(M,linf));
	for(int i=0; i<M; i+=2) plus[n][i] = 0; 
	rep(i,n){
		if(i==0){
			for(int j=1; j<M; j+=2) minus[i][j] = j;
			continue;
		}
		int cnt = 0;
		if(a[i-1] >= a[i]){
			ll tmp = a[i];
			while(a[i-1] >= tmp * 2) tmp *= 2, cnt++;
			if(cnt & 1) cnt--;
		}else{
			ll tmp = a[i-1];
			while(tmp < a[i]) tmp *= 2, cnt--;
			if(cnt & 1) cnt--; 
		}
		for(int j=1; j<M; j+=2){
			if(j+cnt >= M) break;
			if(j+cnt < 1) continue;
			chmin(minus[i][j+cnt], minus[i-1][j] + j+cnt);
		}
		if(cnt > 0) for(int j=1; j<cnt; j+=2) chmin(minus[i][j], minus[i-1][1] + j);
	}
	for(int i=n-1; i>=0; i--){
		if(i==n-1){
			for(int j=0; j<M; j+=2) plus[i][j] = j;
			continue;
		}
		int cnt = 0;
		if(a[i+1] >= a[i]){
			ll tmp = a[i];
			while(a[i+1] >= tmp * 2) tmp *= 2, cnt++;
			if(cnt & 1) cnt--;
		}else{
			ll tmp = a[i+1];
			while(a[i] > tmp) tmp *= 2, cnt--;
			if(cnt & 1) cnt--;
		}
		for(int j=0; j<M; j+=2){
			if(j+cnt >= M) break;
			if(j+cnt < 0) continue;
			chmin(plus[i][j+cnt], plus[i+1][j] + j+cnt);
		}
		if(cnt > 0) for(int j=0; j<cnt; j+=2) chmin(plus[i][j], plus[i+1][0] + j);
	}
	ll ans = plus[0][0];
	rep(i,n){
		chmin(ans, minus[i][1] + plus[i+1][0]);
	}
	cout << ans << "\n";
}