问题何在

program tower;
var
f:array[0..101,0..2001] of longint;
h:array[0..100] of longint;
i,j,n,s:longint;
function max(a,b:longint):longint;
begin
if a>b then exit(a)
else exit(b);
end;
begin
readln(n);
for i:=1 to n do
begin
read(h[i]);
s:=s+h[i];
end;
for i:=0 to n do
begin
for j:=0 to s do
f[i,j]:=-maxlongint;
end;
f[0,0]:=0;
for i:=1 to n do
begin
for j:=s downto 0 do
begin
if j<=h[i] then
f[i,j]:=max(f[i-1,j],max(f[i-1,h[i]-j]+j,f[i-1,j+h[i]]));
if j>h[i] then
f[i,j]:=max(f[i-1,j],max(f[i-1,j+h[i]],f[i-1,j-h[i]]+h[i]))
end;
end;
if f[n,0]>0 then write(f[n,0])
else write('Impossbile');
end.

2 条评论

  • 1

信息

ID
1037
难度
6
分类
动态规划 | 背包 点击显示
标签
(无)
递交数
10570
已通过
2750
通过率
26%
被复制
16
上传者