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 (GCC 5.4.1) (judge verdict: Wrong Answer)



//#include "pch.h"
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>

#define llong long long
#define fr(i,l,r) for(i=(l);i<(r);i++)
#define min(p,q) ((p)<(q)?(p):(q))
#define max(p,q) ((p)>(q)?(p):(q))
#define INF 1000000000000//10^12



int main(void)
{
	//変数の宣言
	int n;
	static llong int a[200010];
	static llong int b[200010];
	static llong int c[200010];
	static llong int bfugo[200010];
	static llong int cfugo[200010];
	static llong int minus[200010][40];
	static llong int plus[200010][40];
	
	int templ;
	int tempr;

	//よく使う変数
	int i, j, k, l;
	int flag = 0;
	llong int ans = 0;
	int count = 0;
	llong int temp, temp1, temp2;
	int max, min;
	int len;
	int sum = 0;
	int under, upper, middle;

	//データの読み込み
	scanf("%d", &n);
//	scanf_s("%d", &n);

	for (i = 0;i < n;i++) {
		scanf("%lld", &a[i]);
//		scanf_s("%d", &a[i]);
	}

	//	printf("nは%dです\n", n);
	//	printf("データの読み込み終了\n");
		//実際の処理

		//n=1の場合分けいる？



	flag = 0;
	tempr = -1;
	for (i = 0;i < n;i++) {
		if (a[i] == 0) {
			if (tempr == -1) {
				templ = i;
				tempr = i;
				flag = 1;
			}
			else if (i - tempr == 1) {
				tempr = i;
			}
			else {
				flag = 2;
				break;
			}
		}
	}
	
	if (flag == 2) {
		printf("-1");
		return 0;
	}
	else if (flag == 0) {
		templ = n;
		tempr = 0;
	}
	
	for(i=0;i<n;i++){
		if(a[i]>=0){
			b[i]=-2*a[i];
			bfugo[i]=1;
		}else{
			b[i]=a[i];
			bfugo[i]=0;
		}
	}
	
	
	for(i=0;i<n;i++){
		if(a[i]<=0){
			c[i]=-2*a[i];
			cfugo[i]=1;
		}else{
			c[i]=a[i];
			cfugo[i]=0;
		}
	}
	
	//minus
	for (i = 0;i < templ;i++) {
		if (i == 0) {
			for(j=0;j<=30;j++){
				minus[0][j]=bfugo[0]+2*j;
			}
		}else{
			temp = 0;
			temp1 =b[i-1];
			temp2 =b[i];
			if(temp1>temp2){
				while (temp1 > temp2) {
					temp++;
					temp1 = temp1 * 4;
				}
			}else if(temp1<temp2){
				while(temp1<temp2){
					temp--;
					temp2=temp2*4;
				}
			}
			
			for(j=0;j<=15;j++){
				minus[i][j]=minus[i-1][max(0,j+temp)]+bfugo[i]+2*j;
			}
			for(j=16;j<=30;j++){
				minus[i][j]=minus[i][j-1]+2*(i+1);
			}
		}
	}
	
	
	
	//plus
	for (i = n-1;i > tempr;i--) {
		if (i == n-1) {
			for(j=0;j<=30;j++){
				plus[i][j]=cfugo[i]+2*j;
			}
		}else{
			temp = 0;
			temp1 =c[i+1];
			temp2 =c[i];
			if(temp1<=temp2){
				while (temp1 < temp2) {
					temp++;
					temp1 = temp1 * 4;
				}
			}else if(temp1>temp2){
				while(temp1>temp2){
					temp--;
					temp2=temp2*4;
				}
			}
			
			for(j=0;j<=15;j++){
				plus[i][j]=plus[i+1][max(0,j+temp)]+cfugo[i]+2*j;
			}
			for(j=16;j<=30;j++){
				plus[i][j]=plus[i][j-1]+2*(n-i);
			}
		}
	}
	
//	for(i=0;i<n;i++){
//		for(j=0;j<5;j++){
//			printf("minus[%d][%d]=%lld,plus[%d][%d]=%lld\n",i,j,minus[i][j],i,j,plus[i][j]);
//		}
//		printf("\n");
//	}
	
	
	if (flag == 1) {
		ans = 0;
		if (templ != 0) {
			ans = ans + minus[templ - 1][0];
		}
		if (tempr != n - 1) {
			ans = ans + plus[tempr + 1][0];
		}
		printf("%lld", ans);
	}
	else {
		for (i = 0;i < n;i++) {
			if (i == 0) {
				ans = min(minus[n - 1][0], plus[0][0]);
			}
			else {
				if (minus[i - 1][0] + plus[i][0] < ans) {
					ans = minus[i - 1][0] + plus[i][0];
				}
			}
		}
		printf("%lld", ans);
	}

	//	printf("計算部分終了\n");
		//出力

		//printf("%lld",ans);


	//	printf("結果の出力終了\n");

	return 0;

}