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)

// https://atcoder.jp/contests/caddi2018/tasks/caddi2018_c
//
#![allow(unused_imports)]
use std::io::*;
use std::fmt::*;
use std::str::*;
use std::cmp::*;
use std::collections::*;

macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        let s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

macro_rules! input_inner {
    ($iter:expr) => {};
    ($iter:expr, ) => {};

    ($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };
}

macro_rules! read_value {
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(read_value!($iter, $t)),* )
    };

    ($iter:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
    };

    ($iter:expr, [ next / $t:tt ]) => {
        {
            let len = read_value!($iter, usize);
            (0..len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
        }
    };

    ($iter:expr, chars) => {
        read_value!($iter, String).chars().collect::<Vec<char>>()
    };

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

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

#[allow(unused_macros)]
macro_rules! dvec {
    ($t:expr ; $len:expr) => {
        vec![$t; $len]
    };

    ($t:expr ; $len:expr, $($rest:expr),*) => {
        vec![dvec!($t; $($rest),*); $len]
    };
}

#[allow(unused_macros)]
macro_rules! ifv {
    ($t:expr, $a:expr, $b: expr) => {
        if $t { $a } else { $b }
    }
}

#[allow(unused_macros)]
macro_rules! fill {
    ($t:expr, $v:expr) => {
        for i in 0..$t.len() {
            $t[i] = $v;
        }
    };
}

#[allow(unused_macros)]
macro_rules! debug {
    ($($a:expr),*) => {
        println!(concat!($(stringify!($a), " = {:?}, "),*), $($a),*);
    }
}

fn step(mut a: i64, mut b: i64) -> (i64, bool) {
    if a <= b {
        (0, a == b)
    } else {
        let mut cnt = 0;
        while b < a {
            b *= 4;
            cnt += 1;
        }
        (cnt, a == b)
    }
}

const INF: i64 = 1e12 as i64;

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

    let mut dp = vec![0; n+1];
    let mut last = a[n-1];
    let mut deq = VecDeque::new();
    deq.push_back((INF, n-1));
    for i in (0..n-1).rev() {
        dp[i] = dp[i+1];

        let (mut c, sm) = step(a[i], last);
        if c == 0 {
            let (c0, sm) = step(last, a[i]);
            if c0 >= 1 {
                deq.push_front((c0, i));
            }
            last = a[i];
            continue;
        }
        // debug!(c, sm, a[i], last);

        last = a[i];

        let mut now = i;
        let mut total = 0;
        let mut rem = 0;
        let mut tweaklast = false;
        for w in 0..deq.len() {
            let &(cnt, idx) = deq.get(w).unwrap();
            total += ((idx - now) as i64) * c;
            if cnt >= c {
                if cnt == c {
                    rem += 1;
                } else {
                    tweaklast = true;
                }
                break;
            } else {
                c -= cnt;
                rem += 1;
            }
        }
        for l in 0..rem {
            deq.pop_front();
        }
        if tweaklast {
            let (c1, idx) = deq.pop_front().unwrap();
            deq.push_back((c1-c, idx));
        }
        dp[i] += total;
    }
    dp
}


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

    let front = solve(a.clone());
    let mut a = a;
    for i in 0..n {
        a[i] *= 2;
    }
    a.reverse();
    let back = solve(a);

    // debug!(front,back);

    let mut best = INF;
    for f in 0..n {
        let take_f = n-f;
        let take_b = n-take_f;
        let b = n-take_b;

        best = min(best, 2 * (front[f] + back[b]) + (take_b as i64));
    }
    println!("{}", best);
}