module mod_segment_tree
  implicit none
  type t_segment_tree
    private
    integer :: deflt = 0
    integer :: n, p
    integer, pointer :: arr(:) => null()
  contains
    procedure :: init => init_segtree
    procedure :: release => release_segtree
    procedure :: update => update
    procedure :: query => query
    procedure :: default => set_default
    procedure :: change => change_default
  end type
  interface segment_tree
    module procedure :: newst
  end interface segment_tree
contains
  integer function op(x,y)
    integer, intent(in) :: x, y
    op = max(x,y)
  end
  function newst(n) result(ret)
    integer, intent(in) :: n
    type(t_segment_tree) :: ret
    integer :: p
    p = 1
    do while (p < n)
      p = 2*p
    end do
    ret%n = n
    ret%p = p
    allocate(ret%arr(2*p-1))
    ret%arr = ret%deflt
  end
  subroutine init_segtree(this,n)
    class(t_segment_tree), intent(inout) :: this
    integer, intent(in) :: n
    integer :: p
    p = 1
    do while (p < n)
      p = 2*p
    end do
    this%n = n
    this%p = p
    allocate(this%arr(2*p-1))
    this%arr = this%deflt
  end
  subroutine release_segtree(this)
    class(t_segment_tree), intent(inout) :: this
    if (associated(this%arr)) deallocate(this%arr)
  end
  subroutine set_default(this,i)
    class(t_segment_tree), intent(inout) :: this
    integer, intent(in) :: i
    integer :: x
    x = i+this%p-1
    this%arr(x) = this%deflt
    do while (x > 1)
      x = x/2
      this%arr(x) = op(this%arr(2*x),this%arr(2*x+1))
    end do
  end
  subroutine change_default(this,deflt)
    class(t_segment_tree), intent(inout) :: this
    integer, intent(in) :: deflt
    this%deflt = deflt
  end
  subroutine update(this,i,v)
    class(t_segment_tree), intent(inout) :: this
    integer, intent(in) :: i
    integer, intent(in) :: v
    integer :: x
    x = i+this%p-1
    this%arr(x) = v
    do while (x > 1)
      x = x/2
      this%arr(x) = op(this%arr(2*x),this%arr(2*x+1))
    end do
  end
  integer function query(this,a,b)
    class(t_segment_tree), intent(inout) :: this
    integer, intent(in) :: a, b
    integer :: l, r
    query = this%deflt
    l = a+this%p-1
    r = b+this%p-1
    do while (l <= r)
      if (mod(l,2) == 1) then
        query = op(query,this%arr(l))
        l = l+1
      end if
      if (mod(r,2) == 0) then
        query = op(query,this%arr(r))
        r = r-1
      end if
      l = l/2
      r = r/2
    end do
  end
end module mod_segment_tree
program robot_arms
  use mod_segment_tree
  implicit none
  integer :: n, i
  integer, dimension(100000,4) :: r = 0
  type(t_segment_tree) :: st
  read(*,*) n
  st = segment_tree(n)
  do i = 1, n
    read(*,*) r(i,1:2)
    r(i,1) = r(i,1)-r(i,2)
    r(i,3) = i
  end do
  call merge_sort(r(1:n,1:3),1)
  do i = n, 1, -1
    call st%update(i,dfs(i))
  end do
  write(*,'(i0)') maxval(r(1:n,4))
contains
  recursive function dfs(i) result(ret)
    integer, intent(in) :: i
    integer :: ret, x, a, b, c
    if (r(i,4) /= 0) then
      ret = r(i,4)
      return
    end if
    x = r(i,1)+2*r(i,2)
    a = i
    b = n+1
    do while (b-a > 1)
      c = (a+b)/2
      if (r(c,1) >= x) then
        b = c
      else
        a = c
      end if
    end do
    ret = st%query(b,n)+1
    r(i,4) = ret
  end
  subroutine merge_sort(a,k)
    integer, intent(inout) :: a(:,:)
    integer, intent(in) :: k
    integer :: n, m, l, i, u
    n = size(a,1)
    m = n
    l = 1
    do while (m > 1)
      do i = 1, m/2
        u = min(2*i*l,n)
        call merger(a(2*(i-1)*l+1:(2*i-1)*l,:),a((2*i-1)*l+1:u,:),k)
      end do
      l = 2*l
      m = (m+1)/2
    end do
  end
  subroutine merger(a1,a2,k)
    integer, intent(inout) :: a1(:,:), a2(:,:)
    integer, intent(in) :: k
    integer :: a(size(a1,1)+size(a2,1),size(a1,2))
    integer :: i1, i2, n1, n2
    i1 = 1
    i2 = 1
    n1 = size(a1,1)
    n2 = size(a2,1)
    do while (i1 <= n1 .and. i2 <= n2)
      if (a1(i1,k) <= a2(i2,k)) then
        a(i1+i2-1,:) = a1(i1,:)
        i1 = i1+1
      else
        a(i1+i2-1,:) = a2(i2,:)
        i2 = i2+1
       end if
    end do
    if (i1 <= n1) then
      a(i1+i2-1:n1+n2,:) = a1(i1:n1,:)
    else
      a(i1+i2-1:n1+n2,:) = a2(i2:n2,:)
    end if
    a1 = a(1:n1,:)
    a2 = a(n1+1:n1+n2,:)
  end
end program robot_arms

Measured memory_bytes: 4020