fun main(args: Array<String>) {
  val n = readLine()!!.toInt()
  val a = readLine()!!.split(" ").map { it.toLong() }.toLongArray()
  val p = MutableList<Pair<Int, Long>>(n) { it to a[it] }
  p.sortWith(compareBy({ -it.second }, { -kotlin.math.abs(0.5*(n-1)-it.first) }))
  var l = 0
  var r = n-1
  var x = 0L
  for (e in p) {
    val i = e.first
    val v = e.second
    if (i-l > r-i) {
      x += v*(i-l)
      l++
    } else {
      x += v*(r-i)
      r--
    }
  }
  println(x)
}