Problem:
Score : 500 points

Problem Statement

There are N children standing in a line from left to right. The activeness of the i-th child from the left is A_i.

You can rearrange these children just one time in any order you like.

When a child who originally occupies the x-th position from the left in the line moves to the y-th position from the left, that child earns A_x \times |x-y| happiness points.

Find the maximum total happiness points the children can earn.

Constraints

2 \leq N \leq 2000

1 \leq A_i \leq 10^9

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the maximum total happiness points the children can earn.

Sample Input 1

4
1 3 4 2

Sample Output 1

20

If we move the 1-st child from the left to the 3-rd position from the left, the 2-nd child to the 4-th position, the 3-rd child to the 1-st position, and the 4-th child to the 2-nd position, the children earns 1 \times |1-3|+3 \times |2-4|+4 \times |3-1|+2 \times |4-2|=20 happiness points in total.

Sample Input 2

6
5 5 6 1 1 1

Sample Output 2

58

Sample Input 3

6
8 6 9 1 2 1

Sample Output 3

85

Solution in Kotlin (1.3.71) (judge verdict: Time Limit Exceeded)

import java.lang.Math.abs

fun main(args: Array<String>) = ABC163E.main()

object ABC163E {
    fun log(message: Any?) {
        println(message)// TODO: ←消す
    }

    data class Problem(val items: List<Infant>, val lefts: Set<Infant>, val rights: Set<Infant>) {
        val minPos: Int get() = lefts.size
        val maxPos: Int get() = lefts.size + items.size - 1
        fun getOneChoice(): Long? {
            return when {
                items.isEmpty() -> 0L
                else -> null
            }
        }

        override fun toString(): String {
            return "${lefts.joinToString(" ") { it.value.toString() }}|${
            items.sortedBy { it.pos }.joinToString(" ") { it.value.toString() }
            }|${rights.reversed().joinToString(" ") { it.value.toString() }}"
        }

        override fun hashCode(): Int {
            var result = items.size
            result = 31 * result + lefts.size
            result = 31 * result + rights.size
            return result
        }
    }


    fun main() {
        val N = readInt()
        val A = readLongs()

        val items = A.mapIndexed { index, it -> Infant(pos = index, value = it) }
            .sortedByDescending { it.value }
        //log(items.joinToString("\n"))

        val table = LazyMap<Problem, Long>(N * N) { problem ->
            problem.getOneChoice() ?: problem.let {
                val infant = it.items.first()
                // generate subProblem
                val left = Problem(
                    items = it.items.drop(1),
                    lefts = it.lefts + infant,
                    rights = it.rights
                )
                val right = Problem(
                    items = it.items.drop(1),
                    lefts = it.lefts,
                    rights = it.rights + infant
                )

                // generate choices
                val leftValue = get(left) + infant.value * abs(infant.pos - it.minPos)
                val rightValue = get(right) + infant.value * abs(it.maxPos - infant.pos)

                // evaluate choices
                val value = if (leftValue < rightValue) rightValue else leftValue
                //log("$it = $value")
                value
            }
        }
        val ans = table[Problem(items = items, lefts = emptySet(), rights = emptySet())]
        println(ans)
    }


    /*####################################################################################*/
    data class Infant(val pos: Int, val value: Long)

    fun readInt() = readLine()!!.toInt()
    fun readLongs() = readLine()!!.split(" ").map { it.toLong() }

    class LazyMap<K, V>(capacity: Int, private val init: LazyMap<K, V>.(K) -> V) : Map<K, V> {
        private val memo = HashMap<K, V>(capacity)
        override fun get(key: K): V {
            return memo[key] ?: init(key).memorizingAt(key)
        }


        private fun V.memorizingAt(key: K): V {
            memo[key] = this
            return this
        }

        override val entries: Set<Map.Entry<K, V>>
            get() = memo.entries
        override val keys: Set<K>
            get() = memo.keys
        override val size: Int
            get() = memo.size
        override val values: Collection<V>
            get() = memo.values

        override fun containsKey(key: K): Boolean = memo.containsKey(key)
        override fun containsValue(value: V): Boolean = memo.containsValue(value)
        override fun isEmpty(): Boolean = memo.isEmpty()
    }
}