def knapsack(ss: list): dp = [-INF] * (W+1) dp[0] = 0 for w, v in ss: for i in reversed(range(W+1)): if dp[i] == -INF: continue if i+w > W: continue dp[i+w] = max(dp[i+w], dp[i] + v) return dp INF = 1 << 60 N, W, D = map(int, input().split()) ss0 = [] ss1 = [] for _ in range(N): t, w, v = map(int, input().split()) if t == 0: ss0.append((w, v)) else: ss1.append((w, v)) dp0 = knapsack(ss0) dp1 = knapsack(ss1) ans = 0 for i in range(W+1): lo = max(0, i-D) hi = min(i+D, W) for j in range(lo, hi+1): if i+j > W: break if dp0[i] == -INF: continue if dp1[j] == -INF: continue ans = max(ans, dp0[i] + dp1[j]) print(ans)