'use strict'

function sumCalc(f, t, A) {
  var ans = 0
  for (var i = f; i <= t; i++) {
    ans += A[i]
  }
  return ans
}

function solve(N, A) {
  var dp = new Array(N).fill(0).map(e => new Array(N).fill(0))
  var col = 1
  while (col < N) {
    var c = col
    for (var r = 0; r < N; r++) {
      if (c === N) {
        break
      }
      dp[r][c] = Number.MAX_SAFE_INTEGER
      for (var k = r; k < c; k++) {
        dp[r][c] = Math.min(dp[r][c], dp[r][k] + dp[k+1][c] + sumCalc(r, k, A) + sumCalc(k+1, c, A))
      }
      c++
    }
    col++
  }
  return dp[0][N-1]
}

function main(input) {
  var lines = input.split("\n")
  var N = parseInt(lines[0])
  var A = lines[1].split(' ').map(l => parseInt(l))
  var ans = solve(N, A)
  console.log(ans)
}

main(require('fs').readFileSync('/dev/stdin', 'utf8'));