#include // cout, endl, cin #include // string, to_string, stoi #include // vector #include // min, max, swap, sort, reverse, lower_bound, upper_bound #include // pair, make_pair #include // tuple, make_tuple #include // int64_t, int*_t #include // printf #include // map #include // queue, priority_queue #include // set #include // stack #include // deque #include // unordered_map #include // unordered_set #include // bitset #include // isupper, islower, isdigit, toupper, tolower #include #include #include #include #include #include #include #include #include //#include //using namespace atcoder; #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define rep1(i, n) for (int i = 1; i <= (int)(n); i++) #define repl(i,l,r) for (int i = l; i < (int)(r); i++) #define all(a) a.begin(),a.end() #define Pii pair #define Pll pair #define INFi 1000000001 #define INFl 1000000000000000001 #define ll long long using namespace std; template inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; } template inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; } template void printArray(vector&A){ for(T&a:A){ cout< void printArrayln(vector&A){ for(T&a:A){ cout< std::ostream &operator<<(std::ostream &out, const pair &A){ cout<<"{"< std::ostream &operator<<(std::ostream &out, const map &M){ for(const auto&A:M){ cout<<"{"< std::ostream &operator<<(std::ostream &out, const set &M){ cout<<"{"; for(const auto&A:M){ cout< std::ostream &operator<<(std::ostream &out, const multiset &M){ cout<<"{"; for(const auto&A:M){ cout< std::ostream &operator<<(std::ostream &out, const vector &A){ for(const T &a:A){ cout< void print(Head H, Tail... T) { cout << H << " "; print(T...); } template std::istream &operator>>(std::istream &in,vector&A){ for(T&a:A){ std::cin>>a; } return in; } int main(void){ std::cin.tie(0)->sync_with_stdio(0); int N,W,D;cin>>N>>W>>D; vector w1(0),v1(0); vector w2(0),v2(0); rep(i,N){ int t,w,d;cin>>t>>w>>d; if(t==0){ w1.push_back(w); v1.push_back(d); }else{ w2.push_back(w); v2.push_back(d); } } int N1 = w1.size(); int N2 = w2.size(); vector> dp1(N1+1,vector(W+1,-1)); vector> dp2(N2+1,vector(W+1,-1)); // dp[i][j] := i番目までみて重さがちょうどjのときの価値の最大値 for(int i=1;i<=N1;i++){ for(int j=0;j<=W;j++){ if(j-w1[i-1]==0){ dp1[i][j] = max(dp1[i][j],v1[i-1]); }else if(j-w1[i-1]>0){ if(dp1[i-1][j-w1[i-1]]!=-1) chmax(dp1[i][j],dp1[i-1][j-w1[i-1]]+v1[i-1]); } chmax(dp1[i][j],dp1[i-1][j]); } } for(int i=1;i<=N2;i++){ for(int j=0;j<=W;j++){ if(j-w2[i-1]==0){ dp2[i][j] = max(dp2[i][j],v2[i-1]); }else if(j-w2[i-1]>0){ if(dp2[i-1][j-w2[i-1]]!=-1) chmax(dp2[i][j],dp2[i-1][j-w2[i-1]]+v2[i-1]); } chmax(dp2[i][j],dp2[i-1][j]); } } // 差がD以下になるように選ぶ int ans = 0; for(int i=0;i<=W;i++){ for(int j=0;j<=W;j++){ if(i+j<=W && abs(i-j)<=D && dp1[N1][i]!=-1 && dp2[N2][j]!=-1){ chmax(ans,dp1[N1][i]+dp2[N2][j]); } } } cout<