#include using namespace std; #define rep(i,n) for(int i=0;i<(int)(n);i++) #define ALL(v) v.begin(),v.end() typedef long long ll; template using V=vector; template using VV=V>; //B(n,V(n)) const ll INF=1e18; ll dp0[5050][5050],dp1[5050][5050]; int main(){ ios::sync_with_stdio(false); std::cin.tie(nullptr); rep(i,5050) rep(j,5050) dp0[i][j]=-INF,dp1[i][j]=-INF; ll n,ww,d; cin>>n>>ww>>d; VV W(2),V(2); int c0=0,c1=0; rep(i,n){ ll t,w,v; cin>>t>>w>>v; if(t==0) c0++; else c1++; W[t].push_back(w),V[t].push_back(v); } dp0[0][0]=0,dp1[0][0]=0; rep(i,c0){ rep(j,ww+1){ dp0[i+1][j]=max(dp0[i+1][j],dp0[i][j]); if(j+W[0][i]<=ww) dp0[i+1][j+W[0][i]]=max(dp0[i+1][j+W[0][i]],dp0[i][j]+V[0][i]); } } rep(i,c1){ rep(j,ww+1){ dp1[i+1][j]=max(dp1[i+1][j],dp1[i][j]); if(j+W[1][i]<=ww) dp1[i+1][j+W[1][i]]=max(dp1[i+1][j+W[1][i]],dp1[i][j]+V[1][i]); } } vector A(ww+1),B(ww+1); rep(i,ww+1){ A[i]=dp0[c0][i]; B[i]=dp1[c1][i]; } ll ans=0; rep(i,ww+1){ if(A[i]==-INF) continue; ll ma=-INF; for(int j=max(0ll,i-d);j<=min(ww,i+d);j++){ if(i+j>ww) continue; ma=max(ma,B[j]); } ans=max(ans,A[i]+ma); } cout<