Cod sursa(job #1106657)

Utilizator TrifanTTrifan TrifanT Data 12 februarie 2014 23:24:44
Problema Problema rucsacului Scor 10
Compilator fpc Status done
Runda Arhiva educationala Marime 0.67 kb
var n,i , j, Max: integer;
    a: array[0..10000] of longint;
    c, g: array[1..10000] of integer;
    s: longint;

function Maxim(a, b: integer): integer;
begin
 if a>b then maxim:=a
        else maxim:=b;
end;


begin
 assign(input,'rucsac.in');
 reset(input);
 assign(output,'rucsac.out');
 rewrite(output);
 readln(n, max);

 for i:=1 to n do readln(g[i], c[i]);
 for i:=0 to max do a[i]:=0;

 s:=0;
 for i:=1 to n do
  for j:=(max-g[i]) downto 0 do
              Begin
               a[j+g[i]]:=Maxim(a[j+g[i]], a[j]+c[i]);
               if a[j+g[i]]>s then s:=a[j+g[i]];
              end;

  writeln(s);



close(input);
close(output);
end.