#include<cstdio>
#include<algorithm>
using namespace std;
long long n,w,td,weight;
long long val0[110],val1[110],val2[110],val3[110];
long long t[4];
long long f[110][55][55][55],ans;
int main(){
    scanf("%lld%lld",&n,&w);
    scanf("%lld",&td); t[0]++; scanf("%lld",&val0[t[0]]);
    for (long long i=2; i<=n; i++) {
        scanf("%lld",&weight);
        if (weight==td) ++t[0],scanf("%lld",&val0[t[0]]);
        if (weight==td+1) ++t[1],scanf("%lld",&val1[t[1]]);
        if (weight==td+2) ++t[2],scanf("%lld",&val2[t[2]]);
        if (weight==td+3) ++t[3],scanf("%lld",&val3[t[3]]);
    }
    long long maxx=0;
    long long maxk=0;
    for (long long i=0; i<4; i++) if (maxx<t[i]) {
        maxx=t[i]; maxk=i;
    }
    sort(val0+1,val0+t[0]+1);
    sort(val1+1,val1+t[1]+1);
    sort(val2+1,val2+t[2]+1);
    sort(val3+1,val3+t[3]+1);
    if (maxk==0) {
        for (long long i=0; i<=t[0]; i++)
            for (long long j=0; j<=t[1]; j++)
                for (long long k=0; k<=t[2]; k++)
                    for (long long u=0; u<=t[3]; u++) {
                        if (i>0) f[i][j][k][u]=max(f[i][j][k][u],f[i-1][j][k][u]+val0[t[0]-i+1]);
                        if (j>0) f[i][j][k][u]=max(f[i][j][k][u],f[i][j-1][k][u]+val1[t[1]-j+1]);
                        if (k>0) f[i][j][k][u]=max(f[i][j][k][u],f[i][j][k-1][u]+val2[t[2]-k+1]);
                        if (u>0) f[i][j][k][u]=max(f[i][j][k][u],f[i][j][k][u-1]+val3[t[3]-u+1]);
                    }
        for (long long i=0; i<=t[0]; i++)
            for (long long j=0; j<=t[1]; j++)
                for (long long k=0; k<=t[2]; k++)
                    for (long long u=0; u<=t[3]; u++)
                        if (i*td+j*(td+1)+k*(td+2)+u*(td+3)<=w) ans=max(ans,f[i][j][k][u]);
        printf("%lld\n",ans);
    }
    if (maxk==1) {
        for (long long i=0; i<=t[1]; i++)
            for (long long j=0; j<=t[0]; j++)
                for (long long k=0; k<=t[2]; k++)
                    for (long long u=0; u<=t[3]; u++) {
                        if (i>0) f[i][j][k][u]=max(f[i][j][k][u],f[i-1][j][k][u]+val1[t[1]-i+1]);
                        if (j>0) f[i][j][k][u]=max(f[i][j][k][u],f[i][j-1][k][u]+val0[t[0]-j+1]);
                        if (k>0) f[i][j][k][u]=max(f[i][j][k][u],f[i][j][k-1][u]+val2[t[2]-k+1]);
                        if (u>0) f[i][j][k][u]=max(f[i][j][k][u],f[i][j][k][u-1]+val3[t[3]-u+1]);
                    }
        for (long long i=0; i<=t[1]; i++)
            for (long long j=0; j<=t[0]; j++)
                for (long long k=0; k<=t[2]; k++)
                    for (long long u=0; u<=t[3]; u++)
                        if (i*(td+1)+j*td+k*(td+2)+u*(td+3)<=w) ans=max(ans,f[i][j][k][u]);
                            printf("%lld\n",ans);
    }
    if (maxk==2) {
        for (long long i=0; i<=t[2]; i++)
            for (long long j=0; j<=t[1]; j++)
                for (long long k=0; k<=t[0]; k++)
                    for (long long u=0; u<=t[3]; u++) {
                        if (i>0) f[i][j][k][u]=max(f[i][j][k][u],f[i-1][j][k][u]+val2[t[2]-i+1]);
                        if (j>0) f[i][j][k][u]=max(f[i][j][k][u],f[i][j-1][k][u]+val1[t[1]-j+1]);
                        if (k>0) f[i][j][k][u]=max(f[i][j][k][u],f[i][j][k-1][u]+val0[t[0]-k+1]);
                        if (u>0) f[i][j][k][u]=max(f[i][j][k][u],f[i][j][k][u-1]+val3[t[3]-u+1]);
                    }
        for (long long i=0; i<=t[2]; i++)
            for (long long j=0; j<=t[1]; j++)
                for (long long k=0; k<=t[0]; k++)
                    for (long long u=0; u<=t[3]; u++)
                        if (i*(td+2)+j*(td+1)+k*(td)+u*(td+3)<=w) ans=max(ans,f[i][j][k][u]);
                            printf("%lld\n",ans);
    }
    if (maxk==3) {
        for (long long i=0; i<=t[3]; i++)
            for (long long j=0; j<=t[1]; j++)
                for (long long k=0; k<=t[2]; k++)
                    for (long long u=0; u<=t[0]; u++) {
                        if (i>0) f[i][j][k][u]=max(f[i][j][k][u],f[i-1][j][k][u]+val3[t[3]-i+1]);
                        if (j>0) f[i][j][k][u]=max(f[i][j][k][u],f[i][j-1][k][u]+val1[t[1]-j+1]);
                        if (k>0) f[i][j][k][u]=max(f[i][j][k][u],f[i][j][k-1][u]+val2[t[2]-k+1]);
                        if (u>0) f[i][j][k][u]=max(f[i][j][k][u],f[i][j][k][u-1]+val0[t[0]-u+1]);
                    }
        for (long long i=0; i<=t[3]; i++)
            for (long long j=0; j<=t[1]; j++)
                for (long long k=0; k<=t[2]; k++)
                    for (long long u=0; u<=t[0]; u++)
                        if (i*(td+3)+j*(td+1)+k*(td+2)+u*(td)<=w) ans=max(ans,f[i][j][k][u]);
        printf("%lld\n",ans);
    }
    return 0;
}

Measured memory_bytes: 127104