#include using namespace std; using ll = long long; constexpr ll mod = 998244353; template struct Mint { using M=Mint; ll v; M& put(ll x) { v=(x>=1; } return res; } M inv() { return pow(mod-2); } }; using mint = Mint; int main(){ cin.tie(0); cin.sync_with_stdio(0); int n, w, d; cin >> n >> w >> d; vector t(n), a(n), v(n); for(int i = 0; i < n; ++i){ cin >> t[i] >> a[i] >> v[i]; } vector> t0, t1; for(int i = 0; i < n; ++i){ if(t[i] == 0)t0.emplace_back(a[i], v[i]); if(t[i] == 1)t1.emplace_back(a[i], v[i]); } auto f = [&](vector> a){ vector dp(w + 1, -1e9); dp[0] = 0; for(int i = 0; i < a.size(); ++i){ auto nex = dp; auto [wei, val] = a[i]; for(int j = 0; j <= w; ++j){ if(j + wei <= w)nex[j + wei] = max(nex[j + wei], dp[j] + val); } swap(dp, nex); } return dp; }; auto r1 = f(t0); auto r2 = f(t1); int ans = 0; for(int i = 0; i <= w; ++i){ for(int j = 0; i + j <= w; ++j){ if(abs(i - j) <= d) ans = max(ans, r1[i] + r2[j]); } } cout << ans << endl; }