#include <iostream>
#define DIM 5001
#define FIN "rucsac.in"
#define FOUT "rucsac.out"
/*
C[i]+C[i-1][j-G[i]], G[i]<=j
C[i][j] =
C[i-1][j], else
*/
typedef struct object {
int weight,
value;
} Object;
int N, Gmax;
Object arr[DIM];
int C[DIM][DIM], P[DIM][DIM];
int main(int argc, char const *argv[]) {
//freopen(FIN, "r", stdin);
//freopen(FOUT, "w", stdout);
std::cin>>N>>Gmax;
for(int i = 1; i <= N; ++i) std::cin>>arr[i].weight>>arr[i].value;
//for(int i = 1; i <= N; ++i) std::cout<<arr[i].weight<<" "<<arr[i].value<<std::endl;
for(int i = 1; i <= N; ++i) {
for(int j = 1; j <= Gmax; ++j) {
if(arr[i].weight <= j && (arr[i].value + C[i-1][j-arr[i].weight]) > C[i-1][j]) {
C[i][j] = arr[i].value + C[i-1][j-arr[i].weight];
} else {
C[i][j] = C[i-1][j];
}
}
}
std::cout<<C[N][Gmax];
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojZGVmaW5lIERJTSA1MDAxCiNkZWZpbmUgRklOICJydWNzYWMuaW4iCiNkZWZpbmUgRk9VVCAicnVjc2FjLm91dCIKCi8qCiAgICAgICAgICBDW2ldK0NbaS0xXVtqLUdbaV1dLCBHW2ldPD1qCkNbaV1bal0gPQogICAgICAgICAgQ1tpLTFdW2pdLCBlbHNlCiovCgp0eXBlZGVmIHN0cnVjdCBvYmplY3QgewogICAgaW50IHdlaWdodCwKICAgICAgICB2YWx1ZTsKfSBPYmplY3Q7CgppbnQgTiwgR21heDsKT2JqZWN0IGFycltESU1dOwppbnQgQ1tESU1dW0RJTV0sIFBbRElNXVtESU1dOwoKaW50IG1haW4oaW50IGFyZ2MsIGNoYXIgY29uc3QgKmFyZ3ZbXSkgewoKICAvL2ZyZW9wZW4oRklOLCAiciIsIHN0ZGluKTsKCiAgLy9mcmVvcGVuKEZPVVQsICJ3Iiwgc3Rkb3V0KTsKCiAgc3RkOjpjaW4+Pk4+PkdtYXg7CgogIGZ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