Cod sursa(job #1174091)

Utilizator azkabancont-vechi azkaban Data 21 aprilie 2014 23:12:45
Problema Lupul Urias si Rau Scor 8
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.79 kb
Program hep_lup;
var n,k,i,a,b,j,m,max,suma,pivot : longint;
    L,Viz,H : array [0..300000] of longint;
    b1,b2 : array [0..1 shl 17] of char;

procedure swap(var x,y : longint);
   var aux : longint;
     begin
        aux:=x;
        x:=y;
        y:=aux;
      end;

procedure qsort ( left,right : longint);
  var i,j,pivot : longint;
    begin
       i:=left; j:=right; pivot:=L[(left+right) div 2];
       repeat
          while L[i]>pivot do i:=i+1;
          while L[j]<pivot do j:=j-1;
          if i<=j then begin
                            swap(L[i],L[j]);
                            swap(H[i],H[j]);
                            i:=i+1;
                            j:=j-1;
                      end;
          until i>j;
          if i<right then qsort(i,right);
          if j>left then qsort(left,j);
       end;

begin
    assign(input,'lupu.in'); settextbuf(input,b1); reset(input);
    assign(output,'lupu.out'); settextbuf(output,b2); rewrite(output);
    readln(m,n,k);
    for i:=1 to m do begin
                          readln(a,b);
                          L[i]:=b;
                          H[i]:=a;
                          suma:=suma+L[i];
                      end;
if k=0 then writeln(suma)
         else begin
suma:=0;
    qsort(1,m);
    for i:=1 to m-1 do
        for j:=i+1 to m do
           if (L[i]=L[j]) and (H[i]<H[j]) then swap(H[i],H[j]);
    pivot:=n; i:=1;

    while pivot>=0 do begin
                    if H[i]<=pivot then begin
                                             suma:=suma+L[i];
                                             pivot:=pivot-k;
                                        end;
                    i:=i+1;
                    end;
    writeln(suma);   end;
    close(input);
    close(output);
end.