Cod sursa(job #151564)

Utilizator dascalu2Dascalu Andi FLorin dascalu2 Data 8 martie 2008 13:27:51
Problema Orase Scor 100
Compilator fpc Status done
Runda Arhiva de probleme Marime 0.97 kb
type list=array[1..50000]of longint;
var m,n,i,max,d2max,ct:longint;
    p,l,dmax:list;

procedure QuickSort(var A,B: List; Lo, Hi: longint);

procedure Sort(l, r: longint);
var
  i, j, x, y: longint;
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;
      y := b[i]; b[i] := b[j]; b[j] := y;
      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
Sort(Lo,Hi);
end;
begin
assign(input,'orase.in');
reset(input);
read(m,n);
for i:=1 to n do
read(p[i],l[i]);
close(input);
quicksort(p,l,1,n);
max:=l[1]-p[1];
d2max:=0;
for i:=2 to n do
begin
     ct:=p[i]+l[i]+max;
     if(ct>d2max)then
     d2max:=ct;
     if(l[i]-p[i]>max)then
     max:=l[i]-p[i];
end;
assign(output,'orase.out');
rewrite(output);
write(d2max);
close(output);
end.