N,W,D = map(int,input().split()) A = [list(map(int,input().split())) for _ in range(N)] A0 = [] A1 = [] for i in range(N): t,w,v = A[i] if t==0: A0.append((w,v)) else: A1.append((w,v)) N0 = len(A0) N1 = len(A1) A0 = [0]+A0 A1 = [0]+A1 INFTY = 3*10**7 dp0 = [[-INFTY for _ in range(W+1)] for _ in range(N0+1)] dp1 = [[-INFTY for _ in range(W+1)] for _ in range(N1+1)] dp0[0][0] = 0 if N0>0: dp0[1][0] = 0 dp0[1][A0[1][0]] = A0[1][1] for i in range(2,N0+1): for j in range(W+1): dp0[i][j] = dp0[i-1][j] if j>=A0[i][0]: dp0[i][j] = max(dp0[i][j],dp0[i-1][j-A0[i][0]]+A0[i][1]) dp1[0][0] = 0 if N1>0: dp1[1][0] = 0 dp1[1][A1[1][0]] = A1[1][1] for i in range(2,N1+1): for j in range(W+1): dp1[i][j] = dp1[i-1][j] if j>=A1[i][0]: dp1[i][j] = max(dp1[i][j],dp1[i-1][j-A1[i][0]]+A1[i][1]) ans = 0 for j0 in range(W+1): for j1 in range(W+1): if abs(j0-j1)>D or j0+j1>W:continue ans = max(ans,dp0[N0][j0]+dp1[N1][j1]) print(ans)