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



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

for i in range(N):

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

    if i == 0:

        w0 = w

        V[0].append(v)

    else:

        V[w-w0].append(v)

J = []

for i in range(4):

    F = sorted(V[i], reverse=True)

    G = [0]

    for f in F:

        G.append(f+G[-1])

    J.append(G)



ans = 0

for s0 in range(len(J[0])):

    for s1 in range(len(J[1])):

        for s2 in range(len(J[2])):

            for s3 in range(len(J[3])):

                if s0*w0 + s1*(w0+1) + s2*(w0+2) + s3*(w0+3) > W:

                    continue

                ans = max(ans, J[0][s0]+J[1][s1]+J[2][s2]+J[3][s3])

print(ans)
import sys

input = sys.stdin.readline



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

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

for i in range(N):

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

    if i == 0: w0 = w

    A[w-w0].append(v)



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

for i in range(4):

    A[i].sort(reverse=True)

    b = 0

    for a in A[i]:

        b += a

        B[i].append(b)

#print(B)

ans = 0

for n1, b1 in enumerate(B[0]):

    for n2, b2 in enumerate(B[1]):

        for n3, b3 in enumerate(B[2]):

            n4 = min((W - n1*w0 - n2*(w0+1) - n3*(w0+2)) // (w0+3), len(B[3])-1)

            if n4 < 0: continue

            ans = max(ans, b1+b2+b3+B[3][n4])



print(ans)
N, W = list(map(int, input().split()))
V = [[] for _ in range(4)]
for i in range(N):
    w, v = list(map(int, input().split()))
    if i == 0:
        w0 = w
        V[0].append(v)
    else:
        V[w - w0].append(v)
J = []
for i in range(4):
    F = sorted(V[i], reverse=True)
    G = [0]
    for f in F:
        G.append(f + G[-1])
    J.append(G)
ans = 0
for s0 in range(len(J[0])):
    for s1 in range(len(J[1])):
        for s2 in range(len(J[2])):
            for s3 in range(len(J[3])):
                if s0 * w0 + s1 * (w0 + 1) + s2 * (w0 + 2) + s3 * (w0 + 3) > W:
                    continue
                ans = max(ans, J[0][s0] + J[1][s1] + J[2][s2] + J[3][s3])
print(ans)

import sys

input = sys.stdin.readline
N, W = list(map(int, input().split()))
A = [[] for _ in range(4)]
for i in range(N):
    w, v = list(map(int, input().split()))
    if i == 0:
        w0 = w
    A[w - w0].append(v)
B = [[0] for _ in range(4)]
for i in range(4):
    A[i].sort(reverse=True)
    b = 0
    for a in A[i]:
        b += a
        B[i].append(b)
# print(B)
ans = 0
for n1, b1 in enumerate(B[0]):
    for n2, b2 in enumerate(B[1]):
        for n3, b3 in enumerate(B[2]):
            n4 = min(
                (W - n1 * w0 - n2 * (w0 + 1) - n3 * (w0 + 2)) // (w0 + 3), len(B[3]) - 1
            )
            if n4 < 0:
                continue
            ans = max(ans, b1 + b2 + b3 + B[3][n4])
print(ans)

+import sys + +input = sys.stdin.readline -V = [[] for _ in range(4)] +A = [[] for _ in range(4)] -        V[0].append(v) -    else: -        V[w - w0].append(v) -J = [] +    A[w - w0].append(v) +B = [[0] for _ in range(4)] -    F = sorted(V[i], reverse=True) -    G = [0] -    for f in F: -        G.append(f + G[-1]) -    J.append(G) +    A[i].sort(reverse=True) +    b = 0 +    for a in A[i]: +        b += a +        B[i].append(b) +# print(B) -for s0 in range(len(J[0])): -    for s1 in range(len(J[1])): -        for s2 in range(len(J[2])): -            for s3 in range(len(J[3])): -                if s0 * w0 + s1 * (w0 + 1) + s2 * (w0 + 2) + s3 * (w0 + 3) > W: -                    continue -                ans = max(ans, J[0][s0] + J[1][s1] + J[2][s2] + J[3][s3]) +for n1, b1 in enumerate(B[0]): +    for n2, b2 in enumerate(B[1]): +        for n3, b3 in enumerate(B[2]): +            n4 = min( +                (W - n1 * w0 - n2 * (w0 + 1) - n3 * (w0 + 2)) // (w0 + 3), len(B[3]) - 1 +            ) +            if n4 < 0: +                continue +            ans = max(ans, b1 + b2 + b3 + B[3][n4])