#include using namespace std; using pint = pair; struct fast_ios { fast_ios(){ cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_; #define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i bool mmax(T &m, const T q) { if (m < q) {m = q; return true;} else return false; } int main() { int N, K; cin >> N >> K; vector PD(N); REP(i, N) cin >> PD[i].first >> PD[i].second; sort(PD.rbegin(), PD.rend()); vector> dp(2, vector(K + 1)); REP(j, K + 1) REP(k, 2) dp[k][j] = -1e9; dp[0][K] = 0; for (auto p : PD) { vector> dpnew = dp; REP(d, 2) REP(j, K + 1) { if (dp[d][j] >= 0) { if (j - p.first >= 0) mmax(dpnew[1][j - p.first], dp[d][j] + p.second); } } REP(j, K + 1) if (dp[1][j] >= 0) mmax(dpnew[0][j], dp[1][j] + p.second); dp = dpnew; } int ret = 0; REP(k, 2) REP(j, K + 1) mmax(ret, dp[k][j]); cout << ret << endl; }