s[i]表示前i堆石头的价值总和,f[i,j]表示把第i堆到第j堆的石头合并成一堆的最有价值。
var
n,i,j,k:longint;
s:array[0..30]of longint;
f:array[0..100,0..100]of longint;
a:array[0..100]of longint;
function min(a,b:longint):longint;
begin
if a>b then exit(b) else exit(a);
end;
begin
readln(n);
for i:=1 to n do
read(a[i]);
for i:=1 to n do
s[i]:=s[i-1]+a[i];
fillchar(f,sizeof(f),$7f div 3);
for i:=1 to n do
f[i,i]:=0;
for i:=n downto 1 do
for j:=i+1 to n do
for k:=i to j-1 do
f[i,j]:=min(f[i,j],f[i,k]+f[k+1,j]+s[j]-s[i-1]);
write(f[1,n]);
end.