#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; typedef pair P; typedef pair P1; typedef pair P2; #define pb push_back #define mp make_pair #define fi first #define sc second #define rep(i,x) for(int i=0;ivec; int dp[2][5005],dp2[2][5005]; int main(){ cin >> n >> k; rep(i,n){ int a,b; cin >> a >> b; vec.pb(P(a,b)); } SORT(vec); reverse(vec.begin(),vec.end()); rep(i,2)rep(j,5005){ dp[i][j] = dp2[i][j] = -INF; } dp[0][0] = 0; int cur = 0,nxt = 1; rep(i,n){ rep(j,5005) dp[nxt][j] = dp2[nxt][j] = -INF; rep(j,k+1){ if(dp[cur][j] < 0) continue; dp[nxt][j] = max(dp[nxt][j],dp[cur][j]); if(j+vec[i].fi <= k){ dp[nxt][j+vec[i].fi] = max(dp[nxt][j+vec[i].fi],dp[cur][j]+vec[i].sc); dp2[nxt][j+vec[i].fi] = max(dp2[nxt][j+vec[i].fi],dp[cur][j]+vec[i].sc); } } rep(j,k+1){ if(dp2[cur][j] < 0) continue; dp2[nxt][j] = max(dp2[nxt][j],dp2[cur][j]); dp[nxt][j] = max(dp[nxt][j],dp2[cur][j]+vec[i].sc); if(j+vec[i].fi <= k){ dp2[nxt][j+vec[i].fi] = max(dp2[nxt][j+vec[i].fi],dp2[cur][j]+vec[i].sc); } } swap(cur,nxt); } ll ans = 0; rep(j,k+1){ ans = max(ans,1LL*dp[cur][j]); ans = max(ans,1LL*dp2[cur][j]); } cout << ans << endl; }