import sys



# sys.stdin = open('d1.in')



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

g = {}

w1 = 0

for i in range(n):

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

    if i == 0:

        w1 = w

        for x in range(w, w + 4):

            g[x] = []

    g[w].append(v)

for w, values in list(g.items()):

    values.sort(reverse=True)



sg = {}

sg[w1] = {}

sg[w1 + 1] = {}

sg[w1 + 2] = {}

sg[w1 + 3] = {}

for a in range(len(g[w1]) + 1):

    sg[w1][a] = sum(g[w1][:a])

for b in range(len(g[w1 + 1]) + 1):

    sg[w1 + 1][b] = sum(g[w1 + 1][:b])

for c in range(len(g[w1 + 2]) + 1):

    sg[w1 + 2][c] = sum(g[w1 + 2][:c])

for d in range(len(g[w1 + 3]) + 1):

    sg[w1 + 3][d] = sum(g[w1 + 3][:d])



res = 0

for a in range(len(g[w1]) + 1):

    for b in range(len(g[w1 + 1]) + 1):

        for c in range(len(g[w1 + 2]) + 1):

            for d in range(len(g[w1 + 3]) + 1):

                total_w = w1 * a + (w1 + 1) * b + (w1 + 2) * c + (w1 + 3) * d

                if total_w > W:

                    continue



                total_v = sg[w1][a] + sg[w1 + 1][b] + sg[w1 + 2][c] + sg[w1 + 3][d]

                if total_v > res:

                    res = total_v

print(res)

import sys



# sys.stdin = open('d1.in')



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

g = {}

w1 = 0

for i in range(n):

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

    if i == 0:

        w1 = w

        for x in range(w, w + 4):

            g[x] = []

    g[w].append(v)

for w, values in list(g.items()):

    values.sort(reverse=True)



sg = {}

sg[w1] = {}

sg[w1 + 1] = {}

sg[w1 + 2] = {}

sg[w1 + 3] = {}

for a in range(len(g[w1]) + 1):

    sg[w1][a] = sum(g[w1][:a])

for b in range(len(g[w1 + 1]) + 1):

    sg[w1 + 1][b] = sum(g[w1 + 1][:b])

for c in range(len(g[w1 + 2]) + 1):

    sg[w1 + 2][c] = sum(g[w1 + 2][:c])

for d in range(len(g[w1 + 3]) + 1):

    sg[w1 + 3][d] = sum(g[w1 + 3][:d])



res = 0

wa = 0

for a in range(len(g[w1]) + 1):

    wa = w1 * a

    if wa > W:

        continue

    for b in range(len(g[w1 + 1]) + 1):

        wb = (w1 + 1) * b

        if wa + wb > W:

            continue

        for c in range(len(g[w1 + 2]) + 1):

            wc = (w1 + 2) * c

            if wa + wb + wc > W:

                continue

            for d in range(len(g[w1 + 3]) + 1):

                wd = (w1 + 3) * d

                if wa + wb + wc + wd > W:

                    continue

                total_w = wa + wb + wc + wd



                total_v = sg[w1][a] + sg[w1 + 1][b] + sg[w1 + 2][c] + sg[w1 + 3][d]

                if total_v > res:

                    res = total_v

print(res)

import sys

# sys.stdin = open('d1.in')
n, W = list(map(int, input().split()))
g = {}
w1 = 0
for i in range(n):
    w, v = list(map(int, input().split()))
    if i == 0:
        w1 = w
        for x in range(w, w + 4):
            g[x] = []
    g[w].append(v)
for w, values in list(g.items()):
    values.sort(reverse=True)
sg = {}
sg[w1] = {}
sg[w1 + 1] = {}
sg[w1 + 2] = {}
sg[w1 + 3] = {}
for a in range(len(g[w1]) + 1):
    sg[w1][a] = sum(g[w1][:a])
for b in range(len(g[w1 + 1]) + 1):
    sg[w1 + 1][b] = sum(g[w1 + 1][:b])
for c in range(len(g[w1 + 2]) + 1):
    sg[w1 + 2][c] = sum(g[w1 + 2][:c])
for d in range(len(g[w1 + 3]) + 1):
    sg[w1 + 3][d] = sum(g[w1 + 3][:d])
res = 0
for a in range(len(g[w1]) + 1):
    for b in range(len(g[w1 + 1]) + 1):
        for c in range(len(g[w1 + 2]) + 1):
            for d in range(len(g[w1 + 3]) + 1):
                total_w = w1 * a + (w1 + 1) * b + (w1 + 2) * c + (w1 + 3) * d
                if total_w > W:
                    continue
                total_v = sg[w1][a] + sg[w1 + 1][b] + sg[w1 + 2][c] + sg[w1 + 3][d]
                if total_v > res:
                    res = total_v
print(res)

import sys

# sys.stdin = open('d1.in')
n, W = list(map(int, input().split()))
g = {}
w1 = 0
for i in range(n):
    w, v = list(map(int, input().split()))
    if i == 0:
        w1 = w
        for x in range(w, w + 4):
            g[x] = []
    g[w].append(v)
for w, values in list(g.items()):
    values.sort(reverse=True)
sg = {}
sg[w1] = {}
sg[w1 + 1] = {}
sg[w1 + 2] = {}
sg[w1 + 3] = {}
for a in range(len(g[w1]) + 1):
    sg[w1][a] = sum(g[w1][:a])
for b in range(len(g[w1 + 1]) + 1):
    sg[w1 + 1][b] = sum(g[w1 + 1][:b])
for c in range(len(g[w1 + 2]) + 1):
    sg[w1 + 2][c] = sum(g[w1 + 2][:c])
for d in range(len(g[w1 + 3]) + 1):
    sg[w1 + 3][d] = sum(g[w1 + 3][:d])
res = 0
wa = 0
for a in range(len(g[w1]) + 1):
    wa = w1 * a
    if wa > W:
        continue
    for b in range(len(g[w1 + 1]) + 1):
        wb = (w1 + 1) * b
        if wa + wb > W:
            continue
        for c in range(len(g[w1 + 2]) + 1):
            wc = (w1 + 2) * c
            if wa + wb + wc > W:
                continue
            for d in range(len(g[w1 + 3]) + 1):
                wd = (w1 + 3) * d
                if wa + wb + wc + wd > W:
                    continue
                total_w = wa + wb + wc + wd
                total_v = sg[w1][a] + sg[w1 + 1][b] + sg[w1 + 2][c] + sg[w1 + 3][d]
                if total_v > res:
                    res = total_v
print(res)

+wa = 0 +    wa = w1 * a +    if wa > W: +        continue +        wb = (w1 + 1) * b +        if wa + wb > W: +            continue +            wc = (w1 + 2) * c +            if wa + wb + wc > W: +                continue -                total_w = w1 * a + (w1 + 1) * b + (w1 + 2) * c + (w1 + 3) * d -                if total_w > W: +                wd = (w1 + 3) * d +                if wa + wb + wc + wd > W: +                total_w = wa + wb + wc + wd