#include #include #include using namespace std; using ll = long long; using ld = long double; using pll = array; using vl = vector; template using vec = vector; template using vv = vec>; template using vvv = vv>; template using minpq = priority_queue, greater>; #define all(a) (a).begin(), (a).end() #define rep(i, n) for (ll i = 0; i < (n); ++i) #define reps(i, l, r) for (ll i = (l); i < (r); ++i) #define rrep(i, l, r) for (ll i = (r) - 1; i >= (l); --i) #define sz(x) (ll)(x).size() template bool chmax(T &a, const T &b) { return a < b ? a = b, true : false; } template bool chmin(T &a, const T &b) { return a > b ? a = b, true : false; } const ll INF = 1LL << 60; using ull = unsigned long long; using ulll = __uint128_t; void solve() { // s // -> (s-d)*2=2s-2d = s + (s - 2d) // -> (s-2d) * 3 = 3s - 6d = 2s-2d + (s - 4d) // -> (s-3d) * 4 = 4s - 12d = 3s-6d + (s-6d) ll n, b; cin >> n >> b; vec items; // cost, value rep(i, n) { ll p, c, s, d; cin >> p >> c >> s >> d; rrep(i, 0, c) { items.push_back({p, s - d * 2 * i}); } } vv dp(items.size() + 1, vl(b+1, -INF)); dp[0][0] = 0; rep(i, (ll)items.size()) { auto [cost, value] = items[i]; // cout << cost << " " << value << endl; rep(j, b+1) { chmax(dp[i+1][j], dp[i][j]); // cout << dp[i][j] << " \n"[j == b]; if(dp[i][j] == -INF) continue; if(j + cost > b) continue; chmax(dp[i+1][j + cost], dp[i][j] + value); } } cout << *max_element(all(dp.back())) << endl; } int main() { cin.tie(nullptr); ios_base::sync_with_stdio(false); cout << fixed << setprecision(20); int t = 1; // cin >> t; while (t--) { solve(); } }