N, W, D = map(int, input().split()) A0 = [] A1 = [] for _ in range(N): t, w, v = map(int, input().split()) if t == 0: A0.append((w, v)) else: A1.append((w, v)) dp0 = [-1] * (W+1) dp0[0] = 0 for a in A0: for j in range(W, a[0]-1, -1): if dp0[j-a[0]] >= 0 and dp0[j] < dp0[j-a[0]]+a[1]: dp0[j] = dp0[j-a[0]]+a[1] dp1 = [-1] * (W+1) dp1[0] = 0 for a in A1: for j in range(W, a[0]-1, -1): if dp1[j-a[0]] >= 0 and dp1[j] < dp1[j-a[0]]+a[1]: dp1[j] = dp1[j-a[0]]+a[1] ans = 0 for i in range(W+1): for j in range(W+1): if abs(i-j) <= D and i+j <= W and dp0[i] >= 0 and dp1[j] >= 0: ans = max(ans, dp0[i]+dp1[j]) print(ans)