import sys input = sys.stdin.buffer.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], reverse=True) dp0 = [[-10**8 for _ in range(K+1)] for _ in range(N+1)] dp1 = [[-10**8 for _ in range(K+1)] for _ in range(N+1)] dp0[0][0] = 0 for i in range(N): p, d = PD[i] for k in range(K+1): dp0[i+1][k] = max(dp0[i+1][k], dp0[i][k], dp1[i][k] + d) dp1[i+1][k] = max(dp1[i+1][k], dp1[i][k]) if k + p <= K: dp1[i+1][k+p] = max(dp1[i+1][k+p], dp0[i][k] + d) print(max(max(dp0[-1]), max(dp1[-1]))) if __name__ == '__main__': main()