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 L: usize = 16;
const MAX_A: u64 = 1 << 32;

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<usize> = sc.vec(n);

    let dp1 = solve(&a);
    a.reverse();
    let dp2 = solve(&a);

    let mut cost = 1 << 50;
    for prefix in 0..(n + 1) {
        let prefix_cost = if prefix > 0 {
            dp1[prefix - 1][0] * 2 + prefix
        } else {
            0
        };
        let suffix_cost = if prefix < n {
            dp2[n - 1 - prefix][0] * 2
        } else {
            0
        };
        cost = cmp::min(prefix_cost + suffix_cost, cost);
    }

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

fn solve(a: &Vec<usize>) -> Vec<Vec<usize>> {
    let n = a.len();
    let mut dp = vec![vec![0; L]; n];
    for i in 0..L {
        dp[0][i] = i;
    }
    for i in 1..n {
        let mut cur = 1;
        let mut prev_l = 0;
        let mut prev_a = a[i - 1];
        for l in 0..L {
            let cur_a = cur * a[i];
            while prev_a < cur_a {
                prev_a *= 4;
                prev_l += 1;
            }
            if prev_l >= L {
                dp[i][l] = dp[i - 1][L - 1] + i * (prev_l - (L - 1)) + l;
            } else {
                dp[i][l] = dp[i - 1][prev_l] + l;
            }
            cur *= 4;
        }
    }
    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: ToString>(&mut self, s: S) {
        use std::io::Write;
        self.1.write(s.to_string().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()
    }
}