Cod sursa(job #702104)

Utilizator andrei_toaderToader Andrei Sorin andrei_toader Data 1 martie 2012 19:34:52
Problema Problema rucsacului Scor 80
Compilator fpc Status done
Runda Arhiva educationala Marime 0.63 kb
program rucsac;
var f,g:text;
    gr,castig:array[1..5000] of integer;
    bufin:array[1..1 shl 17] of char;
    greutate,n,i,l,j:integer;
    c:array [0..1,0..10000] of longint;

begin
 assign (f,'rucsac.in'); reset (F);
 assign (G,'rucsac.out'); rewrite (G);
 settextbuf (f,bufin);
 readln (f,n,greutate);
 for i:=1 to n do
  readln (f,gr[i],castig[i]);
 l:=0;
 for i:=1 to n do
 begin
  for j:=1 to greutate do
  begin
  c[1-l,j]:=c[l,j];
   if gr[i]<=j then
    if castig[i]+c[l,j-gr[i]]>c[l,j] then
     c[1-l,j]:=castig[i]+c[l,j-gr[i]];
  end;
  l:=1-l;
 end;
 write (g,c[l,greutate]);
 close (f); close (g);
end.