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: Wrong Answer)

#include<algorithm>
#include<cmath>
#include<iomanip>
#include<iostream>
#include<map>
#include<numeric>
#include<queue>
#include<set>
#include<sstream>
#include<unordered_map>
#include<unordered_set>
#include<vector>
#include<random>
using ll = long long;
enum : int { M = (int)1e9 + 7 };
enum : ll { MLL = (ll)1e18L + 9 };
using namespace std;
#ifdef LOCAL
#include"rprint2.hpp"
#include"debug_deque.hpp"
#define vector DebugDeque
#else
#define FUNC(name) template <ostream& out = cout, class... T> void name(T&&...){ }
FUNC(prints) FUNC(printe) FUNC(printw) FUNC(printew) FUNC(printb) FUNC(printd) FUNC(printde) FUNC(printdu);
#endif
template <class S, class T>
istream& operator >> (istream& in, pair<S, T>& p){ return in >> p.first >> p.second; }
template <class T>
istream& operator >> (istream& in, vector<T>& v){ for(auto& e : v){ in >> e; } return in; }

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n; cin >> n;
    vector<ll> as(n); cin >> as;
    vector<ll> ls(n), rs(n);
    for(int i = 0; i < n - 1; i++){
        ll a = as[i], b = as[i + 1];
        while(1){
            if(a > 0 && b > 0 && a >= b){
                break;
            }
            ls[i]++;
            a *= -2;
        }
    }
    for(int i = 1; i < n; i++){
        ll a = as[i - 1], b = as[i];
        while(1){
            if(a > 0 && b > 0 && a <= b){
                break;
            }
            rs[i]++;
            b *= -2;
        }
    }

    vector<ll> ls2 = ls, rs2 = rs;
    for(int i = 0; i < n - 1; i++){
        // if(!rs2[i]){ continue; }
        ll num = as[i + 1] << rs2[i + 1];
        int cnt = 0;
        while((as[i] << rs2[i]) > num){
            cnt++;
            num <<= 1;
        }
        rs2[i] += cnt * (n - i - 1);
        printde(i, cnt);
    }
    for(int i = n - 1; i >= 1; i--){
        // if(!ls2[i]){ continue; }
        ll num = as[i - 1] << ls2[i - 1];
        int cnt = 0;
        while((as[i] << ls2[i]) > num){
            cnt++;
            num <<= 1;
        }
        ls2[i] += cnt * (i - 1);
    }
    // for(int i = 0; i < n; i++){
    //     ls2[i] *= (i + 1);
    //     rs2[i] *= (n - i);
    // }
    // for(int i = n - 2; i >= 0; i--){
    //     ls2[i] += ls2[i + 1];
    // }
    // for(int i = 0; i < n - 2; i++){
    //     rs2[i + 1] += rs2[i];
    // }

    vector<ll> ls3 = ls2, rs3 = rs2;
    for(int i = 0; i < n - 1; i++){
        ls3[i + 1] += ls3[i];
    }
    for(int i = n - 2; i >= 0; i--){
        rs3[i] += rs3[i + 1];
    }

    printde(ls, ls2, ls3);
    printde(rs, rs2, rs3);

    ll mini = MLL;
    for(int i = 0; i < n; i++){
        ll cand = i;
        cand += i - 2 >= 0 ? ls3[i - 2] : 0;
        cand += i + 1 < n ? rs3[i + 1] : 0;
        // cand += rs3[i] - rs2[i] * (n - i);
        // cand += ls3[i] - ls2[i] * (i + 1);
        mini = min(mini, cand);
        printde(i, cand);
    }
    cout << mini << '\n';
}
/*
4 1 4 1 4 1
0 2 0 2 0 2
6 4 4 2 2 0


4 1 1 1 1 1
0 2 0 0 0 0
10 0 0 0 0 0


16 1 1 1 1 1
0  4 0 0 0 0

20 0 0 0 0 0


16 1 4 1 1 1
0  4 0 2 0 0

18 6 6 0 0 0


16 1 1 1 1 1
0  4 0 0 0 0
0 20 0 0 0 0
20 0 0 0 0 0

16 1 4 1 1 1
0  4 0 2 0 0
0 12 0 6 0 0
18 6 6 0 0 0

16 1 5 1 1 1
0  4 0 2 0 0
0 12 0 6 0 0
18 6 6 0 0 0

16 1  16 1 1 1
0  4  0  4 0 0
0  4  0 12 0 0
16 12 12 0 0 0

16 1  64 1 1 1
0  4  0  8 0 0
0  4  0 24 0 0
28 24 24 0 0 0

*/