#define REP(a,b) for(int a=0; a<(b); ++a)
#define FWD(a,b,c) for(int a=(b); a<(c); ++a)
#define FWDS(a,b,c,d) for(int a=(b); a<(c); a+=d)
#define BCK(a,b,c) for(int a=(b); a>(c); --a)
#define ALL(a) (a).begin(), (a).end()
#define SIZE(a) ((int)(a).size())
#define VAR(x) #x ": " << x << " "
#define FILL(x,y) memset(x,y,sizeof(x))
#define MIN(a,b) (((a)<(b))?(a):(b))
#define MAX(a,b) (((a)>(b))?(a):(b))
#define x first
#define y second
#define st first
#define nd second
#define pb push_back
#define l(n) (n<<1)
#define r(n) ((n<<1)+1)
#define f(n) (n>>1)
#define lsb(a) (a&-a)
#include<vector>
#include<array>
using namespace std;
#include<fstream>
ifstream cin("rucsac.in");
ofstream cout("rucsac.out");
const int NMAX = 5005;
const int WMAX = 10005;
const int INF = 0x3f3f3f3f;
const int dx[] = {0,0,-1,1}; //1,1,-1,1};
const int dy[] = {-1,1,0,0}; //1,-1,1,-1};
typedef long long LL;
typedef pair<int, int> PII;
typedef long double K;
typedef pair<K, K> PKK;
typedef vector<int> VI;
int n,w;
int P[NMAX], W[NMAX];
array<int,WMAX> dpi1, dpi;
int main()
{
cin>>n>>w;
FWD(i,1,n+1){
cin>>W[i]>>P[i];
}
FWD(i,1,n+1){
FWD(j,0,w+1)
{
dpi[j]=dpi1[j];
if(W[i]<=j)
dpi[j]=MAX(dpi[j], dpi1[j-W[i]] + P[i]);
}
dpi.swap(dpi1);
}
cout<<dpi1[w];
}