#include using namespace std; using ll = long long; int main(){ cin.tie(nullptr); ios_base::sync_with_stdio(false); /* dp1(i):=タイプ1での結果 dp2(j):=タイプ2での結果 max(0<=i<=W, max(0, i-D)<=j<=min(W-i, i+D)) dp1(i)+dp2(j) */ ll N, W, D, t, w, v; cin >> N >> W >> D; vector dp1(W+1, -1e18), dp2(W+1, -1e18); dp1[0] = 0; dp2[0] = 0; for (int i=0; i> t >> w >> v; if (t == 1){ for (int j=W; j>=0; j--){ if (j+w<=W) dp1[j+w] = max(dp1[j+w], dp1[j]+v); } } else{ for (int j=W; j>=0; j--){ if (j+w<=W) dp2[j+w] = max(dp2[j+w], dp2[j]+v); } } } ll ans=0; for (int i=0; i<=W; i++){ for (int j=0; j<=W-i; j++){ if (abs(j-i) <= D) ans = max(ans, dp1[i]+dp2[j]); } } cout << ans << endl; return 0; }