def solve():

    from collections import namedtuple

    import sys



    input = sys.stdin.readline

    Dish = namedtuple('Dish', 'idx cost pt')



    n, t = list(map(int, input().split()))

    dishes = []

    for idx in range(1, n + 1):

        cost, pt = list(map(int, input().split()))

        dishes.append(Dish(idx=idx, cost=cost, pt=pt))



    dp_ht = [[0] * t for _ in range(n + 2)]  # head->tail

    dp_th = [[0] * t for _ in range(n + 2)]  # tail->head

    # dp[注文可能な範囲の端][完食にかかる総時間]:= 美味しさの最大値



    for dish in dishes:

        for cost_ in range(t):

            if cost_ >= dish.cost:

                dp_ht[dish.idx][cost_] = max(dp_ht[dish.idx - 1][cost_],

                                             dp_ht[dish.idx - 1][cost_ - dish.cost] + dish.pt)

            else:

                dp_ht[dish.idx][cost_] = dp_ht[dish.idx - 1][cost_]



    for dish in reversed(dishes):

        for cost_ in range(t):

            if cost_ >= dish.cost:

                dp_th[dish.idx][cost_] = max(dp_th[dish.idx + 1][cost_],

                                             dp_th[dish.idx + 1][cost_ - dish.cost] + dish.pt)

            else:

                dp_th[dish.idx][cost_] = dp_th[dish.idx + 1][cost_]



    ret = 0

    for last_dish in dishes:

        for cost_ in range(t):

            ret = max(ret, dp_ht[last_dish.idx - 1][cost_] + dp_th[last_dish.idx + 1][t - 1 - cost_] + last_dish.pt)

    return ret





print((solve()))

def solve():

    from collections import namedtuple

    import sys



    input = sys.stdin.readline

    Dish = namedtuple('Dish', 'idx cost pt')



    n, t = list(map(int, input().split()))

    dishes = []

    for idx in range(1, n + 1):

        cost, pt = list(map(int, input().split()))

        dishes.append(Dish(idx=idx, cost=cost, pt=pt))



    dp = [[0] * 2 for _ in range(t)]

    # dp[完食にかかる総時間][最後の皿を選んだか]:= 美味しさの最大値



    for dish in dishes:

        for cost_ in range(t - 1, -1, -1):

            dp[cost_][True] = max(dp[cost_][True], dp[cost_][False] + dish.pt)

            if cost_ >= dish.cost:

                dp[cost_][False] = max(dp[cost_][False], dp[cost_ - dish.cost][False] + dish.pt)

                dp[cost_][True] = max(dp[cost_][True], dp[cost_ - dish.cost][True] + dish.pt)



    return max(dp[cost_][True] for cost_ in range(t))





print((solve()))



# 最後の皿を選んだかフラグを設ける

# https://sen-comp.hatenablog.com/entry/2019/11/17/003150

def solve():
    from collections import namedtuple
    import sys

    input = sys.stdin.readline
    Dish = namedtuple("Dish", "idx cost pt")
    n, t = list(map(int, input().split()))
    dishes = []
    for idx in range(1, n + 1):
        cost, pt = list(map(int, input().split()))
        dishes.append(Dish(idx=idx, cost=cost, pt=pt))
    dp_ht = [[0] * t for _ in range(n + 2)]  # head->tail
    dp_th = [[0] * t for _ in range(n + 2)]  # tail->head
    # dp[注文可能な範囲の端][完食にかかる総時間]:= 美味しさの最大値
    for dish in dishes:
        for cost_ in range(t):
            if cost_ >= dish.cost:
                dp_ht[dish.idx][cost_] = max(
                    dp_ht[dish.idx - 1][cost_],
                    dp_ht[dish.idx - 1][cost_ - dish.cost] + dish.pt,
                )
            else:
                dp_ht[dish.idx][cost_] = dp_ht[dish.idx - 1][cost_]
    for dish in reversed(dishes):
        for cost_ in range(t):
            if cost_ >= dish.cost:
                dp_th[dish.idx][cost_] = max(
                    dp_th[dish.idx + 1][cost_],
                    dp_th[dish.idx + 1][cost_ - dish.cost] + dish.pt,
                )
            else:
                dp_th[dish.idx][cost_] = dp_th[dish.idx + 1][cost_]
    ret = 0
    for last_dish in dishes:
        for cost_ in range(t):
            ret = max(
                ret,
                dp_ht[last_dish.idx - 1][cost_]
                + dp_th[last_dish.idx + 1][t - 1 - cost_]
                + last_dish.pt,
            )
    return ret


