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;

const X: usize = 16;

fn main() {
    let (r, w) = (std::io::stdin(), std::io::stdout());
    let mut sc = IO::new(r.lock(), w.lock());
    let n: usize = sc.read();
    let mut a: Vec<i64> = sc.vec(n);

    let suffix_dp = dp(&a);
    a.reverse();
    let prefix_dp = dp(&a);

    let mut ans = 1 << 60;
    for prefix_size in 0..(n + 1) {
        let suffix_size = n - prefix_size;
        let sum = if prefix_size == 0 {
            2 * suffix_dp[0][0]
        } else if suffix_size == 0 {
            2 * prefix_dp[0][0] + n
        } else {
            let p = prefix_dp[n - prefix_size][0];
            let s = suffix_dp[n - suffix_size][0];
            p * 2 + s * 2 + prefix_size
        };
        ans = cmp::min(ans, sum);
    }

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

fn dp(a: &Vec<i64>) -> Vec<Vec<usize>> {
    let n = a.len();

    let mut dp = vec![vec![0; X]; n];
    for x in 0..X {
        dp[n - 1][x] = x;
    }
    for i in (0..(n - 1)).rev() {
        for x in 0..X {
            let mut cur = a[i];
            for _ in 0..x {
                cur *= 4;
            }
            let mut next_x = 0;
            let mut next = a[i + 1];
            while cur > next {
                next *= 4;
                next_x += 1;
            }
            if next_x < X {
                dp[i][x] = dp[i + 1][next_x] + x;
            } else {
                let remain_x = next_x - (X - 1);
                dp[i][x] = dp[i + 1][X - 1] + remain_x * (n - 1 - i) + x;
            }
        }
    }
    dp
}

pub struct IO<R, W: std::io::Write>(R, std::io::BufWriter<W>);

impl<R: std::io::Read, W: std::io::Write> IO<R, W> {
    pub fn new(r: R, w: W) -> IO<R, W> {
        IO(r, std::io::BufWriter::new(w))
    }
    pub fn write<S: std::ops::Deref<Target = str>>(&mut self, s: S) {
        use std::io::Write;
        self.1.write(s.as_bytes()).unwrap();
    }
    pub fn read<T: std::str::FromStr>(&mut self) -> T {
        use std::io::Read;
        let buf = self
            .0
            .by_ref()
            .bytes()
            .map(|b| b.unwrap())
            .skip_while(|&b| b == b' ' || b == b'\n' || b == b'\r' || b == b'\t')
            .take_while(|&b| b != b' ' && b != b'\n' && b != b'\r' && b != b'\t')
            .collect::<Vec<_>>();
        unsafe { std::str::from_utf8_unchecked(&buf) }
            .parse()
            .ok()
            .expect("Parse error.")
    }
    pub fn vec<T: std::str::FromStr>(&mut self, n: usize) -> Vec<T> {
        (0..n).map(|_| self.read()).collect()
    }
    pub fn chars(&mut self) -> Vec<char> {
        self.read::<String>().chars().collect()
    }
}