Cod sursa(job #17695)

Utilizator raduzerRadu Zernoveanu raduzer Data 16 februarie 2007 18:16:38
Problema Lupul Urias si Rau Scor 0
Compilator fpc Status done
Runda Arhiva de probleme Marime 0.99 kb
var a,b:array[1..100000]of longint;
    n,x,l,s,i,j,max:longint;

procedure Sort(l, r: Integer);
var
  i, j, x, y, z: integer;
begin
  i := l; j := r; x := a[(l+r) DIV 2];
  repeat
    while a[i] < x do i := i + 1;
    while x < a[j] do j := j - 1;
    if i <= j then
    begin
      y := a[i]; a[i] := a[j]; a[j] := y;
      z := b[i]; b[i] := b[j]; b[j] := z;
      i := i + 1; j := j - 1;
    end;
  until i > j;
  if l < j then Sort(l, j);
  if i < r then Sort(i, r);
end;


begin
     assign(input,'lupu.in');
     reset(input);
     assign(output,'lupu.out');
     rewrite(output);
     readln(n,x,l);
     for i:=1 to n do readln(a[i],b[i]);
     sort(1,n);
     j:=n-l+1;
     i:=n+1;
     max:=0;
     while i>1 do
     begin
          i:=i-1;
          if a[i]<j then
          begin
               s:=s+max;
               max:=0;
               j:=j-l;
          end;
          if b[i]>max then max:=b[i];
     end;
     writeln(s);
close(output);
end.