#include using namespace std; int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int n, k; cin >> n >> k; vector p(n), d(n); for (int i = 0; i < n; ++i) { cin >> p[i] >> d[i]; } vector idx(n); iota(begin(idx), end(idx), 0); sort(begin(idx), end(idx), [&](int i, int j) { return p[i] > p[j]; }); auto chmax = [](auto&& l, auto r) { l = max(l, r); }; vector< vector > dp(2, vector(k + 1, -1e9)); dp[0][0] = 0; for (int i : idx) { auto ndp = dp; for (int x = 0; x <= k; ++x) { if (x + p[i] <= k) { chmax(ndp[1][x + p[i]], dp[0][x] + d[i]); } chmax(ndp[0][x], dp[1][x] + d[i]); } swap(dp, ndp); } int res = 0; for (auto v : dp) { chmax(res, *max_element(begin(v), end(v))); } cout << res << '\n'; }