#include using namespace std; void chmax(int &a, int b) { a = max(a, b); } int main() { int N, W, D; cin >> N >> W >> D; vector A0(0, array()), A1(0, array()); for (int i = 0; i < N; i++) { int T, W, V; cin >> T >> W >> V; if (T == 0) { A0.push_back({W, V}); } else { A1.push_back({W, V}); } } vector dp0(W + 1, (int)-1e9), dp1(W + 1, (int)-1e9); dp0[0] = 0, dp1[0] = 0; for (auto i : A0) { for (int j = W; j >= i[0]; j--) { if (dp0[j - i[0]] >= 0) chmax(dp0[j], dp0[j - i[0]] + i[1]); } } for (auto i : A1) { for (int j = W; j >= i[0]; j--) { if (dp1[j - i[0]] >= 0) chmax(dp1[j], dp1[j - i[0]] + i[1]); } } int ans = 0; for (int i = 0; i <= W; i++) { for (int j = 0; i + j <= W; j++) { if (abs(i - j) <= D && dp0[i] >= 0 && dp1[j] >= 0) { chmax(ans, dp0[i] + dp1[j]); } } } cout << ans << '\n'; }