#include using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int N,W,D; cin >> N >> W >> D; vector> Q0,Q1; for(int i=0; i> t >> w >> v; if(t == 0) Q0.push_back({w,v}); if(t == 1) Q1.push_back({w,v}); } int inf = 1e9; vector dp0(W+1,-inf),dp1(W+1,-inf); dp0.at(0) = 0; dp1.at(0) = 0; for(auto [w,v] : Q0){ for(int i=W; i>=0; i--) if(i+w <= W) dp0.at(i+w) = max(dp0.at(i+w),dp0.at(i)+v); } for(auto [w,v] : Q1){ for(int i=W; i>=0; i--) if(i+w <= W) dp1.at(i+w) = max(dp1.at(i+w),dp1.at(i)+v); } int answer = 0; for(int i=0; i<=W; i++) for(int k=0; k<=W; k++){ if(i+k > W || abs(i-k) > D) continue; answer = max(answer,dp0.at(i)+dp1.at(k)); } cout << answer << endl; }