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)

#[allow(unused_macros)]
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes
                .by_ref()
                .map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

#[allow(unused_macros)]
macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

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

#[allow(unused_macros)]
macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

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

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

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

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

#[allow(unused_macros)]
macro_rules! debug {
    ($($a:expr),*) => {
        #[cfg(debug_assertions)]
        writeln!(&mut std::io::stderr(), concat!("[DEBUG] ", $(stringify!($a), "={:?} "),*), $($a),*);
    }
}

#[allow(unused_imports)]
use std::cmp::{min, max};

#[allow(unused_imports)]
use std::io::Write;

fn f(a: usize, b: usize) -> (u32, u32) {
    if a <= b {
        let mut p = 0;
        while a*4usize.pow(p+1) <= b {
            p += 1;
        }
        (p, 0)
    } else {
        let mut p = 0;
        while a > b*4usize.pow(p) {
            p += 1;
        }
        (0, p)
    }
}

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

    // for &a in [2, 3, 10].iter() {
    //     for b in 1..20 {
    //         let (p1, p2) = f(a, b);
    //         println!("{},{} => {},{}", a, b, a*4usize.pow(p1), b*4usize.pow(p2));
    //     }
    // }

    // +
    let mut dp = vec![vec![0; 16]; n];
    for i in 0..16 {
        dp[n-1][i] = i;
    }
    for i in (0..n-1).rev() {
        for p in 0..16 {
            let a1 = aa[i];
            let a2 = aa[i+1];
            let (p1, p2) = f(a1, a2);
            let mut x = p2;
            if p > p1 {
                x += p - p1;
            }
            if x < 16 {
                dp[i][p as usize] = dp[i+1][x as usize] + p as usize;
                // debug!(a1, a2, p1, p2, i, p, i+1, x);
            } else {
                dp[i][p as usize] = dp[i+1][15] + p as usize + ((x as usize -15) * (n-i)) as usize;
            }
        }
    }

    // -
    let mut aa = aa;
    aa.reverse();
    for i in 0..n {
        aa[i] = aa[i] * 2;
    }

    let mut dp2 = vec![vec![0; 16]; n];
    for i in 0..16 {
        dp2[n-1][i] = i;
    }
    for i in (0..n-1).rev() {
        for p in 0..16 {
            let a1 = aa[i];
            let a2 = aa[i+1];
            let (p1, p2) = f(a1, a2);
            let mut x = p2;
            if p > p1 {
                x += p - p1;
            }
            if x < 16 {
                dp2[i][p as usize] = dp2[i+1][x as usize] + p as usize;
                // println!("dp[{}][{}] => dp[{}][{}] p1={} p2={}", i, p, i+1, x, p1, p2);
            } else {
                dp2[i][p as usize] = dp2[i+1][15] + p as usize + ((x as usize -15) * (n-i)) as usize;
            }
        }
    }

    let mut ans = min(dp[0][0] * 2, n + dp2[0][0]*2);
    for i in 1..n-1 {
        let c1 = dp[i][0] * 2;
        let c2 = dp2[n-i][0] * 2 + i;
        ans = min(c1+c2, ans);
    }
    println!("{}", ans);
}