program main
  implicit none
  type pair
    integer :: a, b
  end type pair
  integer :: n, i, j, t2, t, res
  type(pair), allocatable :: ab(:)
  integer :: dp(0:3000)
  read *, n, t
  allocate (ab(n))
  do j = 1, n
    read *, ab(j)
  end do
  call quicksort(ab)

  res = 0
  dp(:) = 0
  do j = 1, n
    res = max(res, maxval(dp(0:t - 1)) + ab(j)%b)
    do i = 3000, 0, -1  ! 配る
      t2 = ab(j)%a + i
      if (t2 > 3000) cycle
      dp(t2) = max(dp(t2), dp(i) + ab(j)%b)
    end do
  end do

  print "(i0)", res

  contains
  recursive subroutine quicksort(ab)
    type(pair), intent(inout) :: ab(:)
    type(pair) :: pivot, tmp
    integer :: i, j, k, n
    real(4) :: r
    n = size(ab)
    if (n <= 1) return
    i = 1
    j = n
    call random_number(r)
    k = 1 + floor(r * n)
    k = max(1, min(k, n))
    pivot = ab(k)
    do
      do while (pivot % a > ab(i) % a)
        i = i + 1
      end do
      do while (ab(j) % a > pivot % a)
        j = j - 1
      end do
      if (i >= j) exit
      tmp = ab(i)
      ab(i) = ab(j)
      ab(j) = tmp
      i = i + 1
      j = j - 1
    end do
    call quicksort(ab(1:i - 1))
    call quicksort(ab(j + 1:n))
  end subroutine quicksort
end program main
