fun main(args: Array<String>) {
    val n = readLine()!!.toInt()
    val a = readLine()!!.split(" ").map { it.toLong() }
    var min = 0
    var max = n - 1
    val searched = IntArray(n){0}
    var sum = 0L
    while(min <= max){
        val minArray = LongArray(n){0}
        val maxArray = LongArray(n){0}
        for(i in 0 until n){
            if(searched[i] != 0) continue
            minArray[i] = abs((i - min).toDouble()).toLong() * a[i]
            maxArray[i] = abs((i - max).toDouble()).toLong() * a[i]
        }
        val minMax = minArray.max()
        val maxMax = maxArray.max()
        if(minMax!! >= maxMax!!){
            val index = minArray.indexOf(minMax)
            searched[index] = 1
            sum += minMax!!
            min++
        }else{
            val index = maxArray.indexOf(maxMax)
            searched[index] = 1
            sum += maxMax!!
            max--
        }
    }
    println(sum)
}
