# convert the following C++ code into pypy3 code # https://yukicoder.me/submissions/1041740 import sys from functools import lru_cache def main(): input = sys.stdin.read data = input().split() N = int(data[0]) M = int(data[1]) index = 2 hand = [] ans1 = 0 for _ in range(N): C, D = int(data[index]), int(data[index + 1]) index += 2 if C == 0: ans1 += D else: hand.append((C, D)) hand.sort(reverse=True, key=lambda x: x[0]) K = 10 dp = [[0] * (M + 1) for _ in range(K + 1)] for _C, D in hand: dp_old = [row[:] for row in dp] # Deep copy for i in range(K + 1): for j in range(M + 1): dp[i][j] = max(dp[i][j], dp_old[i][j]) for i in range(K - 1, -1, -1): C = _C << i for j in range(M - C, -1, -1): dp[i + 1][j + C] = max(dp[i + 1][j + C], dp_old[i][j] + D) ans2 = max(max(row) for row in dp) print(ans1 + ans2) if __name__ == "__main__": main()