Pagini recente » Cod sursa (job #610063) | Cod sursa (job #1664052) | Cod sursa (job #2593692) | Cod sursa (job #748930) | Cod sursa (job #930032)
Cod sursa(job #930032)
type formatie=record
a,b:longint;
end;
var f,g:text;v:array[1..100001]of formatie;v1:array[0..100001]of longint;
n,i,j,max,x,y,u,m:longint;
procedure sort(st,dr:longint);
var i,j,x:longint;aux:formatie;
begin
i:=st;j:=dr;x:=v[(st+dr)div 2].b;
repeat
while v[i].b<x do inc(i);
while v[j].b>x do dec(j);
if i<=j then begin
aux:=v[i];v[i]:=v[j];v[j]:=aux;
inc(i);
dec(j);
end;
until i>j;
if st<j then sort(st,j);
if dr>i then sort(i,dr);
end;
begin
assign(f,'heavymetal.in');reset(f);assign(g,'heavymetal.out');rewrite(g);
read(f,n);
for i:=1 to n do read(f,v[i].a,v[i].b);
sort(1,n);
v1[1]:=v[1].b-v[1].a;
for i:=2 to n do begin
max:=0;
x:=0;
y:=i-1;
u:=1;
repeat
m := (y-u)div 2 + u;
if v[i].a< v[m].b then
begin
y := m-1;
end
else
begin
u := m +1;
end;
until u > y;
{while j>x do begin
if (v[j].b<=v[i].a)and(v1[j]>max)then begin max:=v1[j];x:=v[j].a;end;
dec(j);
end;
if v1[i-1]>max+v[i].b-v[i].a then v1[i]:=v1[i-1]
else v1[i]:=max+v[i].b-v[i].a;}
if v1[i-1]>v1[y]+v[i].b-v[i].a then v1[i]:=v1[i-1]
else v1[i]:=v1[y]+v[i].b-v[i].a;
end;
write(g,v1[n]);
close(f);close(g);
end.