Cod sursa(job #903025)

Utilizator andrei_toaderToader Andrei Sorin andrei_toader Data 1 martie 2013 18:07:59
Problema Problema rucsacului Scor 80
Compilator fpc Status done
Runda Arhiva educationala Marime 0.69 kb
program rucsca;
type hacking=record
 gr,cost:integer;
end;
var f,g:text;
    n,i,j,greutate,l:longint;
    a:array[0..1,0..10000] of longint;
    v:array[1..5000] of hacking;
    bufin:array[1..65000] of byte;

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
 begin
  readln (f,v[i].gr,v[i].cost);
 end;
 l:=0;
 for i:=1 to n do
 begin
  for j:=1 to greutate do
  begin
   a[1-l,j]:=a[l,j];
   if v[i].gr<=j then
    if a[l,j-v[i].gr]+v[i].cost>a[1-l,j] then
     a[1-l,j]:=a[l,j-v[i].gr]+v[i].cost;
  end;
  l:=1-l;
 end;
 write (g,a[l,greutate]);
 close (F); close (G);
end.