fun main(args : Array<String>) {
    val n = readLine()!!.toInt()
    val aa = readLine()!!.split(" ").mapIndexed { index, s -> Pair(index, s.toInt()) }.toMutableList()

    var max = 0L

    while (nextPermutation(aa)) {
        var tmp = 0L
        aa.forEachIndexed { index, pair ->
            tmp += pair.second * Math.abs(pair.first-index)
        }
        max = Math.max(max, tmp)
    }

    println(max)
}

fun nextPermutation(array: MutableList<Pair<Int, Int>>): Boolean {
    for (i in array.size-2 downTo 0) {
        if (array[i].first < array[i+1].first) {
            for (j in array.size-1 downTo i+1) {
                if (array[j].first > array[i].first) {
                    var tmp = array[i]
                    array[i] = array[j]
                    array[j] = tmp

                    val tmpArray = array.takeLast(array.size-(i+1)).sortedBy { it.first }
                    tmpArray.forEachIndexed { index, e ->
                        array[i+1+index] = e
                    }

                    return true
                }
            }
        }
    }

    return false
}