Pagini recente » Cod sursa (job #1765379) | Cod sursa (job #3255850) | Cod sursa (job #726753) | Cod sursa (job #1057628) | Cod sursa (job #60288)
Cod sursa(job #60288)
program energii;
type vec= array[1..1001] of word;
var en,c:vec;
s:array[0..10001] of boolean;
c1:array[0..10001] of word;
g,i,j,w,vmax,cost,k:word;
f:text;
begin
assign(f,'energii.in'); reset(f);
readln(f,g); readln(f,w);
for i:= 1 to g do
readln(f,en[i],c[i]);
close(f);
s[0]:=true; cost:=65000; vmax:=0; c1[0]:=0;
for j:= 1 to en[1] do begin s[j]:=false; c1[j]:=0; end;
for i:= 1 to g do
begin
for j:=vmax downto 0 do
if (s[j]) then
begin
if not(s[j+en[i]])then
begin
s[j+en[i]]:=true;
c1[j+en[i]]:=c1[j]+c[i];
if j+en[i]>=w then
if cost>c1[j]+c[i] then cost:=c1[j]+c[i];
if j+en[i]>vmax then vmax:=j+en[i];
end
else if c1[j+en[i]]>c1[j]+c[i] then
begin
c1[j+en[i]]:=c1[j]+c[i];
if j+en[i]>=w then
if cost>c1[j]+c[i] then cost:=c1[j]+c[i];
end;
end;
for k:= vmax+1 to vmax+en[i+1] do begin s[k]:=false; c1[k]:=0; end;
end;
assign(f,'energii.out'); rewrite(f);
if cost=65000 then writeln(f,'-1')
else writeln(f,cost);
close(f);
end.