#include using namespace std; #define rep(i, n) for (int i = 0; i < (int)(n); i++) using vi = vector; using vvi = vector; #define INF 1e9 struct Stone { int w; int v; Stone(int w, int v) : w(w), v(v) {} }; int main() { int N, W, D; cin >> N >> W >> D; vector s0, s1; rep(i, N) { int ti, wi, vi; cin >> ti >> wi >> vi; if (ti == 0) s0.push_back(Stone(wi, vi)); if (ti == 1) s1.push_back(Stone(wi, vi)); } int n0 = (int)s0.size(); int n1 = (int)s1.size(); vi w0(n0+1, 0), w1(n1+1, 0); rep(i, n0) w0[i+1] = w0[i] + s0[i].w; rep(i, n1) w1[i+1] = w1[i] + s1[i].w; vvi dp0(n0+1, vi (W+1, 0)); vvi dp1(n1+1, vi (W+1, 0)); auto f = [&](int n, int m, vvi & dp, vector &s, vi &w) { rep(i, n) { rep(j, W) { if (j > w[n]) break; if (j == s[i].w) dp[i+1][j] = max(s[i].v, dp[i][j]); else if (j >= s[i].w && dp[i][j - s[i].w] > 0) dp[i+1][j] = max(dp[i][j - s[i].w] + s[i].v, dp[i][j]); else dp[i+1][j] = dp[i][j]; } } }; f(n0, W, dp0, s0, w0); f(n1, W, dp1, s1, w1); int m = 0; rep(i, W+1) rep(j, W+1-i) { if (dp0[n0][i] == 0 || dp1[n1][j] == 0) continue; if (abs(i - j) <= D) m = max(dp0[n0][i] + dp1[n1][j], m); } cout << m << endl; return 0; }