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, bytes) => {
        read_value!($next, String).into_bytes()
    };

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

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

// extern crate rand;
// use rand::*;

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

    // let mut a = vec![0; 10000];
    // for i in 0..a.len() {
    //     a[i] = thread_rng().gen_range(1, 200000);
    // }
    let fa = a.iter().map(|v| (*v as f64).log(4.0)).collect::<Vec<_>>();

    // let mut true_ans = (0, 99999999);
    // for i in 0..a.len() {
    //     let t = calc(&fa, i);
    //     if true_ans.1 as i64 > t {
    //         true_ans = (i, t);
    //     }
    //     // println!("{} {}", i, t);
    // }

    let mut lo = 0;
    let mut hi = a.len() - 1;

    while (hi - lo) > 2000 {
        let m1 = (lo * 2 + hi) / 3;
        let m2 = (lo + hi * 2) / 3;

        if calc(&fa, m1) < calc(&fa, m2) {
            hi = m2;
        } else {
            lo = m1;
        }
    }

    let mut ans = calc(&fa, lo);
    // println!("{} {}", lo, hi);
    for i in lo+1..hi+1 {
        // println!("{}: {:?}", i, calc(&fa, i));
        ans = std::cmp::min(ans, calc(&fa, i))
    }

    // println!("{:?}, {}", true_ans, ans);

    println!("{}", ans);
}

fn calc(fa: &[f64], i: usize) -> i64 {
    let n = fa.len();

    let mut ans = 0;

    let mut cur = fa[i];
    let mut minus = false;
    for j in (0..i).rev() {
        if !minus {
            if cur >= fa[j] {
                cur = fa[j];
            } else {
                ans += 1;
                cur = fa[j] + 0.5;
                minus = true;
            }
        } else {
            ans += 1;
            let nn = fa[j] + 0.5;

            if cur <= nn {
                cur = nn;
            } else {
                let inc = (cur - nn).ceil() as i64;
                ans += inc * 2;
                cur = nn + inc as f64;
            }
        }
    }

    let mut cur = fa[i];
    for j in (i + 1)..n {
        // println!("{}: {}", j, cur);

        if cur <= fa[j] {
            cur = fa[j];
        } else {
            let inc = (cur - fa[j]).ceil() as i64;
            ans += inc * 2;
            cur = fa[j] + inc as f64;
        }

        // println!("{}: inc = {}, cur = {}", j, inc, cur);
    }

    // println!("*** {}: {}", i, ans);

    ans
}