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 solve(pos: usize, k: i64, a: &[i64], rtbl: &mut Vec<Vec<Option<i64>>>) -> i64 {
    if pos + 1 >= a.len() {
        return 0;
    }

    if k >= 16 {
        return solve(pos, 15, a, rtbl) + (k - 15) * (a.len() - pos - 1) as i64;
    }

    if let &Some(ret) = &rtbl[pos][k as usize] {
        return ret;
    }

    // a[pos] * 4^k <= a[pos+1] * 4^m
    // a[pos] <= a[pos+1] * 4^(m - k)

    let d = if a[pos] < a[pos + 1] {
        let mut d = 0;
        while a[pos] * 4i64.pow(d) < a[pos + 1] {
            d += 1
        }
        -(d as i64 - 1)
    } else {
        let mut d = 0;
        while a[pos] > a[pos + 1] * 4i64.pow(d) {
            d += 1
        }
        d as i64
    };

    let nk = d + k;
    let nk = std::cmp::max(nk, 0);

    // println!("* {} * 4^{} <= {} * 4^{}", a[pos], k, a[pos + 1], nk);

    let ret = solve(pos + 1, nk, a, rtbl) + nk;

    rtbl[pos][k as usize] = Some(ret);
    ret
}

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

    let mut ra = a.clone();
    ra.reverse();

    let mut rtbl = vec![vec![None; 16]; n];
    let mut ltbl = vec![vec![None; 16]; n];

    let mut ans = 999999999;

    for i in 0..n {
        let cur = solve(i, 0, &a, &mut rtbl) * 2 + solve(n - i, 0, &ra, &mut ltbl) * 2 + i as i64;
        // println!(
        //     "{}: {} = {} + {}",
        //     i,
        //     cur,
        //     solve(i, 0, &a, &mut rtbl),
        //     solve(n - i, 0, &ra, &mut ltbl) + i as i64
        // );
        ans = std::cmp::min(ans, cur);
    }

    // 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);

    // let mut v = vec![];

    // for i in 0..1000 {
    //     let lo = (n * i) / 1000;
    //     let hi = (n * (i + 1)) / 1000;
    //     let mi = (lo + hi) / 2;

    //     v.push((calc(&fa, mi), mi));
    // }

    // v.sort();

    // let mut ans = 9999999999;

    // for &(_, mi) in v[0..10].iter() {
    //     for i in mi..mi + 100 {
    //         if i < 50 {
    //             continue;
    //         }
    //         if i - 50 >= fa.len() {
    //             continue;
    //         }
    //         ans = std::cmp::min(ans, calc(&fa, i - 50));
    //     }
    // }

    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
}