import java.io.PrintWriter
import kotlin.math.abs
import java.util.ArrayDeque

fun main() {
    val pw = PrintWriter(System.out)

    val n = readInputLine().toInt()
    val aArr = readInputLine().split(" ").map { it.toLong() }.toLongArray()
    val ansArr = IntArray(n) { -1 }

    val queue = ArrayDeque<Int>()

    repeat(n) {
        queue.add(it)
    }

    while (queue.isNotEmpty()) {
        val p = queue.poll()!!
        for (i in n downTo 0) {
            if (p - i > 0) {
                if (ansArr[p - i] == -1) {
                    ansArr[p - i] = p
                    break
                } else {
                    val reserved = ansArr[p - i]
                    if (abs(reserved - (p - i)) * aArr[reserved] < abs(p - (p - i)) * aArr[p]) {
                        ansArr[p - i] = p
                        queue.add(reserved)
                        break
                    }
                }
            }
            if (p + i < n) {
                if (ansArr[p + i] == -1) {
                    ansArr[p + i] = p
                    break
                } else {
                    val reserved = ansArr[p + i]
                    if (abs(reserved - (p + i)) * aArr[reserved] < abs(p - (p + i)) * aArr[p]) {
                        ansArr[p + i] = p
                        queue.add(reserved)
                        break
                    }
                }
            }
        }
    }

    var ans = 0L

    repeat(n) {
        ans += abs(ansArr[it] - it) * aArr[it]
    }

    pw.println(ans)

    pw.flush()
}

fun readInputLine(): String {
    return readLine()!!
}