print((solve()))

def solve():
    from collections import namedtuple
    import sys

    input = sys.stdin.readline
    Dish = namedtuple("Dish", "idx cost pt")
    n, t = list(map(int, input().split()))
    dishes = []
    for idx in range(1, n + 1):
        cost, pt = list(map(int, input().split()))
        dishes.append(Dish(idx=idx, cost=cost, pt=pt))
    dp = [[0] * 2 for _ in range(t)]
    # dp[完食にかかる総時間][最後の皿を選んだか]:= 美味しさの最大値
    for dish in dishes:
        for cost_ in range(t - 1, -1, -1):
            dp[cost_][True] = max(dp[cost_][True], dp[cost_][False] + dish.pt)
            if cost_ >= dish.cost:
                dp[cost_][False] = max(
                    dp[cost_][False], dp[cost_ - dish.cost][False] + dish.pt
                )
                dp[cost_][True] = max(
                    dp[cost_][True], dp[cost_ - dish.cost][True] + dish.pt
                )
    return max(dp[cost_][True] for cost_ in range(t))


print((solve()))
# 最後の皿を選んだかフラグを設ける
# https://sen-comp.hatenablog.com/entry/2019/11/17/003150

-    dp_ht = [[0] * t for _ in range(n + 2)]  # head->tail -    dp_th = [[0] * t for _ in range(n + 2)]  # tail->head -    # dp[注文可能な範囲の端][完食にかかる総時間]:= 美味しさの最大値 +    dp = [[0] * 2 for _ in range(t)] +    # dp[完食にかかる総時間][最後の皿を選んだか]:= 美味しさの最大値 -        for cost_ in range(t): +        for cost_ in range(t - 1, -1, -1): +            dp[cost_][True] = max(dp[cost_][True], dp[cost_][False] + dish.pt) -                dp_ht[dish.idx][cost_] = max( -                    dp_ht[dish.idx - 1][cost_], -                    dp_ht[dish.idx - 1][cost_ - dish.cost] + dish.pt, +                dp[cost_][False] = max( +                    dp[cost_][False], dp[cost_ - dish.cost][False] + dish.pt -            else: -                dp_ht[dish.idx][cost_] = dp_ht[dish.idx - 1][cost_] -    for dish in reversed(dishes): -        for cost_ in range(t): -            if cost_ >= dish.cost: -                dp_th[dish.idx][cost_] = max( -                    dp_th[dish.idx + 1][cost_], -                    dp_th[dish.idx + 1][cost_ - dish.cost] + dish.pt, +                dp[cost_][True] = max( +                    dp[cost_][True], dp[cost_ - dish.cost][True] + dish.pt -            else: -                dp_th[dish.idx][cost_] = dp_th[dish.idx + 1][cost_] -    ret = 0 -    for last_dish in dishes: -        for cost_ in range(t): -            ret = max( -                ret, -                dp_ht[last_dish.idx - 1][cost_] -                + dp_th[last_dish.idx + 1][t - 1 - cost_] -                + last_dish.pt, -            ) -    return ret +    return max(dp[cost_][True] for cost_ in range(t)) +# 最後の皿を選んだかフラグを設ける +# https://sen-comp.hatenablog.com/entry/2019/11/17/003150