#include<bits/stdc++.h>
using namespace std;
#define faster_io ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
typedef long long ll;
#define bug(a)       cerr << #a << " : " << a << endl;
#define itc(n) char(n+48)
#define cti(ch) (ch-48)

int n,W,w[1005],p[1005],dp[1005][1005];
int Knapsack(int pos, int pw)
{
    if(pos>=n)return 0;
    int p1 , p2;
    if(dp[pos][pw] !=  -1)return dp[pos][pw];
    if(pw + w[pos] <= W) p2 = Knapsack(pos + 1 , pw + w[pos]) + p[pos];
    else p2 = 0;
    p1 = Knapsack(pos + 1 , pw);
    dp[pos][pw] = max(p1 , p2);
    return dp[pos][pw];
}


int main()
{
//    freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    faster_io;

    cin>>n>>W;
//    for (int i=0; i<n; i++){
//        cin>>p[i];
//    }
    memset(dp,-1,sizeof dp);

    for (int i=0; i<n; i++){
        cin>>w[i]>>p[i];
    }

    cout<<Knapsack(0,0)<<endl;

    return 0;
}
