Cod sursa(job #669011)

Utilizator andrei_toaderToader Andrei Sorin andrei_toader Data 25 ianuarie 2012 22:39:33
Problema Problema rucsacului Scor 0
Compilator fpc Status done
Runda Arhiva educationala Marime 0.96 kb
program rucsac;
var f,g:text;
    c,greutate:array [1..5000] of integer;
    gr,i,n,j,obiect,k:longint;
    alege,castig:array [1..2,0..100] of integer;
    bufin,bufout:array [1..65000] of byte;

begin
 assign (f,'rucsac.in'); reset (f);
 assign (g,'rucsac.out'); rewrite (G);
 settextbuf (f,bufin);
 settextbuf (g,bufout);
 readln (f,n,gr);
 for i:=1 to n do
  readln (f,greutate[i],c[i]);
 for i:=1 to n do
 begin
    for k:=1 to gr do
  begin
   castig[1,k]:=castig[2,k];
   alege[1,k]:=alege[2,k];
  end;
  for j:=1 to  gr do

   if greutate[i]<=j then
    if c[i]+castig[1,j-greutate[i]]>castig[1,j] then
    begin
     castig[2,j]:=c[i]+castig[1,j-greutate[i]];
     alege[2,j]:=i;
    end
   else
   begin
    castig[2,j]:=castig[1,j];
    alege[2,j]:=alege[1,j];
   end
  else
  begin
   castig[2,j]:=castig[1,j];
   alege[1,j]:=alege[1,j];
  end;
 end;
 i:=2; j:=gr;
 writeln (g,castig[i,j]);
 close (f);
 close (g);
end.