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

   typedef long long ll;
   typedef pair<ll, ll> Pii;

   const double EPS = (1e-7);
   const ll INF =(1e9);
   const double PI = (acos(-1));
   const ll MOD = ll(1e9) + 7;

   #define REP(i, n) for(ll i = 0; i < (ll)(n); i++)
   #define REPR(i, n) for(ll i = n; i > -1; i--)
   #define FOR(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
   #define ALL(x) (x).begin(),(x).end()
   #define SORT(x) sort((x).begin(), (x).end())
   #define REVERSE(x) reverse((x).begin(), (x).end())
   #define SZ(x) ((ll)(x).size())
   #define pb push_back
   #define mp make_pair

   //chmax(a, b): a>bならaをbで更新　更新したときにtrueを返す
   //chmin(a, b): a<bならaをbで更新　更新したときにtrueを返す
   template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
   template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }

   #define dump(x) cerr<< #x << "= " << (x) << endl;

   ll gcd(ll a, ll b){return b?gcd(b,a%b):a;};

   ll pow(ll a, ll b){ if (b == 0) {return 1ll;} else if ( b % 2 == 0ll) {return pow(a * a, b / 2);} else {return a * pow(a * a, b / 2);}}
   
   ll dx[4]={1,0,-1,0};
   ll dy[4]={0,1,0,-1};

const ll MAX_N = 2e5, MAX_X = 16;
ll N;
ll A[MAX_N]; //0-indexed
ll dp_pos[MAX_N][MAX_X + 1];
ll dp_neg[MAX_N][MAX_X + 1];

ll positive(ll l, ll x){
   //A_l, ..., A_N-1に対し、A_lに*4をx回するという条件下で*4倍を合計何回すれば単調増加列を作れるか？
   if (dp_pos[l][x] >= 0) return dp_pos[l][x];
   else if (l >= N) return 0;
   else if (x > MAX_X) return (positive(l, MAX_X) + (x - MAX_X) * (N - l));
   else if (l == N - 1) {
      dp_pos[l][x] = x;
      return x;
   }
   else{
      ll k = 0;
      ll tmp1 = A[l] * (1ll << (2*x));
      ll tmp2 = A[l + 1];
      while (tmp2 < tmp1){
         tmp1 /= 4;
         k ++;
      }
      dp_pos[l][x] = positive(l + 1, k) + x;
      return dp_pos[l][x];     
   }
}

ll negative(ll l, ll x){
   /*
   A0, A1, ..., A_lに対し, A_lに*4をx回したときに合計*4を何回すれば単調減少列を作れるか？
   */
  if (dp_neg[l][x] >= 0) return dp_neg[l][x];
  else if (l < 0) return 0;
  else if (x > MAX_X) return (negative(l, MAX_X) + (x - MAX_X) * (l + 1));
  else if (l == 0){
     dp_neg[l][x] = x;
     return x;
  }
  else{
     ll k = 0;
     ll tmp1 = A[l] * (1ll << (2 * x));
     ll tmp2 = A[l - 1];
     while (tmp2 < tmp1){
        tmp1 /= 4;
        k++;
     }
     dp_neg[l][x] = negative(l - 1, k) + x;
     return dp_neg[l][x];
   }
}

int main(){
   //input
   cin >> N;
   REP(i, N){
      cin >> A[i];
   }
   //init DPtable with -INF
   REP(l, MAX_N){
      REP(x, MAX_X + 1){
         dp_pos[l][x] = -INF;
         dp_neg[l][x] = -INF;
      }
   }
   /*
   REP(l, N){
      REP(x, 4){
         printf("positive(%lld, %lld) = %lld\n", l, x, positive(l,x));
         printf("negative(%lld, %lld) = %lld\n", l, x, negative(l,x));
      }
   }
   */
   ll ans = INF;
   REP(i, N + 1){
      ll tmp = positive(i, 0) * 2 + negative(i - 1, 0) * 2 + i;
      chmin(ans, tmp);
   }
   cout << ans << endl;
}