Cod sursa(job #300914)

Utilizator cristinabCristina Brinza cristinab Data 7 aprilie 2009 19:38:26
Problema Energii Scor 0
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.09 kb
var sol:array[0..1000000] of longint;
    en,cost:array[1..5000] of integer;
    nrg,w:integer;
    rez:longint;

procedure citire;
var f:text;
    i:integer;
begin
assign(f,'energii.in'); reset(f);
readln(f,nrg);
readln(f,w);
for i:=1 to nrg do readln(f,en[i],cost[i]);
close(f);
end;

procedure rezolvare;
var i,j,max:longint;
begin

sol[0]:=0;
max:=0;

for i:=1 to nrg do
    for j:=max downto 0 do
        if sol[j+en[i]]<sol[j]+cost[i] then
           begin
           sol[j+en[i]]:=sol[j]+cost[i];
           if j+en[i]>max then max:=j+en[i];
           end;

rez:=maxlongint;
for i:=0 to max do
    if (sol[i]<rez) and (sol[i]>=w) then rez:=sol[i];
end;

procedure afisare;
var g:text;
begin
assign(g,'energii.out'); rewrite(g);
if rez<>maxlongint then writeln(g,rez)
                   else writeln(g,-1);
close(g);
end;

begin
citire;
rezolvare;
afisare;
end.