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: Accepted)

#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <string>
#include <cmath>
#include <iomanip>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <bitset>
#include <cassert>
#include <cstring>
#include <climits>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define SORT(c) sort((c).begin(), (c).end())

typedef long long ll;
typedef long double lb;
typedef pair<int, int> P;
typedef vector<int> V;
typedef map<int, int> M;

constexpr ll INF = 1e18;
constexpr ll MOD = 1e9 + 7;
constexpr double PI = 3.14159265358979323846;
constexpr int dx[] = {0, 0, 1, -1};
constexpr int dy[] = {1, -1, 0, 0};

ll div_up(ll a, ll b)
{
    return (a + b - 1) / b;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    ll n, a[212345], dp1[212345][16], dp2[212345][16];

    cin >> n;
    for (ll i = 0; i < n; i++)
    {
        cin >> a[i];
    }

    for (int i = 0; i <= 15; i++)
    {
        dp1[n - 1][i] = i;
        dp2[0][i] = i;
    }

    for (int i = n - 2; i >= 0; i--)
    {
        ll tmp = a[i];
        for (int j = 0; j <= 15; j++)
        {
            if (tmp <= a[i + 1])
                dp1[i][j] = j + dp1[i + 1][0];
            else
            {
                int x = ceil(log(div_up(tmp, a[i + 1])) / log(4));
                dp1[i][j] = j + dp1[i + 1][min(x, 15)];

                if (x >= 15)
                {
                    dp1[i][j] += (x - 15) * (n - i - 1);
                }
            }

            tmp *= 4;
        }
    }

    for (int i = 1; i < n; i++)
    {
        ll tmp = a[i];
        for (int j = 0; j <= 15; j++)
        {
            if (tmp <= a[i - 1])
                dp2[i][j] = j + dp2[i - 1][0];
            else
            {
                int x = ceil(log(div_up(tmp, a[i - 1])) / log(4));
                dp2[i][j] = j + dp2[i - 1][min(x, 15)];

                if (x >= 15)
                {
                    dp2[i][j] += (x - 15) * i;
                }
            }

            tmp *= 4;
        }
    }

    ll res = min(dp1[0][0] * 2, dp2[n - 1][0] * 2 + n);

    for (int i = 1; i < n; i++)
    {
        res = min(res, dp1[i][0] * 2 + dp2[i - 1][0] * 2 + i);
    }

    cout << res << endl;

    return 0;
}