Cod sursa(job #669100)

Utilizator andrei_toaderToader Andrei Sorin andrei_toader Data 26 ianuarie 2012 09:32:21
Problema Problema rucsacului Scor 65
Compilator fpc Status done
Runda Arhiva educationala Marime 0.82 kb
program rucsac;
var f,g:text;
c,greutate:array [1..5000] of int64;
gr,i,n,j,obiect,k:longint;
alege,castig:array [1..2,0..5000] of longint;
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 j:=1 to  gr do
begin
alege[1,j]:=alege[2,j]; castig[1,j]:=castig[2,j] ;
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[2,j]:=alege[1,j];
end;
end;
end;
writeln (g,castig[2,gr]);
close (f);
close (g);
end.