pasal标程

var
  i,j,n,v:longint;
  a:array[0..31]of longint;
  opt:array[0..31,0..20001]of longint;
function
  max(x,y:longint):longint;
  begin
    if x>y then exit(x)
           else exit(y);
  end;
begin
  readln(v);
  readln(n);
  for i:=1 to n do
    readln(a[i]);
  for j:=1 to v do
    if j>=a[1]
      then
        opt[1,j]:=a[1];
  for i:=2 to n do
    for j:=1 to v do
      if j>a[i]
        then
          opt[i,j]:=max(opt[i-1,j],opt[i-1,j-a[i]]+a[i])
        else
          opt[i,j]:=opt[i-1,j];
  writeln(v-opt[n,v]);
end.

2 条评论

  • @ 2016-11-13 15:35:54

    二维数组别闹!
    var
    w:array[1..30] of longint;
    f:array[0..20000] of longint;
    n, m, i, j:longint;
    begin
    readln(m);
    readln(n);
    for i:=1 to n do readln(w[i]);
    fillchar(f, sizeof(f), 0);
    for i:=1 to n do
    for j:=m downto w[i] do if f[j-w[i]]+w[i]>f[j] then f[j]:=f[j-w[i]]+w[i];
    write(m-f[m])
    end.

  • @ 2016-11-12 15:42:35

    标题打错了
    pascal

  • 1

信息

ID
1133
难度
4
分类
动态规划 | 背包 点击显示
标签
递交数
10795
已通过
4484
通过率
42%
被复制
27
上传者