import numpy as np import sys input = sys.stdin.buffer.readline inf = 10**18 N, K = map(int, input().split()) PD = list(tuple(map(int, input().split())) for _ in range(N)) PD.sort(reverse=True) dp0 = np.full(K + 1, -inf, dtype=np.int64) dp1 = np.full_like(dp0, -inf) dp0[0] = 0 for p, d in PD: newDP0 = np.copy(dp0) newDP1 = np.copy(dp1) newDP1[p:] = np.maximum(newDP1[p:], dp0[:-p] + d) newDP0 = np.maximum(newDP0, dp1 + d) dp0 = newDP0 dp1 = newDP1 ans = max(dp0.max(), dp1.max()) print(ans)