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: Wrong Answer)

use std::io::*;
use std::str::FromStr;
use utils::*;

pub fn main() {
    let i = stdin();
    let mut o = Vec::new();
    run(i.lock(), &mut o);
    stdout().write_all(&o).unwrap();
}

fn run<R: BufRead, W: Write>(i: R, o: &mut W) {
    let mut i = CpReader::new(i);
    let n = i.read::<usize>();
    let a = i.read_vec::<usize>(n);

    writeln!(o, "{}", solve(a)).unwrap();
}
fn solve(a: Vec<usize>) -> usize {
    struct Solver {
        a: Vec<usize>,
        m: Vec<Vec<Option<usize>>>,
    }
    impl Solver {
        fn new(a: Vec<usize>) -> Self {
            let len = a.len();
            Solver {
                a: a,
                m: vec![vec![None; len]; 16],
            }
        }
        // i-1番目(0-origin)の項目をc*2回-2倍して、
        // j in [0,i-1) の項目の絶対値を |a[j]| => |a[j+1]| にするための `-2倍の 適用回数/2 ` を返す
        fn get(&mut self, i: usize, c: usize) -> usize {
            if i == 0 {
                return 0;
            }
            if i == 1 {
                return c;
            }

            if c >= self.m.len() {
                let d = c - self.m.len() + 1;
                return self.get(i, c - d) + d;
            }
            if let Some(value) = self.m[c][i - 2] {
                return value;
            }
            let mut c_next = c;
            let mut a0 = self.a[i - 2];
            let mut a1 = self.a[i - 1];
            while a0 < a1 {
                a0 *= 4;
                c_next += 1;
            }
            while c_next > 0 && a0 * 4 >= a1 {
                a1 *= 4;
                c_next -= 1;
            }
            let value = c + self.get(i - 1, c_next);
            self.m[c][i - 1] = Some(value);
            value
        }
    }
    let a_len = a.len();

    let a0 = a.clone();
    let mut a1 = a;
    a1.reverse();

    let mut s0 = Solver::new(a0);
    let mut s1 = Solver::new(a1);

    (0..a_len)
        .map(|x| s0.get(x, 0) * 2 + x + s1.get(a_len - x, 0) * 2)
        .min()
        .unwrap()
}

mod utils {
    use super::*;

    pub struct CpReader<R: BufRead> {
        r: R,
        s: String,
    }

    macro_rules! fn_read {
        { $f:ident($($t:ident),*) } => {
            #[allow(dead_code)]
            pub fn $f<$($t: FromStr),*>(&mut self) -> ($($t),*) {
                let i = &mut self.read_line().split(' ');
                ($(read_next::<$t,_>(i)),*)
            }
        };
    }

    impl<R: BufRead> CpReader<R> {
        pub fn new(r: R) -> Self {
            CpReader {
                r: r,
                s: String::new(),
            }
        }
        pub fn read_line(&mut self) -> &str {
            self.s.clear();
            self.r.read_line(&mut self.s).unwrap();
            self.s.trim()
        }

        pub fn read<T: FromStr>(&mut self) -> T {
            self.read_line().parse().ok().unwrap()
        }
        fn_read! { read2(T1, T2) }
        fn_read! { read3(T1, T2, T3) }
        fn_read! { read4(T1, T2, T3, T4) }
        fn_read! { read5(T1, T2, T3, T4, T5) }

        pub fn read_vec<T: FromStr>(&mut self, n: usize) -> Vec<T> {
            self.read_line()
                .split(' ')
                .take(n)
                .map(|x| x.parse().ok().unwrap())
                .collect()
        }
    }
    fn read_next<'a, T: FromStr, I: Iterator<Item = &'a str>>(i: &mut I) -> T {
        i.next().unwrap().parse().ok().unwrap()
    }

}