【區間DP】乘法遊戲
阿新 • • 發佈:2018-12-23
☆乘法遊戲 |
|
================================
======================
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.