#include <iostream>
#include <cstdio>
#include <algorithm>
#define x first
#define y second
using namespace std;

const int N = 3e3+5;
int n, m, f[N], ans, b[N];
pair<int, int> a[N];

int main()
{
    scanf("%d%d",&n,&m); m-=1;
    for(int i=1; i<=n; ++i) scanf("%d%d",&a[i].x,&a[i].y);
    sort(a+1, a+1+n);
    for(int i=n; i>=1; --i) b[i] = max(b[i+1], a[i+1].y);
    ans = max(b[1], a[1].y);

    for(int i=1; i<=n; ++i) for(int j=m; j>=a[i].x; --j) {
        f[j] = max(f[j], f[j-a[i].x] + a[i].y);
        ans = max(ans, f[j] + b[i]);
    }
    printf("%d",ans);
}
