# -*- coding: utf-8 -*-

from itertools import accumulate, product

from operator import itemgetter



def inpl(): return tuple(map(int, input().split()))

N, W = inpl()

I = [inpl() for _ in range(N)]

w1 = I[0][0]

A = {w1 + x: [] for x in range(4)}



for w, v in I:

    A[w].append(v)



for k, v in list(A.items()):

    A[k] = [0] + list(accumulate(sorted(v, reverse=True)))



res = 0

g0 = itemgetter(0)

g1 = itemgetter(1)



for L in product(enumerate(A[w1]),

                 enumerate(A[w1+1]),

                 enumerate(A[w1+2]),

                 enumerate(A[w1+3])):

    if sum([(w1+i)*l for i, l in enumerate(map(g0, L))]) <= W:

        res = max(res, sum(map(g1, L)))



print(res)
# -*- coding: utf-8 -*-

def inpl(): return list(map(int, input().split()))

from itertools import accumulate, product

 

N, W = inpl()

A = []

for _ in range(N):

    A.append(inpl())

B = [[] for _ in range(4)]

w1 = A[0][0]

 

for w, v in A:

    B[w-w1].append(v)

 

B = [sorted(b, reverse=True) for b in B]

C = [[0] + list(accumulate(b)) for b in B]

 

res = 0



for L in product(enumerate(C[0]),

                 enumerate(C[1]),

                 enumerate(C[2]),

                 enumerate(C[3])):

    if sum([(w1+i)*l[0] for i, l in enumerate(L)]) <= W:

        res = max(res, sum([l[1] for l in L]))



print(res)

# -*- coding: utf-8 -*-
from itertools import accumulate, product
from operator import itemgetter


def inpl():
    return tuple(map(int, input().split()))


N, W = inpl()
I = [inpl() for _ in range(N)]
w1 = I[0][0]
A = {w1 + x: [] for x in range(4)}
for w, v in I:
    A[w].append(v)
for k, v in list(A.items()):
    A[k] = [0] + list(accumulate(sorted(v, reverse=True)))
res = 0
g0 = itemgetter(0)
g1 = itemgetter(1)
for L in product(
    enumerate(A[w1]), enumerate(A[w1 + 1]), enumerate(A[w1 + 2]), enumerate(A[w1 + 3])
):
    if sum([(w1 + i) * l for i, l in enumerate(map(g0, L))]) <= W:
        res = max(res, sum(map(g1, L)))
print(res)

# -*- coding: utf-8 -*-
def inpl():
    return list(map(int, input().split()))


from itertools import accumulate, product

N, W = inpl()
A = []
for _ in range(N):
    A.append(inpl())
B = [[] for _ in range(4)]
w1 = A[0][0]
for w, v in A:
    B[w - w1].append(v)
B = [sorted(b, reverse=True) for b in B]
C = [[0] + list(accumulate(b)) for b in B]
res = 0
for L in product(enumerate(C[0]), enumerate(C[1]), enumerate(C[2]), enumerate(C[3])):
    if sum([(w1 + i) * l[0] for i, l in enumerate(L)]) <= W:
        res = max(res, sum([l[1] for l in L]))
print(res)

-from itertools import accumulate, product -from operator import itemgetter +def inpl(): +    return list(map(int, input().split())) -def inpl(): -    return tuple(map(int, input().split())) - +from itertools import accumulate, product -I = [inpl() for _ in range(N)] -w1 = I[0][0] -A = {w1 + x: [] for x in range(4)} -for w, v in I: -    A[w].append(v) -for k, v in list(A.items()): -    A[k] = [0] + list(accumulate(sorted(v, reverse=True))) +A = [] +for _ in range(N): +    A.append(inpl()) +B = [[] for _ in range(4)] +w1 = A[0][0] +for w, v in A: +    B[w - w1].append(v) +B = [sorted(b, reverse=True) for b in B] +C = [[0] + list(accumulate(b)) for b in B] -g0 = itemgetter(0) -g1 = itemgetter(1) -for L in product( -    enumerate(A[w1]), enumerate(A[w1 + 1]), enumerate(A[w1 + 2]), enumerate(A[w1 + 3]) -): -    if sum([(w1 + i) * l for i, l in enumerate(map(g0, L))]) <= W: -        res = max(res, sum(map(g1, L))) +for L in product(enumerate(C[0]), enumerate(C[1]), enumerate(C[2]), enumerate(C[3])): +    if sum([(w1 + i) * l[0] for i, l in enumerate(L)]) <= W: +        res = max(res, sum([l[1] for l in L]))