Pagini recente » Cod sursa (job #1148109) | Cod sursa (job #2228308) | Cod sursa (job #23551) | Cod sursa (job #814803) | Cod sursa (job #670450)
Cod sursa(job #670450)
Program rucsac_dinamic;
type tudor=array[0..10000] of longint;
var fi,fo: text;
cistig:array[0..2] of tudor;
g,cost,d:array[0..10000] of longint;
n,i,j,w: longint;
Function max(a,b:longint):longint;
begin
if a>b then max:=a
else max:=b;
end;
begin
assign(fi,'rucsac.in'); reset(fi); readln(fi,n,w);
assign(fo,'rucsac.out'); rewrite(fo);
for i:=1 to n do readln(fi,g[i],cost[i]);
for i:=1 to n-1 do
begin
for j:=1 to w do if g[i]<=j then cistig[2,j]:=max(cistig[1,j],cistig[1,j-g[i]]+cost[i])
else cistig[2,j]:=cistig[1,j];
d:=cistig[2];
cistig[1]:=d;
end;
i:=n;
for j:=1 to w do if g[i]<=j then cistig[2,j]:=max(cistig[1,j],cistig[1,j-g[i]]+cost[i])
else cistig[2,j]:=cistig[1,j];
write(fo,cistig[2,w]);
close(fi); close(fo);
end.