Pagini recente » Cod sursa (job #1314261) | Cod sursa (job #690008) | Cod sursa (job #2927502) | Cod sursa (job #1590667) | Cod sursa (job #6427)
Cod sursa(job #6427)
program energii;
type generator=record
e,c:longint;
end;
var f,g:text;
n,w:longint;
v:array[1..10000] of generator;
procedure iofile;
var i:longint;
begin
assign(f,'energii.in');
reset(f);
assign(g,'energii.out');
rewrite(g);
readln(f,n);
readln(f,w);
for i:=1 to n do
readln(f,v[i].e,v[i].c);
close(f);
end;
procedure pozitie(var m:longint;p,u:longint);
var i,j,di,dj,aux:longint;
aux1:generator;
begin
i:=p;
j:=u;
di:=0;
dj:=-1;
while i<j do
begin
if v[i].e/v[i].c<v[j].e/v[j].c then
begin
aux:=di;
di:=-dj;
dj:=-aux;
aux1:=v[i];
v[i]:=v[j];
v[j]:=aux1;
end;
i:=i+di;
j:=j+dj;
end;
m:=i;
end;
procedure quick(p,u:longint);
var m:longint;
begin
if p<u then
begin
pozitie(m,p,u);
quick(p,m-1);
quick(m+1,u);
end;
end;
procedure prel;
var i,poz,sum,sc:longint;
begin
quick(1,n);
poz:=1;
sum:=0;
sc:=0;
while (poz<=n)and(sum<w) do
begin
sum:=sum+v[poz].e;
sc:=sc+v[poz].c;
inc(poz);
end;
if sum<w then
begin
writeln(g,'-1');
close(g);
halt;
end;
poz:=1;
while sum-v[poz].e>=w do
begin
sum:=sum-v[poz].e;
sc:=sc-v[poz].c;
inc(poz);
end;
writeln(g,sc);
close(g);
end;
begin
iofile;
prel;
end.