import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, std.bitmanip;

immutable long MOD = 10^^9 + 7;

void main() {
    auto N = readln.chomp.to!int;
    auto A = readln.split.map!(to!long).array;

    auto B = new long[](N+1);
    foreach (i; 0..N) B[i+1] = B[i] + A[i];

    auto dp = new long[][](N, N);
    foreach (i; 0..N) foreach (j; 0..N) if (i != j) dp[i][j] = 1L << 59;

    foreach (len; 2..N+1) {
        foreach (i; 0..N-len+1) {
            int j = i + len - 1;
            foreach (k; i..j) {
                dp[i][j] = min(dp[i][j], dp[i][k] + dp[k+1][j] + B[j+1] - B[i]);
            }
        }
    }

    dp[0][N-1].writeln;
}
