from collections import defaultdict dic = defaultdict(int) N, W, D = map(int, input().split()) TWV = [list(map(int, input().split())) for _ in range(N)] dp = [[-1] * (W + 1) for _ in range(2)] dp[0][0] = 0 dp[1][0] = 0 for t, w, v in TWV: for i in reversed(range(W - w + 1)): if dp[t][i] >= 0: dp[t][i + w] = max(dp[t][i + w], dp[t][i] + v) ans = 0 for i in range(W + 1): for j in range(W - i + 1): if abs(i - j) > D: continue if dp[0][i] != -1 and dp[1][i] != -1: ans = max(ans, dp[0][i] + dp[1][j]) print(ans)