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)

use std::cmp;

fn main() {
    let s = std::io::stdin();
    let mut sc = Scanner { reader: s.lock() };
    let n: usize = sc.read();
    let a: Vec<i64> = sc.read_vec(n);

    let mut reverse: Vec<i64> = a.iter().map(|&a| -a).collect();
    reverse.reverse();
    let prefix = gen(&a);
    let suffix = gen_suffix(&a);

    let mut ans: usize = 1e16 as usize;
    for i in 0..(n + 1) {
        ans = cmp::min(ans, prefix[i] + suffix[i]);
    }
    println!("{}", ans);
}

fn gen_suffix(a: &Vec<i64>) -> Vec<usize> {
    let n = a.len();
    let mut suffix = vec![0; n + 1];
    let mut stack: Vec<(i64, usize)> = vec![];
    for i in (0..n).rev() {
        let cur = a[i];
        let mut cost = 0;
        while !stack.is_empty() && stack[stack.len() - 1].0 < cur {
            let mut head = stack.pop().unwrap();
            head.0 *= 4;
            cost += 2 * head.1;
            if !stack.is_empty() && stack[stack.len() - 1].0 < cur * 4 {
                let s = stack.len() - 1;
                stack[s].0 = head.0;
                stack[s].1 += head.1;
            } else {
                stack.push(head);
            }
        }
        if stack.is_empty() || stack[stack.len() - 1].0 >= cur * 4 {
            stack.push((cur, 1));
        } else {
            let mut head = stack.pop().unwrap();
            head.0 = cur;
            head.1 += 1;
            stack.push(head);
        }
        suffix[i] = suffix[i + 1] + cost;
    }

    suffix
}

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

    let mut prefix = vec![0; n + 1];
    let mut stack: Vec<(i64, usize)> = vec![];
    for i in 0..n {
        let cur = -2 * a[i];
        let mut cost = 1;
        while !stack.is_empty() && stack[stack.len() - 1].0 > cur {
            let mut tail = stack.pop().unwrap();
            tail.0 *= 4;
            cost += 2 * tail.1;
            if !stack.is_empty() && stack[stack.len() - 1].0 > cur * 4 {
                let s = stack.len() - 1;
                stack[s].0 = tail.0;
                stack[s].1 += tail.1;
            } else {
                stack.push(tail);
            }
        }
        if stack.is_empty() || stack[stack.len() - 1].0 <= cur * 4 {
            stack.push((cur, 1));
        } else {
            let mut x = stack.pop().unwrap();
            x.0 = cur;
            x.1 += 1;
            stack.push(x);
        }
        prefix[i + 1] = cost + prefix[i];
    }
    prefix
}

pub struct Scanner<R> {
    reader: R,
}

impl<R: std::io::Read> Scanner<R> {
    pub fn read<T: std::str::FromStr>(&mut self) -> T {
        use std::io::Read;
        let buf = self
            .reader
            .by_ref()
            .bytes()
            .map(|b| b.unwrap())
            .skip_while(|&b| b == b' ' || b == b'\n')
            .take_while(|&b| b != b' ' && b != b'\n')
            .collect::<Vec<_>>();
        unsafe { std::str::from_utf8_unchecked(&buf) }
            .parse()
            .ok()
            .expect("Parse error.")
    }
    pub fn read_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()
    }
}