0/1背包-背包问题程序代码(pascal)

var
        n,t,i,j:longint;
        f:array[0..1000,0..1000] of longint;
        w,v:array[0..1000] of longint;
function max(x,y:longint):longint;
begin
        if x>y then max:=x
                else max:=y;
end;
begin
        readln(t,n);
        for i:=1 to n do
        begin
                readln(w[i],v[i]); 
        end;
        for i:=1 to n do
                for j:=0 to t do
                begin
                        f[i,j]:=f[i-1,j];
                        if j>=w[i] then
                                f[i,j]:=max(f[i-1,j-w[i]]+v[i],f[i,j]);//状态转移方程
                end;
        writeln(f[n,t]);
end.

你可能感兴趣的:(0/1背包-背包问题程序代码(pascal))