#define _USE_MATH_DEFINES #include using namespace std; signed main() { ios::sync_with_stdio(false); cin.tie(0); int n, k; cin >> n >> k; vector> a(n); for (auto& p : a) cin >> p.first >> p.second; vector> dp(2, vector(k + 1, -(1 << 30))); dp[0][0] = dp[1][0] = 0; sort(a.begin(), a.end()); for (int i = 0; i < n; i++) { vector> nxt(2, vector(k + 1, -(1 << 30))); auto p = a[i].first; auto d = a[i].second; for (int u = 0; u < 2; u++) { for (int j = 0; j <= k; j++) { int c = u == 0 ? p : 0; if (j + c <= k) nxt[u ^ 1][j + c] = max(nxt[u ^ 1][j + c], dp[u][j] + d); nxt[u][j] = max(nxt[u][j], dp[u][j]); } } dp.swap(nxt); } cout << *max_element(dp[1].begin(), dp[1].end()) << endl; return 0; }