Cod sursa(job #609092)

Utilizator ctlin04UAIC.VlasCatalin ctlin04 Data 19 august 2011 16:03:49
Problema Problema rucsacului Scor 65
Compilator fpc Status done
Runda Arhiva educationala Marime 0.9 kb
Program probleama_rucsacului;
 var w,p:array [0..5000] of integer;
     sol: array [0..1,0..10000] of longint;
     i,j,n,g,k:integer;
     b1:array [1..1 shl 10] of char;
     fi,fo:text;
 function max(a,b:longint):longint;
 begin
  if a>b then max:=a
   else max:=b;
 end;
begin
 assign(fi,'rucsac.in');
  assign(fo,'rucsac.out');
 settextbuf(fi,b1);
 reset(fi);
 rewrite(fo);
 readln(fi,n,g);
 for i:=1 to n do readln(fi,w[i],p[i]);
 {______________________________________}
 i:=1;
 repeat
 inc(k);
 if k mod 2<>0 then begin
  for j:=0 to g do
   if w[k]<=j then
        sol[i,j]:=max(sol[i-1,j],sol[i-1,j-w[k]]+p[k])
        else sol[i,j]:=sol[i-1,j];
        end
  else for j:=0 to g do   begin
      if w[k]<=j then
       sol[i-1,j]:=max(sol[i,j],sol[i,j-w[k]]+p[k])
        else sol[i-1,j]:=sol[i,j];
        end;
  until k=n;
  write(fo,max(sol[0,g],sol[1,g]));
close(fo);
end.