#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 w = 0; w <= W; ++w) dp1[0][w] = 0,dp2[0][w]=0; // DPループ for (int i = 0; i < n; ++i) { for (int j = 0; j <= W; ++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 = 0; j <= W; ++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]; } } // a[k]とb[k]の計算 int tmp = -1e9; for (int k = 0; k <= W; k++) { a[k] = dp1[n][k]; if(k >= 1 && a[k] == tmp) a[k] = -1e9; if(a[k] != -1e9) tmp = a[k]; } tmp = -1e9; for (int k = 0; k <= W; k++) { b[k] = dp2[n][k]; if(k >= 1 && b[k] == tmp) b[k] = -1e9; if(b[k] != -1e9) tmp = b[k]; } 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; }