def main():

  e=enumerate

  (n,t),*g=[list(map(int,t.split()))for t in open(0)]

  d=[0]

  g.sort()

  dp=[]

  d=[0]*t

  for a,b in g:

    p=d[:]

    for i in range(a,t):

      v=d[i-a]+b

      if v>p[i]:p[i]=v

    dp+=p,

    d=p

  a=m=0

  for(*_,v),(_,w)in zip(dp[-2::-1],g[::-1]):

    if w>m:m=w

    if v+m>a:a=v+m

  print(a)

main()
from numpy import*

N,T,*L=int64(open(0).read().split())

d=zeros(6010,int)

for w,v in sorted(zip(*[iter(L)]*2)):d[w:T+w]=maximum(d[w:T+w],d[:T]+v)

print((max(d)))
def main():
    e = enumerate
    (n, t), *g = [list(map(int, t.split())) for t in open(0)]
    d = [0]
    g.sort()
    dp = []
    d = [0] * t
    for a, b in g:
        p = d[:]
        for i in range(a, t):
            v = d[i - a] + b
            if v > p[i]:
                p[i] = v
        dp += (p,)
        d = p
    a = m = 0
    for (*_, v), (_, w) in zip(dp[-2::-1], g[::-1]):
        if w > m:
            m = w
        if v + m > a:
            a = v + m
    print(a)


main()

from numpy import *

N, T, *L = int64(open(0).read().split())
d = zeros(6010, int)
for w, v in sorted(zip(*[iter(L)] * 2)):
    d[w : T + w] = maximum(d[w : T + w], d[:T] + v)
print((max(d)))

-def main(): -    e = enumerate -    (n, t), *g = [list(map(int, t.split())) for t in open(0)] -    d = [0] -    g.sort() -    dp = [] -    d = [0] * t -    for a, b in g: -        p = d[:] -        for i in range(a, t): -            v = d[i - a] + b -            if v > p[i]: -                p[i] = v -        dp += (p,) -        d = p -    a = m = 0 -    for (*_, v), (_, w) in zip(dp[-2::-1], g[::-1]): -        if w > m: -            m = w -        if v + m > a: -            a = v + m -    print(a) +from numpy import * - -main() +N, T, *L = int64(open(0).read().split()) +d = zeros(6010, int) +for w, v in sorted(zip(*[iter(L)] * 2)): +    d[w : T + w] = maximum(d[w : T + w], d[:T] + v) +print((max(d)))