#include #include #include #include #include #include #include #include #include #include #include #include #include #define rep(i,a,b) for(int i=a;i<=b;i++) using namespace std; using ll = long long; using vec = vector; using Graph = vector; using Pair = pair; void debug1(vec v){ for(auto x:v)cout << x << ' '; cout << endl; } void debug2(vector v){ for(auto x:v)cout << '(' << x.first << ',' << x.second << ')' << endl; } void debug3(Graph v){ rep(i,0,v.size()-1)debug1(v[i]); cout << endl; } int main(){ int n,w,d; cin >> n >> w >> d; Graph zer = {{0,0}},one = {{0,0}}; rep(i,1,n){ int t,wi,v; cin >> t >> wi >> v; if(t == 0)zer.push_back({wi,v}); else one.push_back({wi,v}); } ll n1 = zer.size(),n2 = one.size(); Graph dp1(n1,vec(w+1,-1)),dp2(n2,vec(w+1,-1)); dp1[0][0] = 0;dp2[0][0] = 0; if(n1 > 1)rep(i,1,n1-1){ dp1[i][0] = 0; rep(ww,0,w){ if(dp1[i-1][ww] == -1)continue; if(ww + zer[i-1][0] > w)continue; dp1[i][ww + zer[i][0]] = max(dp1[i][ww + zer[i][0]],dp1[i-1][ww] + zer[i][1]); } } if(n2 > 1)rep(i,1,n2-1){ dp2[i][0] = 0; rep(ww,0,w){ if(dp2[i-1][ww] == -1)continue; if(ww + one[i-1][0] > w)continue; dp2[i][ww + one[i][0]] = max(dp2[i][ww + one[i][0]],dp2[i-1][ww] + one[i][1]); } } ll ans = 0; rep(i,0,w){ if(dp1[n1-1][i] == -1)continue; if(min(w-i,i+d) - max(0,i-d) > 0)rep(j,max(0,i-d),min(w-i,i+d)){ if(dp2[n2-1][j] == -1)continue; ans = max(ans,dp1[n1-1][i] + dp2[n2-1][j]); } } //debug3(dp1); //debug3(dp2); cout << ans; }