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>
typedef long long ll;
using namespace std;
ll lft[200005] , rgt[200005] , n , a[200005];
ll dp[200005][18];
const ll inf = 1e15;
ll solve(ll id ,ll x)
{
        if(x >= 17) return inf;
        if(id == n + 1) {
                return 0;
        }
        if(dp[id][x] != -1) return dp[id][x];
        ll mup = 1LL;
        for(ll i=0;i<x;i++) mup = mup*4;
        mup = mup * a[id-1];
        ll cnt = 0;
        ll cur = a[id];
        while(mup > cur) {
                cur = cur * 4; cnt++;
        }
        return solve(id+1, cnt) + cnt;
}
ll ans[200005];
ll ans2[200005];
int main()
{
        cin >> n;
        for(ll i=1;i<=n;i++) cin >> a[i];
        for(ll i=2;i<=n;i++) {
                lft[i] = 0;
                ll cnt = 0;
                ll x = a[i];
                ll y = a[i-1];
                while(x < y) {
                        x = x*4;
                        cnt++;
                }
                lft[i] = cnt + cnt;
                //cout << i << " " << lft[i] << endl;
        }
        for(ll i = n-1; i>=1;i--) {

                rgt[i] =0;
                ll x = a[i] * 2;
                ll y = a[i+1];
                ll cnt = 0;
                while(x < y) {
                        x = x * 4;
                        cnt++;
                }
                rgt[i] = cnt + cnt + 1;
                //cout << i << " " << rgt[i] << endl;
        }
        memset(dp,-1,sizeof dp);
        for(ll i=2;i<=n;i++) {
                for(ll j=0;j<=15;j++) {
                        ll  y= solve(i, j);
                        if(j==0) ans[i] = y + y;
                }
        }
        //for(ll i=2;i<=n;i++) cout << i << " --- " << ans[i] << "\n";
        for(ll i=1;i<=n;i++) a[i] = a[i] * 2;
        memset(dp,-1,sizeof dp);
        reverse(a+1, a+n+1);
        //for(ll i=1;i<=n;i++) cout << a[i] << " "; cout << endl;
        for(ll i=2;i<=n;i++) {
                for(ll j=0;j<=15;j++) {
                        ll y = solve(i,j);
                        if(j==0){
                                //cout << y + y << " ";
                                ans2[i] += y + y + (n-i+1);
                        }
                }
        }
        ll ret = 1e14;
        //for(ll i=2;i<=n;i++) cout << i << " ------- " << ans2[i] << endl;
        for(ll i=1;i<=n;i++) {
                ret = min(ret , ans[i + 1] + ans2[n+1-i + 1]);
        }
        cout << ret << endl;
        //for(ll i=1;i<=n;i++) cout << ans[i] << " ";
        return 0;
}