N, W, D = map(int, input().split()) stone = [list(map(int, input().split())) for _ in range(N)] def DP(A): dp = [[-1]*5001 for _ in range(len(A)+1)] dp[0][0] = 0 for i in range(len(A)): w, v = A[i] for j in range(5001): if dp[i][j] == -1: continue dp[i+1][j] = max(dp[i+1][j], dp[i][j]) if j+w <= 5000: dp[i+1][j+w] = max(dp[i+1][j+w], dp[i][j]+v) return dp[-1] zero = [] one = [] for t, w, v in stone: if t == 0: zero.append((w, v)) else: one.append((w, v)) zero = DP(zero) one = DP(one) ans = 0 for i in range(5001): for j in range(i-D, i+D+1): if 0 <= j <= 5000 and i+j <= W and zero[i] != -1 and one[j] != -1: ans = max(ans, zero[i]+one[j]) print(ans)