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


fun main() {
    val sc = Scanner(System.`in`)
    val n = sc.nextInt()
    val infant = Array(n+1){0 to 0L}
    val dp = Array(n+1){LongArray(n+1){-1L} }

    for(i in 0 until n) {
        infant[i] = i to sc.nextLong()
    }

    infant.sortByDescending { it.second }

    fun solve(i:Int, j:Int): Long {
        val len = j - i + 1
        val idx = n - len
        val (p, a)  = infant[idx]
        if(len == 1) {
            return abs(i - p) * a
        }
        if(dp[i][j] != -1L) return dp[i][j]
        dp[i][j] = max(solve(i + 1, j) + abs(i - p) * a, solve(i, j - 1) + abs(j - p) * a)
        return dp[i][j]
    }
    val ans = solve(0, n -1)
    print(ans)
}