Cod sursa(job #1179715)

Utilizator Mihai_ChihaiMihai Chihai Mihai_Chihai Data 29 aprilie 2014 09:02:43
Problema Lupul Urias si Rau Scor 8
Compilator fpc Status done
Runda Arhiva de probleme Marime 0.81 kb
 var a,b:array[0..100000] of longint;
     n,i,j,x,l,max,sum,k:longint;

 procedure sort(l,r:longint);
  var aux,m,i,j:longint;
 begin
 i:=l;
 j:=r;
 m:=b[(i+j) div 2];
 while i<=j do begin
  while b[i]<m do inc(i);
  while b[j]>m do dec(j);
  if i<=j then begin
      aux:=a[i];
      a[i]:=a[j];
      a[j]:=aux;
      aux:=b[i];
      b[i]:=b[j];
      b[j]:=aux;
      inc(I);
      dec(j);
      end;
  end;
  if i<r then sort(i,r);
  if l<j then sort(l,j);
 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);
 sum:=0;
 i:=n+1;
 repeat
 dec(i);
 while (a[i]>x)and(i>0) do dec(i);

 if a[i]<=x then sum:=sum+b[i];
 x:=x-l;
 until i=0;

 write(sum);
 close(output);
 end.