#include #include #include #include using namespace std; typedef pair pii; int dp[2][5001][5001]; int main(){ memset(dp, -1, sizeof(dp)); dp[0][0][0] = 0; int n, k; cin >> n >> k; vector v; for(int i = 0; i < n; i++){ int p, d; cin >> p >> d; v.push_back({p, d}); } sort(v.rbegin(), v.rend()); for(int i = 0; i < n; i++){ for(int j = k; j >= 0; j--){ dp[1][i+1][j] = max(dp[1][i+1][j], dp[1][i][j]); dp[0][i+1][j] = max(dp[0][i+1][j], dp[0][i][j]); if(dp[0][i][j] != -1){ if(j+v[i].first <= k) dp[1][i+1][j+v[i].first] = max(dp[1][i+1][j+v[i].first], dp[0][i][j]+v[i].second); } if(dp[1][i][j] != -1){ dp[0][i+1][j] = max(dp[0][i+1][j], dp[1][i][j]+v[i].second); dp[1][i+1][j] = max(dp[1][i+1][j], dp[1][i][j]); } } } int ans = 0; for(int i = 0; i <= k; i++){ ans = max(ans, dp[0][n][i]); ans = max(ans, dp[1][n][i]); } cout << ans << endl; return 0; }