N,W=list(map(int,input().split()))

w=[0]*N;v=[0]*N

for i in range(N):w[i],v[i]=list(map(int,input().split()))

V=[[] for _ in [0]*4]

for i in range(N):V[w[i]-w[0]].append(v[i])

for i in range(4):

    V[i].sort(reverse=True)

    for j in range(len(V[i])-1):V[i][j+1]+=V[i][j]

    V[i].insert(0,0)



a=0

for i in range(len(V[0])):

    for j in range(len(V[1])):

            for k in range(len(V[2])):

                for l in range(len(V[3])):

                    if i*w[0]+j*-~w[0]+k*-~-~w[0]+l*-~-~-~w[0]<=W:a=max(a,V[0][i]+V[1][j]+V[2][k]+V[3][l])

print(a)
from collections import defaultdict

from itertools import accumulate

N, W = list(map(int, input().split()))

w = [0] * N; v = [0] * N

d = defaultdict(list)

for i in range(N):

    w[i], v[i] = list(map(int, input().split()))

    d[w[i]].append(v[i])



for i in range(4):

    d[w[0]+i].sort(reverse=True)

    d[w[0]+i].insert(0, 0)

    d[w[0]+i] = list(accumulate(d[w[0]+i]))





ans = 0

for i in range(len(d[w[0]])):

    for j in range(len(d[w[0]+1])):

        for k in range(len(d[w[0]+2])):

            for l in range(len(d[w[0]+3])):

                if i * w[0] + j * (w[0] + 1) + k * (w[0] + 2) + l * (w[0] + 3) <= W:

                    ans = max(ans, d[w[0]][i] + d[w[0]+1][j] + d[w[0]+2][k] + d[w[0]+3][l])

print(ans)

N, W = list(map(int, input().split()))
w = [0] * N
v = [0] * N
for i in range(N):
    w[i], v[i] = list(map(int, input().split()))
V = [[] for _ in [0] * 4]
for i in range(N):
    V[w[i] - w[0]].append(v[i])
for i in range(4):
    V[i].sort(reverse=True)
    for j in range(len(V[i]) - 1):
        V[i][j + 1] += V[i][j]
    V[i].insert(0, 0)
a = 0
for i in range(len(V[0])):
    for j in range(len(V[1])):
        for k in range(len(V[2])):
            for l in range(len(V[3])):
                if i * w[0] + j * -~w[0] + k * -~-~w[0] + l * -~-~-~w[0] <= W:
                    a = max(a, V[0][i] + V[1][j] + V[2][k] + V[3][l])
print(a)

from collections import defaultdict
from itertools import accumulate

N, W = list(map(int, input().split()))
w = [0] * N
v = [0] * N
d = defaultdict(list)
for i in range(N):
    w[i], v[i] = list(map(int, input().split()))
    d[w[i]].append(v[i])
for i in range(4):
    d[w[0] + i].sort(reverse=True)
    d[w[0] + i].insert(0, 0)
    d[w[0] + i] = list(accumulate(d[w[0] + i]))
ans = 0
for i in range(len(d[w[0]])):
    for j in range(len(d[w[0] + 1])):
        for k in range(len(d[w[0] + 2])):
            for l in range(len(d[w[0] + 3])):
                if i * w[0] + j * (w[0] + 1) + k * (w[0] + 2) + l * (w[0] + 3) <= W:
                    ans = max(
                        ans,
                        d[w[0]][i] + d[w[0] + 1][j] + d[w[0] + 2][k] + d[w[0] + 3][l],
                    )
print(ans)

+from collections import defaultdict +from itertools import accumulate + +d = defaultdict(list) -V = [[] for _ in [0] * 4] -for i in range(N): -    V[w[i] - w[0]].append(v[i]) +    d[w[i]].append(v[i]) -    V[i].sort(reverse=True) -    for j in range(len(V[i]) - 1): -        V[i][j + 1] += V[i][j] -    V[i].insert(0, 0) -a = 0 -for i in range(len(V[0])): -    for j in range(len(V[1])): -        for k in range(len(V[2])): -            for l in range(len(V[3])): -                if i * w[0] + j * -~w[0] + k * -~-~w[0] + l * -~-~-~w[0] <= W: -                    a = max(a, V[0][i] + V[1][j] + V[2][k] + V[3][l]) -print(a) +    d[w[0] + i].sort(reverse=True) +    d[w[0] + i].insert(0, 0) +    d[w[0] + i] = list(accumulate(d[w[0] + i])) +ans = 0 +for i in range(len(d[w[0]])): +    for j in range(len(d[w[0] + 1])): +        for k in range(len(d[w[0] + 2])): +            for l in range(len(d[w[0] + 3])): +                if i * w[0] + j * (w[0] + 1) + k * (w[0] + 2) + l * (w[0] + 3) <= W: +                    ans = max( +                        ans, +                        d[w[0]][i] + d[w[0] + 1][j] + d[w[0] + 2][k] + d[w[0] + 3][l], +                    ) +print(ans)