import java.util.*
import kotlin.math.abs


data class Infant(val idx:Int, val target:Int, val happy:Long)

fun main() {
    val sc = Scanner(System.`in`)
    val n = sc.nextInt()
    val infants = LongArray(n + 1)

    for(i in 1..n) {
        infants[i] = sc.nextLong()
    }

    val pq = PriorityQueue<Infant>(kotlin.Comparator { o1, o2 -> o2.happy.compareTo(o1.happy)})

    for(i in 1..n) {
        val d1 = abs(i - 1).toLong() * infants[i]
        val d2 = abs(n - i).toLong() * infants[i]
        if(d1 > d2) {
            pq.add(Infant(i, 1, d1))
        } else {
            pq.add(Infant(i, n, d2))
        }
    }

    val checked = BooleanArray(n + 1)
    val checkedInfant = BooleanArray(n+1)

    var ans = 0L

    while (pq.isNotEmpty()) {
        val infant = pq.poll()
        if(checkedInfant[infant.idx]) {
            continue
        }
        if(checked[infant.target]) {
            var bestHappy = 0L
            var bestIdx = 0
            for(i in 1..n) {
                if(checked[i]) continue
                val d = abs(infant.idx - i).toLong() * infants[infant.idx]
                if(d > bestHappy) {
                    bestHappy = d
                    bestIdx = i
                }
            }
            pq.add(Infant(infant.idx, bestIdx, bestHappy))
            continue
        }
        ans += infant.happy
        checked[infant.target] = true
        checkedInfant[infant.idx] = true
    }
    print(ans)


}