Cod sursa(job #609862)

Utilizator ctlin04UAIC.VlasCatalin ctlin04 Data 23 august 2011 17:43:52
Problema Problema rucsacului Scor 0
Compilator fpc Status done
Runda Arhiva educationala Marime 0.95 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;
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 shr 1<>0 then begin
  for j:=0 to g do
   if w[k]<=j then begin
      if sol[i-1,j]>sol[i-1,j-w[k]]+p[k] then sol[i,j]:=sol[i-1,j]
      else sol[i,j]:=sol[i-1,j-w[k]]+p[k];
        end
        else sol[i,j]:=sol[i-1,j];
   if k=n then write(fo,sol[i,g]);
        end
  else for j:=0 to g do begin
      if w[k]<=j then begin
      if sol[i,j]>sol[i,j-w[k]]+p[k] then sol[i-1,j]:=sol[i,j]
      else sol[i-1,j]:=sol[i,j-w[k]]+p[k];
       end
        else sol[i-1,j]:=sol[i,j];
       if k=n then write(fo,sol[i-1,g]);
        end;
  until k=n;
close(fo);
end.