Cod sursa(job #609894)

Utilizator ctlin04UAIC.VlasCatalin ctlin04 Data 23 august 2011 18:26:39
Problema Problema rucsacului Scor 65
Compilator fpc Status done
Runda Arhiva educationala Marime 0.84 kb
Program probleama_rucsacului;
 var w,p:array [0..5001] of integer;
     sol:array [0..1,0..10001] of longint;
     i,j,n,g,k:integer;
     b1:array [1..1 shl 12] 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
 k:=k+1;
 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
      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];
  until k=n;
 write(fo,max(sol[0,g],sol[1,g]));
close(fo);
end.