Pagini recente » Cod sursa (job #530530) | Cod sursa (job #75097) | Cod sursa (job #1557045) | Cod sursa (job #1157549) | Cod sursa (job #163550)
Cod sursa(job #163550)
program peste;
var f,g:text;
n,k,ttotal,dim_heap:longint;
p:array[1..50001] of longint;
t:array[1..50001] of longint;
procedure iofile;
var i:longint;
begin
assign(f,'peste.in');reset(f);
assign(g,'peste.out');rewrite(g);
readln(f,n,k,ttotal);
for i:=1 to n do
read(f,p[i],t[i]);
close(f);
end;
procedure heap_dw(i,time:longint);
var l,r,max,aux:longint;
begin
l:=i*2;
r:=l+1;
max:=i;
if (l<=dim_heap)and((time div t[l])*p[l]<(time div t[max])*p[max]) then
max:=l;
if (r<=dim_heap)and((time div t[r])*p[r]<(time div t[max])*p[max]) then
max:=r;
if max<>i then
begin
aux:=t[i];
t[i]:=t[max];
t[max]:=aux;
aux:=p[i];
p[i]:=p[max];
p[max]:=aux;
heap_dw(max,time);
end;
end;
procedure build_heap(time:longint);
var i:longint;
begin
for i:=n div 2 downto 1 do
heap_dw(i,time);
end;
procedure heapsort(time:longint);
var i,aux:longint;
begin
build_heap(time);
for i:=n downto 2 do
begin
aux:=p[i];
p[i]:=p[1];
p[1]:=aux;
aux:=t[i];
t[i]:=t[1];
t[1]:=aux;
dec(dim_heap);
heap_dw(1,time);
end;
end;
procedure solve;
var tcurrent,maxt,i,nrp,tex:longint;
begin
tcurrent:=0;
nrp:=0;
if k>n then k:=n;
while (tcurrent<ttotal) do
begin
dim_heap:=n;
heapsort(ttotal-tcurrent);
maxt:=0;
for i:=1 to k do
begin
tex:=(ttotal-tcurrent) div t[i];
nrp:=nrp+tex*p[i];
if (tex*t[i]>maxt) then
maxt:=tex*t[i];
end;
if maxt=0 then maxt:=ttotal-tcurrent;
tcurrent:=tcurrent+maxt;
end;
writeln(g,nrp);
close(g);
end;
begin
iofile;
solve;
end.