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 Rust (1.15.1) (judge verdict: Accepted)

// https://atcoder.jp/contests/caddi2018/tasks/caddi2018_c
//
#![allow(unused_imports)]
use std::io::*;
use std::fmt::*;
use std::str::*;
use std::cmp::*;
use std::collections::*;

macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        let s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

macro_rules! input_inner {
    ($iter:expr) => {};
    ($iter:expr, ) => {};

    ($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };
}

macro_rules! read_value {
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(read_value!($iter, $t)),* )
    };

    ($iter:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
    };

    ($iter:expr, [ next / $t:tt ]) => {
        {
            let len = read_value!($iter, usize);
            (0..len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
        }
    };

    ($iter:expr, chars) => {
        read_value!($iter, String).chars().collect::<Vec<char>>()
    };

    ($iter:expr, usize1) => {
        read_value!($iter, usize) - 1
    };

    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}

#[allow(unused_macros)]
macro_rules! dvec {
    ($t:expr ; $len:expr) => {
        vec![$t; $len]
    };

    ($t:expr ; $len:expr, $($rest:expr),*) => {
        vec![dvec!($t; $($rest),*); $len]
    };
}

#[allow(unused_macros)]
macro_rules! ifv {
    ($t:expr, $a:expr, $b: expr) => {
        if $t { $a } else { $b }
    }
}

#[allow(unused_macros)]
macro_rules! fill {
    ($t:expr, $v:expr) => {
        for i in 0..$t.len() {
            $t[i] = $v;
        }
    };
}

#[allow(unused_macros)]
macro_rules! debug {
    ($($a:expr),*) => {
        println!(concat!($(stringify!($a), " = {:?}, "),*), $($a),*);
    }
}

fn step(mut a: i64, mut b: i64) -> (i64, bool) {
    if a <= b {
        (0, a == b)
    } else {
        let mut cnt = 0;
        while b < a {
            b *= 4;
            cnt += 1;
        }
        (cnt, a == b)
    }
}

fn diff(mut a: i64, mut b: i64) -> i64 {
    let mut d = 0;
    if a > b {
        while b < a {
            b *= 4;
            d += 1;
        }
        -d
    } else {
        while a <= b {
            a *= 4;
            d += 1;
        }
        d - 1
    }
}

fn form_p4(mut a: i64) -> (i64, i64) {
    let mut a4 = 1;
    while a >= 4 {
        a /= 4;
        a4 += 1;
    }
    (a4, a)
}

/// Returns minimum pb such that a * 4^pa <= b * 4^pb holds.
fn compare(mut a: i64, mut pa: i64, mut b: i64) -> i64 {
    let dab = diff(a, b) - pa;
    if dab < 0 {
        -dab
    } else {
        0
    }
}

const INF: i64 = 1e12 as i64;
const MAX_POW: usize = 16;

fn solve(a: Vec<i64>) -> Vec<i64> {
    let n = a.len();

    let mut dp = dvec!(0i64; n+1, MAX_POW);
    for i in 0..MAX_POW {
        dp[n-1][i] = i as i64;
    }

    for i in (0..n-1).rev() {
        for d in 0..MAX_POW {
            let req = compare(a[i], d as i64, a[i+1]) as usize;
            if req < MAX_POW {
                dp[i][d] = (d as i64) + dp[i+1][req];
            } else {
                dp[i][d] = (d as i64) + (dp[i+1][15] + ((req - 15) * (n - i - 1)) as i64);
            }
        }
    }

    let mut ret = vec![0; n+1];
    for i in 0..n+1 {
        ret[i] = dp[i][0];
    }
    ret
}


fn main() {
    input! {
        n: usize,
        a: [i64; n]
    };

    let front = solve(a.clone());
    let mut a = a;
    for i in 0..n {
        a[i] *= 2;
    }
    a.reverse();
    let back = solve(a);

    // debug!(front,back);

    let mut best = INF;
    for f in 0..n {
        let take_f = n-f;
        let take_b = n-take_f;
        let b = n-take_b;

        best = min(best, 2 * (front[f] + back[b]) + (take_b as i64));
    }
    println!("{}", best);
}