Cod sursa(job #163700)

Utilizator vanila_CPPIonescu Victor Cristian vanila_CPP Data 22 martie 2008 14:59:22
Problema Peste Scor 0
Compilator fpc Status done
Runda preONI 2008, Runda Finala, Clasa a 10-a Marime 2.57 kb
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.