Cod sursa(job #1538249)

Utilizator ili226Vlad Ilie ili226 Data 28 noiembrie 2015 18:26:16
Problema Problema rucsacului Scor 50
Compilator fpc Status done
Runda Arhiva educationala Marime 0.63 kb
var f:text;
    n,g,i,j:word;
    pmax,max:longint;
    w,p:array[1..5000]of word;
    best:array[0..1,0..10000]of longint;
begin
assign(f,'rucsac.in');
reset(f);
readln(f,n,g);
for i:=1 to n do
 readln(f,w[i],p[i]);
pmax:=-maxlongint;
close(f);
for j:=w[1] to g do
 best[0,j]:=p[1];
if w[1]<=g then pmax:=p[1];
for i:=2 to n do
 for j:=0 to g do
  begin
   max:=best[i mod 2,j];
   if w[i]<=j then
    if p[i]+best[i mod 2,j-w[i]]>max then
     max:=best[i mod 2,j-w[i]]+p[i];
   best[(i+1)mod 2,j]:=max;
   if max>pmax then pmax:=max;
  end;
assign(f,'rucsac.out');
rewrite(f);
writeln(f,pmax);
close(f);
end.