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)
  }
}