Cod sursa(job #614349)

Utilizator free_arenafree arena free_arena Data 6 octombrie 2011 10:18:14
Problema Energii Scor 5
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.04 kb
Program energii;
var f1,f2 : text;
    n:0..1002;
    w:0..5002;
    g,cost:array[0..1002] of 0..10001;
    cistig:array[0..1002,0..5002] of longint;
    i,j:0..10001;
    k:longint;
function max(a,b:longint):longint;
begin
      if a>b then max:=a
             else max:=b;

end;

begin
     assign(f1,'energii.in'); reset(f1); readln(f1,n,w);  k:=0; assign(f2,'energii.out'); rewrite(f2);
     for i:=1 to n do begin read(f1,g[i],cost[i]); k:=k+g[i]; end;
     if k<w then writeln(f2,'-1') else begin
                              for i:=0 to n do for j:=0 to w do cistig[i,j]:=0;
                              for i:=1 to n do
                                        for j:=1 to w do
                                                if g[i]<=j then cistig[i,j]:=max(cistig[i-1,j],cistig[i-1,j-g[i]]+cost[i])
                                                           else cistig[i,j]:=cistig[i-1,j];
                                        writeln(f2,cistig[n,w]);
                                        end;
     close(f1); close(f2);
end.