#include using namespace std; #define rep(i,a,b) for(int i=(a);i<(int)(b);i++) int main() { int n, W, D; cin >> n >> W >> D; vector t(n), w(n), v(n); rep(i,0,n) { cin >> t[i] >> w[i] >> v[i]; } vector> dp(W+1); dp[0][0] = 0; rep(i,0,n) { for(int j=W;j>=w[i];j--) { auto it = dp[j - w[i]].begin(); while (it != dp[j - w[i]].end()) { int diff = it->first; int new_diff = (t[i] == 0) ? (diff + w[i]) : (diff - w[i]); int value = it->second + v[i]; auto& current_map = dp[j]; auto found = current_map.find(new_diff); if (found == current_map.end() || found->second < value) { current_map[new_diff] = value; } ++it; } } } int max_value = 0; rep(j,0,W+1) { for(auto &p:dp[j]) { if(abs(p.first)<=D) { max_value=max(max_value,p.second); } } } cout << max_value << endl; return 0; }