#include <bits/stdc++.h>
#define INF 400000000000
#define MOD 1000000007
#define MAXN 1000005
#define ins insert
#define pb push_back
#define mp make_pair
#define sz size
#define rep(i, n) for(int i = 0; i < n; ++i)
#define sd(n) scanf("%d",&n)
#define sll(n) scanf("%I64d",&n)
#define pdn(n) printf("%d\n",n)
#define plln(n) printf("%I64d\n",n)
#define pd(n) printf("%d ",n)
#define nl() printf("\n")
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<ll> vi;
typedef vector<vi> vii;
typedef pair<ll, ll> pii;
ll n, m; vector< pair<pii, ll> > a;
ll dp[10005][701][2];
ll solve(ll idx, ll rem, ll act) {
if(idx == 0) {
if(rem-a[idx].first.second < 0) {
if(act <= 1 && (rem-a[idx].first.second+a[idx].second >= 0))
return a[idx].first.first;
return 0;
}
return a[idx].first.first;
}
if(dp[idx][rem][act] != -1)
return dp[idx][rem][act];
ll x = a[idx].first.first+solve(idx-1, rem+a[idx].second-a[idx].first.second, act+1);
ll y = a[idx].first.first+solve(idx-1, rem-a[idx].first.second, act);
ll ans = 0;
if(rem-a[idx].first.second >= 0)
ans = max(ans, y);
if(rem+a[idx].second-a[idx].first.second >= 0 && (act <= 1))
ans = max(ans, x);
return dp[idx][rem][act] = max(ans, solve(idx-1, rem, act));
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
memset(dp, -1, sizeof dp);
rep(i, n) {
ll pi, wi, di;
cin >> pi >> wi >> di;
a.pb(mp(mp(pi, wi), di));
}
ll ans = solve(n-1, m, 0);
cout << ans;
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CiNkZWZpbmUgSU5GIDQwMDAwMDAwMDAwMAojZGVmaW5lIE1PRCAxMDAwMDAwMDA3CiNkZWZpbmUgTUFYTiAxMDAwMDA1CiNkZWZpbmUgaW5zIGluc2VydAojZGVmaW5lIHBiIHB1c2hfYmFjawojZGVmaW5lIG1wIG1ha2VfcGFpcgojZGVmaW5lI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