import kotlin.math.abs
import kotlin.math.max

class MaxHappinessCalculator(
    private val n: Int,
    private val a: Array<Long>
) {
    private val memo = Array(n + 1) { Array(n + 1) { null as Long?} }
    private val sorted = (0 until n).sortedByDescending { a[it] }

    private fun calcFromMemo(l: Int, r: Int): Long {
        val memoed = memo[l][r]
        if (memoed != null) {
            return memoed
        }

        val ans = calc(l, r)
        memo[l][r] = ans
        return ans
    }

    fun calc(l: Int, r: Int): Long {
        val m = l + r
        if (m == n) {
            return 0
        }

        val i = sorted[m]
        val lans = calcFromMemo(l + 1, r) + a[i] * abs(l - i)
        val rans = calcFromMemo(l, r + 1) + a[i] * abs((n - r - 1) - i)

        return max(lans, rans)
    }
}

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

    val ans = MaxHappinessCalculator(n, a).calc(0, 0)
    println(ans)
}
