#include using namespace std; using ll = long long; #define FOR(i, l, r) for(ll i = l; i < r; i++) #define rep(i, n) FOR(i, 0, n) #define FORR(i, l, r) for(ll i = r - 1; i >= l; i--) #define ALL(ar) begin(ar), end(ar) template inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); } template inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); } template istream &operator>>(istream &is, pair &p) { is >> p.first >> p.second; return is; } template ostream &operator<<(ostream &os, const pair &p) { os << p.first << ' ' << p.second; return os; } template istream &operator>>(istream &is, vector &v) { for(T &x : v) is >> x; return is; } template ostream &operator<<(ostream &os, const vector &v) { for(size_t i = 0; i < v.size(); i++) os << (i ? " " : "") << v[i]; return os; } //------------------------------------------------- void solve() { int N, W, D; cin >> N >> W >> D; vector w[2], v[2]; rep(i, N) { int a, b, c; cin >> a >> b >> c; w[a].push_back(b); v[a].push_back(c); } vector dp(2, vector(N + 1, vector(W + 1, -(1 << 29)))); dp[0][0][0] = dp[1][0][0] = 0; rep(t, 2) { rep(i, w[t].size()) { rep(j, W) if(dp[t][i][j] >= 0) { chmax(dp[t][i + 1][j], dp[t][i][j]); if(j + w[t][i] <= W) chmax(dp[t][i + 1][j + w[t][i]], dp[t][i][j] + v[t][i]); } } } int mx = 0; rep(i, W + 1) rep(j, W + 1) if(i + j <= W && abs(i - j) <= D) chmax(mx, dp[0][w[0].size()][i] + dp[1][w[1].size()][j]); cout << mx << endl; } int main() { int t = 1; // cin >> t; while(t--) solve(); }