fun main(args: Array<String>) {
    val N = nextInt()
    val A = listOfInt().mapIndexed { index, a -> a to index }.toTypedArray()
    val B = A.sortedByDescending { it.first }.toTypedArray()
    val P = IntArray(N) { -1 }
    var ans = 0L
    var l = 0; var r = N - 1
    for (n in 0 until N) {
        val (v, p) = B[n]
        if (Math.abs(l - p) >= Math.abs(r - p)) {
            P[l] = v
            ans += v * Math.abs(l - p)
            l += 1
        } else {
            P[r] = v
            ans += v * Math.abs(r - p)
            r -= 1
        }
    }
    println(ans)
}

fun Int.isEven() = this % 2 == 0
operator fun LongArray.get(index: Long) = this[index.toInt()]
operator fun MutableList<Long>.get(index: Long) = this[index.toInt()]

fun next() = readLine()!!
fun nextInt(delta: Int = 0) = Integer.parseInt(next()) + delta
fun listOfString() = next().split(" ")
fun listOfInt(d: Int = 0) = listOfString().map { Integer.parseInt(it) + d }
fun listOfLong() = listOfString().map(String::toLong)
