#include <bits/stdc++.h>
using namespace std;
int n;
int a[1006];
int s[1007];
int f[1008][1009];
int main() {
freopen("stones.INP", "r", stdin);
freopen("stones.OUT", "w", stdout);
cin>>n;
for (int i=1;i<=n;i++)
{
cin>>a[i];
s[i]=s[i-1]+a[i];
}
for (int i=1;i<=n;i++) {
f[i][i]=0;
}
for (int len=2;len<=n;len++) {
for (int i=1;i+len-1<=n;i++) {
int j=i+len-1;
f[i][j] = INT_MAX;
for (int k=i;k<j;k++) {
f[i][j]=min(f[i][j],f[i][k]+f[k+1][j]+s[j]-s[i-1]);
}
}
}
cout<<f[1][n];
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CmludCBuOwppbnQgYVsxMDA2XTsKaW50IHNbMTAwN107CmludCBmWzEwMDhdWzEwMDldOwoKaW50IG1haW4oKSB7CiAgICBmcmVvcGVuKCJzdG9uZXMuSU5QIiwgInIiLCBzdGRpbik7CiAgICBmcmVvcGVuKCJzdG9uZXMuT1VUIiwgInciLCBzdGRvdXQpOwogICAgY2luPj5uOwogICAgZm9yIChpbnQgaT0xO2k8PW47aSsrKSAKIHsKICAgICAgICBjaW4+PmFbaV07CiAgICAgICAgc1tpXT1zW2ktMV0rYVtpXTsKICAgIH0KCiAgICBmb3IgKGludCBpPTE7aTw9bjtpKyspIHsKICAgICAgICBmW2ldW2ldPTA7CiAgICB9CiAgICBmb3IgKGludCBsZW49MjtsZW48PW47bGVuKyspIHsKICAgICAgICBmb3IgKGludCBpPTE7aStsZW4tMTw9bjtpKyspIHsKICAgICAgICAgICAgaW50IGo9aStsZW4tMTsKICAgICAgICAgICAgZltpXVtqXSA9IElOVF9NQVg7CiAgICAgICAgICAgIGZvciAoaW50IGs9aTtrPGo7aysrKSB7CiAgICAgICAgICAgICAgICBmW2ldW2pdPW1pbihmW2ldW2pdLGZbaV1ba10rZltrKzFdW2pdK3Nbal0tc1tpLTFdKTsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgIH0KICAgIGNvdXQ8PGZbMV1bbl07CiAgICByZXR1cm4gMDsKfQoK