#include using namespace std; typedef long long ll; typedef pair l_l; typedef pair i_i; template inline bool chmax(T &a, T b) { if(a < b) { a = b; return true; } return false; } template inline bool chmin(T &a, T b) { if(a > b) { a = b; return true; } return false; } #define EPS (1e-7) #define INF (1e9) #define PI (acos(-1)) //const ll mod = 1000000007; int N, K; int dp[5001][5001][2]; vector INPUT; int main() { //cout.precision(10); cin.tie(0); ios::sync_with_stdio(false); cin >> N >> K; INPUT.resize(N); for(int i = 0; i < N; i++) { cin >> INPUT[i].first >> INPUT[i].second; } sort(INPUT.begin(), INPUT.end(), greater()); for(int i = 0; i <= 5000; i++) { for(int j = 0; j <= 5000; j++) { dp[i][j][1] = -1e9; } } for(int i = 0; i < N; i++) { int P = INPUT[i].first; int V = INPUT[i].second; for(int j = 0; j <= K; j++) { chmax(dp[i+1][j][0], dp[i][j][0]); chmax(dp[i+1][j][1], dp[i][j][1]); chmax(dp[i+1][j][0], dp[i][j][1] + V); if(j + P <= K) { chmax(dp[i+1][j+P][1], dp[i][j][0] + V); } } } int ans = 0; for(int i = 0; i <= K; i++) { chmax(ans, dp[N][i][0]); chmax(ans, dp[N][i][1]); } cout << ans << endl; return 0; }