| | | | 背景 Background
| | | 太原成成中学第2次模拟赛 第四道
| | | |
| | | | 描述 Description
| | | 乘法游戏是在一行牌上进行的。每一张牌包括了一个正整数。在每一个移动中,玩家拿出一张牌,得分是用它的数字乘以它左边和右边的数,所以不允许拿第1张和最后1张牌。最后一次移动后,这里只剩下两张牌。
你的目标是使得分的和最小。
例如,如果数是10 1 50 20 5,依次拿1、20、50,总分是 10*1*50+50*20*5+10*50*5=8000
而拿50、20、1,总分是1*50*20+1*20*5+10*1*5=1150。
| | | |
| | | | 输入格式 Input Format
| | | 输入文件的第一行包括牌数(3<=n<=100),第二行包括N个1-100的整数,用空格分开。
| | | |
| | | | 输出格式 Output Format
| | | 输出文件只有一个数字:最小得分
| | | |
| | | | 样例输入 Sample Input [复制数据]
| | | 6 10 1 50 50 20 5
| | | |
| | | | 样例输出 Sample Output [复制数据]
| | | 3650
| | | |
| | | | 时间限制 Time Limitation
| | | 各个测试点1s
|
|
================================
======================
var
n:longint;
a:array[1..100]of longint;
f:array[1..100,1..100]of longint;
procedure init;
begin
assign(input,'ty1014.in');
assign(output,'ty1014.out');
reset(input); rewrite(output);
end;
procedure terminate;
begin
close(input); close(output);
halt;
end;
function dp(s,t:longint):longint;
var
i:longint;
now:longint;
begin
if s+1=t then exit(0);
//if s>t then exit(0);
if f[s,t]<>maxlongint then exit(f[s,t]);
dp:=10000000;
for i:=s+1 to t-1 do
begin
if dp>dp(s,i)+dp(i,t)+a[i]*a[s]*a[t] then
dp:=dp(s,i)+dp(i,t)+a[i]*a[s]*a[t];
end;
f[s,t]:=dp;
end;
procedure main;
var
i,j,k:longint;
begin
readln(n);
for i:=1 to n do read(a[i]);
//fillchar(f,sizeof(f),0);
for i:=1 to n do
for j:=1 to n do
f[i,j]:=maxlongint;
writeln(dp(1,n));
end;
begin
init;
main;
terminate;
end.