import kotlin.math.max

data class Infant(val a: Long, val origPos: Long)

fun main() {
    val builder = StringBuilder()

    val n = readInputLine().toInt()

    val aArr = readInputLine().split(" ").mapIndexed() { index, s ->
        Infant(s.toLong(), index.toLong() + 1L)
    }.sortedByDescending { it.a }.toTypedArray()

    val dp = Array(n + 1) { LongArray(n + 1) }

    dp[0][0] = 0L

    for ((index, infant) in aArr.withIndex()) {
        val numInfant = index + 1
        for (j in 1 until numInfant) {
            dp[j][numInfant - j] = max(
                dp[j - 1][numInfant - j] + infant.a * (infant.origPos - j),
                dp[j][numInfant - j - 1] + infant.a * ((n - (numInfant - j) + 1) - infant.origPos)
            )
        }

        dp[numInfant][0] = dp[numInfant - 1][0] + infant.a * (infant.origPos - numInfant)
        dp[0][numInfant] = dp[0][numInfant - 1] + infant.a * ((n - numInfant + 1) - infant.origPos)
    }

    var ans = 0L

    repeat(n) {
        ans = max(ans, dp[it][n - it])
    }

    builder.appendln(ans)

    print(builder.toString())
}

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