#include #include #include #include using namespace std; #define REP(i, S, E) for(int i=(S); i<=(E); i++) int N, K; int dp[5009][5009][2]; vector> pizza; int main(void){ cin >> N >> K; pizza.push_back({-1, -1}); REP(i, 1, N){ int p, d; cin >> p >> d; pizza.push_back({p, d}); } sort(pizza.begin(), pizza.end()); REP(i, 1, N){ REP(j, 0, K){ int p = pizza[i].first; int d = pizza[i].second; dp[i][j][0] = max(dp[i][j][0], dp[i-1][j][0]); dp[i][j][0] = max(dp[i][j][0], dp[i-1][j][1] + d); if (p > j){ dp[i][j][1] = max(dp[i][j][1], dp[i-1][j][1]); }else{ dp[i][j][1] = max(dp[i][j][1], dp[i-1][j][1]); dp[i][j][1] = max(dp[i][j][1], dp[i-1][j-p][0] + d); } } } cout << dp[N][K][1] << endl; return 0; }