#include using namespace std; //#include //using namespace atcoder; using ll = long long int; using ull = unsigned long long int; using ld = long double; constexpr ll MAX = 2000000000000000000; constexpr ld PI = 3.14159265358979; constexpr ll MOD = 0;//2024948111; ld dotorad(ld K){ return PI * K / 180.0; } ld radtodo(ld K){ return K * 180.0 / PI; } mt19937 mt; void randinit(){ srand((unsigned)time(NULL));mt = mt19937(rand()); } int main(){ ll N,W,D; cin >> N >> W >> D; vector DP1(W + 1,-MAX),DP2(W + 1,-MAX); DP1[0] = 0; DP2[0] = 0; for(ll i = 0;i < N;i++){ ll t,w,v; cin >> t >> w >> v; if(t == 1){ for(ll j = W;j >= w;j--){ DP1[j] = max(DP1[j],DP1[j - w] + v); } } else{ for(ll j = W;j >= w;j--){ DP2[j] = max(DP2[j],DP2[j - w] + v); } } } ll ans = 0; for(ll i = 0;i <= W;i++){ for(ll j = 0;j <= W;j++){ if(i + j <= W && abs(i - j) <= D){ ans = max(ans,DP1[i] + DP2[j]); } } } cout << ans << endl; }