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 = [[-10**8 for _ in range(W+1)] for _ in range(len(A0)+1)] dp0[0][0] = 0 for i in range(len(A0)): for j in range(W+1): dp0[i+1][j] = max(dp0[i+1][j], dp0[i][j]) if j+A0[i][0] <= W: dp0[i+1][j+A0[i][0]] = max(dp0[i+1] [j+A0[i][0]], dp0[i][j]+A0[i][1]) dp1 = [[-10**8 for _ in range(W+1)] for _ in range(len(A1)+1)] dp1[0][0] = 0 for i in range(len(A1)): for j in range(W+1): dp1[i+1][j] = max(dp1[i+1][j], dp1[i][j]) if j+A1[i][0] <= W: dp1[i+1][j+A1[i][0]] = max(dp1[i+1] [j+A1[i][0]], dp1[i][j]+A1[i][1]) ans = 0 for i in range(W+1): for j in range(W+1): if abs(i-j) <= D and i+j <= W: ans = max(ans, dp0[len(A0)][i]+dp1[len(A1)][j]) print(ans)