Pagini recente » Monitorul de evaluare | Cod sursa (job #26344) | Cod sursa (job #2104969) | Monitorul de evaluare | Cod sursa (job #5983)
Cod sursa(job #5983)
program energii;
var a,b:array[1..5000]of longint;
g,w,i,j,x,y,min:longint;
fin,fout:text;
begin
assign(fin,'energii.in'); reset(fin);
assign(fout,'energii.out'); rewrite(fout);
readln(fin,g);
readln(fin,w);
for i:=1 to w+1 do begin
a[i]:=-1;
b[i]:=-1;
end;
for i:=1 to g do begin
readln(fin,x,y);
for j:=1 to x do
if (a[j]<y)and(a[j]<>-1) then b[j]:=a[j]
else b[j]:=y;
for j:=x+1 to w do
if (a[j-x]=-1)and(a[j]=-1) then b[j]:=-1
else begin
if a[j-x]+y < a[j] then begin
if a[j-x]<>-1 then b[j]:=a[j-x]+y
else b[j]:=-1
end else begin
if a[j]=-1 then b[j]:=a[j-x]+y
else b[j]:=a[j]
end;
end;
min:=maxlongint;
for j:=w-x+1 to w do
if (a[j]<min)and(a[j]<>-1) then min:=a[j];
if min=maxlongint then b[w+1]:=-1
else begin
if (min+y<=a[w+1]) then b[w+1]:=min+y
else
if a[w+1]=-1 then b[w+1]:=min+y
else b[w+1]:=a[w+1];
end;
a:=b;
{for j:=1 to w+1 do write(fout,a[j],' ');
writeln(fout);}
end;
if a[w]<a[w+1] then writeln(fout,a[w])
else writeln(fout,a[w+1]);
close(fin); close(fout);
end.