import sys import io, os input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline def main(): n, k = map(int, input().split()) PD = [] for i in range(n): p, d = map(int, input().split()) PD.append((p,d)) PD.sort(key=lambda x: -x[0]) INF = 10**18 dp0 = [-INF]*(k+1) dp1 = [-INF]*(k+1) dp0[0] = 0 for p, d in PD: nx0 = [-INF]*(k+1) nx1 = [-INF]*(k+1) for i in range(k+1): nx0[i] = dp0[i] for i in range(k+1): nx1[i] = dp1[i] for i in range(k+1): if dp0[i] == -INF: continue if i+p <= k: nx1[i+p] = max(nx1[i+p], dp0[i]+d) for i in range(k+1): if dp1[i] == -INF: continue nx0[i] = max(nx0[i], dp1[i]+d) dp0 = nx0 dp1 = nx1 ans = -INF for i in range(k+1): ans = max(ans, dp0[i], dp1[i]) print(ans) if __name__ == '__main__': main()