Cod sursa(job #702095)

Utilizator andrei_toaderToader Andrei Sorin andrei_toader Data 1 martie 2012 19:30:48
Problema Problema rucsacului Scor 80
Compilator fpc Status done
Runda Arhiva educationala Marime 0.7 kb
program rucsac;
type natural=record
 gr,c:integer;
end;
var f,g:text;
    v:array[1..5000] of natural;
    bufin,bufout:array[1..65000] of byte;
    greutate,n,i,l,j:integer;
    c:array [0..2,0..10000] of longint;

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