Problem:
Score : 100 points

Problem Statement

There are N slimes lining up in a row.
Initially, the i-th slime from the left has a size of a_i.

Taro is trying to combine all the slimes into a larger slime.
He will perform the following operation repeatedly until there is only one slime:

Choose two adjacent slimes, and combine them into a new slime. The new slime has a size of x + y, where x and y are the sizes of the slimes before combining them. Here, a cost of x + y is incurred. The positional relationship of the slimes does not change while combining slimes.

Find the minimum possible total cost incurred.

Constraints

All values in input are integers.

2 \leq N \leq 400

1 \leq a_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 \ldots a_N

Output

Print the minimum possible total cost incurred.

Sample Input 1

4
10 20 30 40

Sample Output 1

190

Taro should do as follows (slimes being combined are shown in bold):

(10, 20, 30, 40) → (30, 30, 40)

(30, 30, 40) → (60, 40)

(60, 40) → (100)

Sample Input 2

5
10 10 10 10 10

Sample Output 2

120

Taro should do, for example, as follows:

(10, 10, 10, 10, 10) → (20, 10, 10, 10)

(20, 10, 10, 10) → (20, 20, 10)

(20, 20, 10) → (20, 30)

(20, 30) → (50)

Sample Input 3

3
1000000000 1000000000 1000000000

Sample Output 3

5000000000

The answer may not fit into a 32-bit integer type.

Sample Input 4

6
7 6 8 6 1 1

Sample Output 4

68

Taro should do, for example, as follows:

(7, 6, 8, 6, 1, 1) → (7, 6, 8, 6, 2)

(7, 6, 8, 6, 2) → (7, 6, 8, 8)

(7, 6, 8, 8) → (13, 8, 8)

(13, 8, 8) → (13, 16)

(13, 16) → (29)

Solution in Scala (2.11.7) (judge verdict: Accepted)

object Main extends App {

  import scala.io.StdIn._

  val n = readLine.trim.toInt
  val slimes = readLine.trim.split(' ').map(_.toInt)
  println(minMergeCost(slimes))
  def minMergeCost(slimes: Array[Int]): Long = {
    val memo = Array.tabulate(slimes.length){l ⇒ Array.tabulate(slimes.length - l){_ ⇒ -1L}}
    val sum = slimes.map{_.toLong}.also{arr ⇒
      for (i ← 1 until arr.length){
        arr(i) += arr(i - 1)
      }
    }
    def sumSize(left: Int, right: Int): Long = {
      if (left == 0) sum(right)
      else sum(right) - sum(left - 1)
    }
    def sub(left: Int, right: Int): Long = {
      if (left == right) 0L
      else if (memo(left)(right - left) >= 0) memo(left)(right - left)
      else {
        (left until right).map{split ⇒ sub(left, split) + sub(split + 1, right) + sumSize(left, split) + sumSize(split + 1, right)}.min.also{min ⇒ memo(left)(right - left) = min}
      }
    }
    sub(0, slimes.length - 1)
  }
  implicit class Extend[T](value: T){
    def also(func: T ⇒ Unit): T = {func(value); value}
    def let[A](func: T ⇒ A): A = func(value)
  }
}