1 #include<iostream>
2 #include<cstdio>
3 #include<cstring>
4 #include<cmath>
5 #include<algorithm>
6 #define lli long long int
7 using namespace std;
8 const int MAXN=5001;
9 const int maxn=0x7fffffff;
10 void read(int &n)
11 {
12 char c='+';int x=0;bool flag=0;
13 while(c<'0'||c>'9'){c=getchar();if(c=='-')flag=1;}
14 while(c>='0'&&c<='9')
15 x=(x<<1)+(x<<3)+c-48,c=getchar();
16 flag==1?n=-x:n=x;
17 }
18 int n;
19 int a[MAXN];
20 int sum[MAXN];
21 int dp[MAXN][MAXN];
22 int mid[MAXN][MAXN];
23 int main()
24 {
25 read(n);
26 for(int i=1;i<=n;i++)
27 read(a[i]);
28 for(int i=1;i<=n;i++)
29 sum[i]=a[i]+sum[i-1];
30 for(int i=1;i<=n;i++)
31 dp[i][i]=0,mid[i][i]=i;
32 for(int len=1;len<=n-1;len++)
33 {
34 for(int i=1;i<=n-len;i++)
35 {
36 int j=len+i;
37 dp[i][j]=maxn;
38 for(int k=mid[i][j-1];k<=mid[i+1][j];k++)
39 {
40 if(dp[i][j]>dp[i][k]+dp[k+1][j]+sum[j]-sum[i-1])
41 {
42 dp[i][j]=dp[i][k]+dp[k+1][j]+sum[j]-sum[i-1];
43 mid[i][j]=k;
44 }
45 }
46 }
47 }
48
49 printf("%d",dp[1][n]);
50 return 0;
51 }