#include using namespace std; int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); long long n,tw,d; cin >> n >> tw >> d; vector d0(tw+1,0); vector d1(tw+1,0); while(n>0){ n--; long long t,w,v; cin >> t >> w >> v; if(t==0){ for(long long i=tw-w;i>=0;i--){ d0[i+w]=max(d0[i+w],d0[i]+v); } } else{ for(long long i=tw-w;i>=0;i--){ d1[i+w]=max(d1[i+w],d1[i]+v); } } } long long res=0; for(long long i=0;i<=tw;i++){ for(long long j=0;i+j<=tw;j++){ if(abs(i-j)<=d){ res=max(res,d0[i]+d1[j]); } } } cout << res << "\n"; return 0; }