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)

use std::cmp::*;
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 counts(a: &[usize]) -> Vec<usize> {
    let mut rs = Vec::with_capacity(a.len() + 1);

    rs.push(0);
    rs.push(0);

    let mut ss = ([0; 16], [0; 16]);
    let mut ss = (&mut ss.0, &mut ss.1);

    for c in 0..ss.0.len() {
        ss.0[c] = c;
    }

    for i in 1..a.len() {
        let mut d = 0isize;
        let mut a0 = a[i - 1];
        let mut a1 = a[i];
        while a0 < a1 {
            a0 *= 4;
            d += 1;
        }
        while a0 >= a1 * 4 {
            a1 *= 4;
            d -= 1;
        }
        for c in 0..ss.1.len() {
            let mut c_next = max(c as isize + d, 0) as usize;
            let mut r = c;
            if c_next >= ss.0.len() {
                let d = c_next - ss.0.len() + 1;
                r += i * d + ss.0.last().unwrap();
                for c in c..ss.1.len() {
                    ss.1[c] = r;
                    r += i + 1;
                }
                break;
            }
            ss.1[c] = r + ss.0[c_next];
        }
        rs.push(ss.1[0]);
        std::mem::swap(&mut ss.0, &mut ss.1);
    }
    rs
}
fn solve(mut a: Vec<usize>) -> usize {
    let cs0 = counts(&a);
    a.reverse();
    let cs1 = counts(&a);

    (0..a.len())
        .map(|x| cs0[x] * 2 + x + cs1[a.len() - x] * 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()
    }

}