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

enum inf = 10L^^18;
enum mod = 10L ^^ 9 + 7;

void main() {
    int n;
    scan(n);

    auto a = readln.split.to!(long[]);
    auto as = new long[](n + 1);
    foreach (i ; 1 .. n + 1) {
        as[i] = as[i-1] + a[i-1];
    }

    long ssum(int l, int r) {
        return as[r] - as[l];
    }

    auto dp = new long[][](n + 1, n + 1);
    fillAll(dp, -1);

    long dfs(int l, int r) {
        if (dp[l][r] != -1) {
            return dp[l][r];
        }
        if (r - l == 1) {
            return 0;
        }

        long ans = inf;

        foreach (i ; l+1 .. r) {
            ans = min(ans, ssum(l, r) + dfs(l, i) + dfs(i, r));
        }

        return dp[l][r] = ans;
    }

    long ans = dfs(0, n);

    debug {
        writefln("%(%s\n%)", dp);
    }
    

    writeln(ans);
}







void scan(T...)(ref T args) {
    import std.stdio : readln;
    import std.algorithm : splitter;
    import std.conv : to;
    import std.range.primitives;

    auto line = readln().splitter();
    foreach (ref arg; args) {
        arg = line.front.to!(typeof(arg));
        line.popFront();
    }
    assert(line.empty);
}


void fillAll(R, T)(ref R arr, T value) {
    static if (is(typeof(arr[] = value))) {
        arr[] = value;
    }
    else {
        foreach (ref e; arr) {
            fillAll(e, value);
        }
    }
}