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 Scala (2.11.7) (judge verdict: Wrong Answer)

object Main extends App {

  import scala.io.StdIn._
  import scala.math._

  implicit def toWith(value: Long): NumberWithExponent = value.toWithExp
  implicit class Converter(private val value : Long){
    def toWithExp: NumberWithExponent = NumberWithExponent(value, 0)
  }
  val n = readLine.trim.toInt
  val numbers = readLine.trim.split(' ').map(_.toLong)
  val minus = minCountOfSortWithMinusTo(numbers).toList
  val plus = minCountOfSortWithPlusAfter(numbers).toList
  println(
    (0::minus).zip(plus ++ List(0)).map{case (l, r) ⇒ l + r}.min
  )
  def minCountOfSortWithMinusTo(numbers: Array[Long]): Array[Int] = {
    var list = Nil: List[MinusSortedSequence]
    numbers.map{right ⇒
      val (c, n) = addMinus(right, list)
      list = n
      c
    }.also{arr ⇒
      for (i ← 1 until arr.length){
        arr(i) += arr(i - 1)
      }
    }
  }
  def minCountOfSortWithPlusAfter(numbers: Array[Long]): Array[Int] = {
    var list = Nil: List[PlusSortedSequence]
    numbers.reverse.map{left ⇒
      val (c, n) = addPlus(left, list)
      list = n
      c
    }.also{arr ⇒
      for (i ← 1 until arr.length){
        arr(i) += arr(i - 1)
      }
    }.reverse
  }

  def addPlus(addLeft: Long, prev: List[PlusSortedSequence] = Nil): (Int, List[PlusSortedSequence]) = {
    prev match {
      case Nil if addLeft > 0 ⇒ (0, PlusSortedSequence(addLeft, addLeft, 1)::Nil)
      case Nil ⇒ (1, PlusSortedSequence(addLeft * -2, addLeft * -2, 1)::Nil)
      case PlusSortedSequence(left, right, length)::t if left >= addLeft && addLeft >= 0 ⇒
        if (left >= addLeft * 4) (0, PlusSortedSequence(addLeft, addLeft, 1)::prev)
        else (0, PlusSortedSequence(addLeft, right, length + 1)::t)
      case PlusSortedSequence(left, _, _)::_ if left < addLeft ⇒
        val (count, next) = increaseWhile(prev, addLeft)
        addPlus(addLeft, next).let{case (c, n) ⇒ (c + count, n)}
      case _ ⇒ addPlus(addLeft * -2, prev).let{case (c, n) ⇒ (c + 1, n)}
    }
  }
  def addMinus(addRight: Long, prev: List[MinusSortedSequence] = Nil): (Int, List[MinusSortedSequence]) = {
    prev match{
      case Nil if addRight < 0 ⇒ (0, MinusSortedSequence(addRight, addRight, 1)::Nil)
      case Nil ⇒ (1, MinusSortedSequence(addRight * -2, addRight * -2, 1)::Nil)
      case MinusSortedSequence(left, right, length)::t if right <= addRight && addRight <= 0 ⇒
        if (right <= addRight * 4) (0, MinusSortedSequence(addRight, addRight, 1)::prev)
        else (0, MinusSortedSequence(left, addRight, length + 1)::t)
      case MinusSortedSequence(_, right, _)::_ if addRight < right ⇒
        val (count, next) = decreaseWhile(prev, addRight)
        addMinus(addRight, next).let{case (c, n) ⇒ (c + count, n)}
      case _ ⇒ addMinus(addRight * -2, prev).let{case (c, n) ⇒ (c + 1, n)}
    }
  }
  def increaseWhile(list: List[PlusSortedSequence], target: NumberWithExponent): (Int, List[PlusSortedSequence]) = {
    list match {
      case h::_ if h.left < target ⇒ increaseWhile(increase(list), target).let{case (c, l) ⇒ (c + h.length * 2, l)}
      case _ ⇒ (0, list)
    }
  }
  def decreaseWhile(list: List[MinusSortedSequence], target: NumberWithExponent): (Int, List[MinusSortedSequence]) = {
    list match {
      case h::_ if h.right > target ⇒
        decreaseWhile(decrease(list), target).let{case (c, l) ⇒ (c + h.length * 2, l)}
      case _ ⇒ (0, list)
    }
  }
  def increase(list: List[PlusSortedSequence]): List[PlusSortedSequence] = {
    list match {
      case PlusSortedSequence(left, r, len1)::PlusSortedSequence(l, right, len2)::t if r.copy(exponent = r.exponent + 4) > l ⇒ PlusSortedSequence(left.copy(exponent = left.exponent + 2), right, len1 + len2)::t
      case PlusSortedSequence(left, right, length)::t ⇒ PlusSortedSequence(left.copy(exponent = left.exponent + 2), right.copy(exponent = right.exponent + 2), length)::t
      case Nil ⇒ Nil
    }
  }
  def decrease(list: List[MinusSortedSequence]): List[MinusSortedSequence] = {
    list match {
      case MinusSortedSequence(l, right, length)::MinusSortedSequence(left, r, len)::t if l.copy(exponent = l.exponent + 4) > r ⇒ MinusSortedSequence(left, right.copy(exponent = right.exponent + 2), length + len)::t
      case MinusSortedSequence(left, right, length)::t ⇒  MinusSortedSequence(left.copy(exponent = left.exponent + 2), right.copy(exponent = right.exponent + 2), length)::t
      case Nil ⇒ Nil
    }
  }
  case class PlusSortedSequence(left: NumberWithExponent, right: NumberWithExponent, length: Int)
  case class MinusSortedSequence(left: NumberWithExponent, right: NumberWithExponent, length: Int)
  case class NumberWithExponent(value: Long, exponent: Int) extends Ordered[NumberWithExponent] {
    override def compare(that: NumberWithExponent): Int = toLogWithSign.compare(that.toLogWithSign)
    private val sign: Int = math.signum(value).toInt * (if (exponent % 2 == 1) -1:Int else 1: Int)
    private val toLogWithSign: Double = (math.log(math.abs(value)) + math.log(2) * exponent) * sign
  }
  implicit class Extension[T](val value :T){
    def also(func: T ⇒ Unit): T = {
      func(value)
      value
    }
    def let[R](func: T ⇒ R): R = func(value)
  }
}