#include #include using mint = atcoder::modint998244353; //using mint = atcoder::modint1000000007; using namespace atcoder; using namespace std; using ll = long long; using pi = pair; using pl = pair; using vi = vector; using vm = vector; using vvi = vector; using vl = vector; using vvl = vector; using vpi = vector>; using vpl = vector>; #define rep(i,n) for (ll i = 0; i < n; i++) #define replr(i,l,r) for (ll i = l; i < r; i++) set abc = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'}; const int inf =1e9; const ll infl = 4e18; template bool chmax(T &a, const T& b) {if (a < b) {a = b; return true;}return false;} template bool chmin(T &a, const T& b) {if (a > b) {a = b; return true;}return false;} template T max(vector &v) {T m = 0;for (auto x : v) chmax(m, x);return m;} template T min(vector &v) {T m = inf;for (auto x : v) chmin(m, x);return m;} template void print(vector &v){for(auto x : v){cout<> N >> W >> D; vpi wv0, wv1; rep(i,N){ int t,w,v; cin >> t >> w >> v; if(t == 0) wv0.push_back({w,v}); else wv1.push_back({w,v}); } vi dp0(W+1,-1), dp1(W+1,-1); dp0[0] = 0; dp1[0] = 0; rep(i,wv0.size()){ for(int j = W; j >= wv0[i].first; j--){ if (dp0[j-wv0[i].first] == -1) continue; chmax(dp0[j], dp0[j-wv0[i].first] + wv0[i].second); } } rep(i,wv1.size()){ for(int j = W; j >= wv1[i].first; j--){ if (dp1[j-wv1[i].first] == -1) continue; chmax(dp1[j], dp1[j-wv1[i].first] + wv1[i].second); } } int ans=0; rep(i,W+1){ replr(j,-D,D+1){ if(i+j < 0 || i+j > W || 2*i+j > W) continue; if (dp0[i] == -1 || dp1[i+j] == -1) continue; chmax(ans, dp0[i] + dp1[i+j]); } } cout << ans << endl; }