program zabuton
  implicit none
  integer(8), parameter :: infty = 1000000000000000000_8
  integer :: n, i, j
  integer(8) :: h(5000), p(5000), dp(0:5000,0:5000)
  h = 0_8
  p = 0_8
  dp = infty
  dp(0,0) = 0_8
  read(*,*) n
  do i = 1, n
    read(*,*) h(i), p(i)
  end do
  call merge_sort(h(1:n),p(1:n))
  do i = 1, n
    do j = n, 1, -1
      dp(i,j) = dp(i-1,j)
      if (dp(i-1,j-1).le.h(i)) dp(i,j) = min(dp(i,j),dp(i-1,j-1)+p(i))
    end do
  end do
  do i = n, 1, -1
    if (dp(n,i).ne.infty) then
      write(*,'(i0)') i
      stop
    end if
  end do
  stop
contains
  subroutine merge_sort(x,y)
    implicit none
    integer(8), intent(inout) :: x(:), y(:)
    integer :: n, m, l, i, u
    n = size(x)
    m = n
    l = 1
    do while (m.gt.1)
      do i = 1, m/2
        u = min(2*i*l,n)
        call merger(x(2*(i-1)*l+1:(2*i-1)*l),x((2*i-1)*l+1:u), &
        &           y(2*(i-1)*l+1:(2*i-1)*l),y((2*i-1)*l+1:u))
      end do
      l = 2*l
      m = (m+1)/2
    end do
    return
  end subroutine merge_sort
  subroutine merger(x1,x2,y1,y2)
    implicit none
    integer(8), intent(inout) :: x1(:), x2(:), y1(:), y2(:)
    integer(8) :: x(size(x1)+size(x2)), y(size(y1)+size(y2))
    integer :: i1, i2, n1, n2
    i1 = 1
    i2 = 1
    n1 = size(x1)
    n2 = size(x2)
    do while (i1.le.n1.and.i2.le.n2)
      if (x1(i1)+y1(i1).le.x2(i2)+y2(i2)) then
        x(i1+i2-1) = x1(i1)
        y(i1+i2-1) = y1(i1)
        i1 = i1+1
      else
        x(i1+i2-1) = x2(i2)
        y(i1+i2-1) = y2(i2)
        i2 = i2+1
      end if
    end do
    if (i1.le.n1) then
      x(i1+i2-1:n1+n2) = x1(i1:n1)
      y(i1+i2-1:n1+n2) = y1(i1:n1)
    else if (i2.le.n2) then
      x(i1+i2-1:n1+n2) = x2(i2:n2)
      y(i1+i2-1:n1+n2) = y2(i2:n2)
    end if
    x1 = x(1:n1)
    x2 = x(n1+1:n1+n2)
    y1 = y(1:n1)
    y2 = y(n1+1:n1+n2)
    return
  end subroutine merger
end program zabuton