#include using namespace std; int n, W, d, t[5100], w[5100], v[5100], dp1[5100][5100], dp2[5100][5100], a[5100], b[5100]; int main() { cin >> n >> W >> d; for (int i = 0; i < n; i++) cin >> t[i] >> w[i] >> v[i]; for(int i = 0; i <= n; i++){ for(int j = 0; j <= W; j++){ dp1[i][j]=-1e9,dp2[i][j] = -1e9; } } dp1[0][0]=0,dp2[0][0]=0; for (int i = 0; i < n; ++i) { for (int j = W; j >=0; j--) { if (j >= w[i] && t[i] == 0) dp1[i+1][j] = max(dp1[i][j-w[i]] + v[i], dp1[i][j]); else dp1[i+1][j] = dp1[i][j]; } } for (int i = 0; i < n; ++i) { for (int j = W; j >=0; j--) { if (j >= w[i] && t[i] == 1) dp2[i+1][j] = max(dp2[i][j-w[i]] + v[i], dp2[i][j]); else dp2[i+1][j] = dp2[i][j]; } } for(int i = 0; i <= W; i++) a[i] = dp1[n][i],b[i] = dp2[n][i]; int ans = 0; for(int k = 0; k <= W; k++) { for(int j = 0; j <= W-k; j++) { if (abs(k-j) > d) continue; ans = max(ans, a[k] + b[j]); } } cout << ans; return 0; }