n, k = map(int, input().split()) info = [list(map(int, input().split())) for i in range(n)] info = sorted(info, reverse = True) dp = [[0]*(k+1) for _ in range(2)] for i in range(n): cost, val = info[i] for j in range(k+1)[::-1]: # i番目を無料としてもらう if dp[1][j] != 0: dp[0][j] = max(dp[1][j] + val, dp[0][j]) # i番目をお金を払ってもらう if j - cost >= 0: dp[1][j] = max(dp[0][j - cost] + val, dp[1][j]) print(max(dp[0][k], dp[1][k]))