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: Runtime Error)

/*
    We make a bipartile graph of 2 * n vertices: we connect every number to every place in array; In this graph we
    have some bad edges - some numbers we can't put in some places; For every k we count the amount of ways to
    choose from bad edges a matching of size k; then using inclusion-exclusion formula we get the answer;
*/

#include <bits/stdc++.h>
#include <complex>
#define pb push_back
#define pll pair <ll, ll>
#define MOMI using namespace std;
#define mp make_pair
#define pyshnapyshnakaa ios_base :: sync_with_stdio(0); cin.tie(0); cout.tie(0);
#pragma optimize("TKACHENKO-GORYACHENKO")
#pragma GCC optimize("O3")

//#pragma GCC optimize("unroll-loops")
//#define double long double

typedef long long ll;

typedef long double ld;

using namespace std;

const ll maxn = 5e5;
const ll inf = 1e18;

ll n, m, k;

ll st2[maxn];

vector <ll> dp(vector <ll> A){
    vector <ll> DP;
    A.pb(inf);
    DP.resize(maxn);
    DP[n - 1] = 0;
    ll q;
    vector <pll> LAST;
    LAST.pb(mp(n - 1, A[n - 1]));
    for (q = n - 2; q >= 0; q--){
        ll c = 0;
        ll a = A[q], b = A[q + 1];
        while (a > b){
            c++;
            b *= 4;
        }
//        cout << "q c " << q << " " << c << endl;
        if (c == 0){
            DP[q] = DP[q + 1];
            LAST.pb(mp(q, A[q]));
//            cout << "DP " <<  q << " " << DP[q] << endl;
            continue;
        }
        ll ans = DP[q + 1];
        ll nc = c;
        ll j = q;
        while (LAST.size() != 0){
            ll last = LAST[LAST.size() - 1].second;
            ll i = LAST[LAST.size() - 1].first;
            ans = ans + nc * (i - j);
            if (last * st2[2 * nc] <= A[i + 1]){
                LAST[LAST.size() - 1].second *= st2[2 * nc];
                break;
            }
            else{
                ll a, b, dc = 0;
                a = last * st2[2 * nc], b = A[i + 1];
                while (a > b){
                    dc++;
                    b *= 4;
                }
                nc = dc;
                LAST.pop_back();
                LAST[LAST.size() - 1].second *= (last / A[i]);
            }
        }
//        ll last = LAST[LAST.size() - 1].first;
        DP[q] = ans;
//        cout << "DP " <<  q << " " << DP[q] << endl;
    }
    return DP;
}

int main() {
    ll q, w, e, a, b;
//    pyshnapyshnakaa;
    st2[0] = 1;
    vector <ll> A;
    for (q = 1; q < maxn; q++){
        st2[q] = st2[q - 1] * 2;
    }
    cin >> n;
    A.resize(n);
    for (q = 0; q < n; q++){
        cin >> A[q];
    }
    vector <ll> DP1 = dp(A);
//    cout << "DP1" << endl;
//    for (q = 0; q < n; q++){
//        cout << DP1[q] << " ";
//    }
//    cout << endl << endl << endl;
    reverse(A.begin(), A.end());
    vector <ll> DP2 = dp(A);
//    cout << "DP2" << endl;
//    for (q = 0; q < n; q++){
//        cout << DP2[q] << " ";
//    }
//    cout << endl;
    reverse(DP2.begin(), DP2.end());
    ll ans = 2 * DP1[0];
    for (q = 0; q <= n; q++){
        ans = min(ans, 2 * DP1[q + 1] + q + 1 + 2 * DP2[q]);
    }
    cout << ans;
    return 0;
}