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 <bits/stdc++.h>
using namespace std;

void doRoutine() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
#ifdef LOCAL
    freopen("C:\\Users\\M\\alg\\practice\\clion_main\\input.txt", "r", stdin);
    freopen("C:\\Users\\M\\alg\\practice\\clion_main\\output.txt", "w", stdout);
#endif
}


#define DBG(VAR) cerr << #VAR << ": " << VAR << endl

#define all(x) x.begin(), x.end()
#define vb vector<bool>
#define vi vector<int>
#define vvi vector<vi>
#define pii pair<int, int>
#define fi first
#define se second
#define pb push_back
#define vc vector
#define $ make_pair

#ifndef LOCAL
#define cerr if(0) cerr
#define DBG(...) (true)
#endif

#define D double
#define int long long

// ----------------------------- CODE NOW ------------------------

const int INF = 1000000000000000000;

struct num {
    int x, s;
    num(int x_ = 0, int s_ = 0) {
        x = x_, s = s_;
    }
};

bool operator<=(num a, num b) {
    int x = min(a.s, b.s);
    a.s -= x;
    b.s -= x;
    if (a.s == 0) {
        while (a.x > b.x && b.s > 0) {
            --b.s;
            b.x *= 4;
        }
        return a.x <= b.x;
    }
    else {
        while (a.x <= b.x && a.s > 0) {
            a.x *= 4;
            --a.s;
        }
        return a.x <= b.x;
    }
}

void solve(vi &arri, vi &res) {
    int n = arri.size();
    vi prev(n, -1);
    res[0] = res[1] = 0;
    vc<num> arr(n);
    for (int i = 0; i < n; ++i) {
        arr[i] = num(arri[i], 0);
    }
    for (int i = 1; i < n; ++i) {
        if (arr[i] <= arr[i - 1]) {
            auto t = arr[i];
            ++t.s;
            if (t <= arr[i - 1]) {
                prev[i] = i - 1;
            }
            else {
                prev[i] = prev[i - 1];
            }
            res[i + 1] = res[i];
        }
        else {
            int j = i;
            int glob_delta = 0;
            while (j != 0) {
                int delta = 0;
                while (!(arr[j] <= arr[j - 1])) {
                    ++arr[j - 1].s;
                    ++delta;
                }
                int cnt = delta;
                if (cnt == 0) {
                    break;
                }
                delta *= j - 1 - prev[j - 1];
                glob_delta += delta;

                bool two_or_more_elements = (j - 1 - prev[j - 1] >= 2);
                j = prev[j - 1] + 1;
                if (two_or_more_elements) {
                    arr[j].s += cnt;
                }
            }
            prev[i] = j - 1;
            res[i + 1] = res[i] + glob_delta * 2;
        }
    }
}

bool FLAG = false;

void solve2(vi &arri, vi &res) {
    int n = arri.size();
    res[0] = res[1] = 0;
    vc<num> arr(n);
    for (int i = 0; i < n; ++i) {
        arr[i] = num(arri[i], 0);
    }
    for (int i = 1; i < n; ++i) {
        int cnt = 0;
        for (int j = i - 1; j >= 0; --j) {
            while (!(arr[j + 1] <= arr[j])) {
                ++arr[j].s;
                ++cnt;
            }
        }
        res[i + 1] = res[i] + 2 * cnt;
        if (FLAG) {
            cerr << "After step i: ";
            for (int j = 0; j < n; ++j) {
                cerr << "(" << arr[j].x << ", " << arr[j].s << ") ";
            }
            cerr << endl;
        }
    }
}

void print(vi &a, int k) {
    for (int i = 0; i < min((int)a.size(), k); ++i) {
        cerr << a[i] << " ";
    }
    cerr << endl;
}

signed main() {
    doRoutine();

    /*
    srand(123326142);
    while (true) {
        int n = 10;
        vi a(n);
        for (int i = 0; i < n; ++i) {
            a[i] = abs(rand()) % 100 + 1;
        }
        vi r1(n + 1), r2(n + 1);
        solve(a, r1);
        solve2(a, r2);
        if (r1 != r2) {
            cerr << "WRONG" << endl;
            FLAG = true;
        }
        if (FLAG) {
            print(a, 100);
            print(r1, 100);
            print(r2, 100);
            solve(a, r1);
            solve2(a, r2);
        }
    }

    return 0;
     */

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

    vi ra = a;
    reverse(all(ra));

    vi dp1(n + 1), dp2(n + 1);
    solve(a, dp1);
    solve(ra, dp2);

    int ans = INF;
    for (int l = 0; l <= n; ++l) {
        ans = min(ans, l + dp1[l] + dp2[n - l]);
    }

    cout << ans << endl;

    return 0;
}