import kotlin.math.max

fun main() {
    val n = readLine()!!.toInt()
    val a = readLine()!!.split(" ").map { it.toInt() }

    var r = 0
    var l = n - 1

    val done = MutableList(n) { false }
    var ans = 0L

    repeat(n) {
        val p = (0 until n).filter { !done[it] }
            .maxBy {
                a[it] * max(l - it, it - r)
            }!!

        done[p] = true;
        ans += a[p] * max(l - p, p - r)

        when {
            l - p > p - r -> l--
            else -> r++
        }
    }

    println(ans)
}