#include #define rep(i, n) for(int i=0, i##_len=(n); i=0; --i) #define rreps(i, n) for(int i=((int)(n)); i>0; --i) #define all(v) (v).begin(), (v).end() using namespace std; using ll = long long; using ull = unsigned long long; using vi = vector; using vvi = vector>; using vvvi = vector>>; using vl = vector; using vvl = vector>; using vvvl = vector>>; using vs = vector; using pi = pair; using pl = pair; templatebool chmax(T &a, const T &b) { if (abool chmin(T &a, const T &b) { if (bbool chmaxeq(T &a, const T &b) { if (a<=b) { a=b; return 1; } return 0; } templatebool chmineq(T &a, const T &b) { if (b<=a) { a=b; return 1; } return 0; } bool yes(bool a) { cout << (a?"yes":"no") << endl; return a; } bool Yes(bool a) { cout << (a?"Yes":"No") << endl; return a; } bool YES(bool a) { cout << (a?"YES":"NO") << endl; return a; } void _main(); int main() { cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(16); _main(); return 0; } ll INF = 1001001001; void _main() { ll N, W, D; cin >> N >> W >> D; vl w0, v0, w1, v1; rep(i, N) { ll t, w, v; cin >> t >> w >> v; if (t == 0) { w0.push_back(w); v0.push_back(v); } else { w1.push_back(w); v1.push_back(v); } } ll N0 = w0.size(); ll N1 = w1.size(); vvl dp0(N0+1, vl(W+1, -INF)), dp1(N1+1, vl(W+1, -INF)); dp0[0][0] = dp1[0][0] = 0; rep(i, N0) rep(j, W+1) { dp0[i+1][j] = dp0[i][j]; if (j-w0[i] >= 0) chmax(dp0[i+1][j], dp0[i][j-w0[i]]+v0[i]); } rep(i, N1) rep(j, W+1) { dp1[i+1][j] = dp1[i][j]; if (j-w1[i] >= 0) chmax(dp1[i+1][j], dp1[i][j-w1[i]]+v1[i]); } ll ans = 0; rep(i, W+1) rep(j, W+1) { if (abs(i-j) > D || i+j > W) continue; chmax(ans, dp0[N0][i]+dp1[N1][j]); } cout << ans << endl; }