n = gets.to_i
a = gets.split.map(&:to_i)

INF = 1 << 40

@b = a.clone
(n-1).times {|i| @b[i+1] += @b[i]}
@b << 0

@memo = Array.new(n) {Array.new(n)}
n.times {|i| @memo[i][i] = 0}

def calc(l, r)
    return @memo[l][r] if @memo[l][r]

    t = INF
    l.upto(r-1) do |i|
        u = calc(l, i) + calc(i+1, r)
        t = u if t > u
    end

    @memo[l][r] = @b[r] - @b[l-1] + t
end

calc(0, n-1)
p @memo[0][n-1]