code:
var n,m:longword;
f:array[0..500, 0..500] of longword;
w,v:array[0..500] of longword;
i,j:longword;
begin
read(n,m);
for i:=1 to n do
read(w[i], v[i]);
fillchar(f[0], sizeof(f[0]),0);
for i:=1 to n do
for j:=0 to m do
begin
f[i,j]:=f[i-1,j];
if (j>=w[i]) and (f[i,j]<f[i-1, j-w[i]] + v[i]) then
f[i,j]:=f[i-1, j-w[i]] + v[i];
end;
writeln(f[n,m]);
readln
end.