import sys

from collections import defaultdict, deque



def main():

    input = sys.stdin.readline

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

    A = [tuple(map(int, input().split())) for _ in range(N)]



    d = defaultdict(lambda: 0)

    q = deque()

    for a in A:

        while len(q) > 0:

            w0, v0 = q.popleft()



            w1 = w0 + a[0]

            v1 = v0 + a[1]



            if w1 <= W:

                d[w1] = max(d[w1], v1)



            d[w0] = max(d[w0], v0)



        if a[0] <= W:

            q.append(a)



        for key in list(d.keys()):

            q.append((key, d[key]))



    ans = 0

    for key in list(d.keys()):

        ans = max(ans, d[key])



    return ans





if __name__ == '__main__':

    print((main()))

# Editorial



import sys

from itertools import accumulate



def main():

    input = sys.stdin.readline

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



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

    w0, v0 = list(map(int, input().split()))

    V[0].append(v0)

    for _ in range(N-1):

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

        V[w-w0].append(v)



    for i in range(4):

        V[i] = list(accumulate([0] + sorted(V[i], key=lambda x: -x)))



    ans = 0

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

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

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

                weight = w0 * i + (w0 + 1) * j + (w0 + 2) * k

                if weight <= W:

                    l  = min(len(V[3]) - 1, (W - weight) // (w0 + 3))

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



    return ans





if __name__ == '__main__':

    print((main()))

import sys
from collections import defaultdict, deque


def main():
    input = sys.stdin.readline
    N, W = list(map(int, input().split()))
    A = [tuple(map(int, input().split())) for _ in range(N)]
    d = defaultdict(lambda: 0)
    q = deque()
    for a in A:
        while len(q) > 0:
            w0, v0 = q.popleft()
            w1 = w0 + a[0]
            v1 = v0 + a[1]
            if w1 <= W:
                d[w1] = max(d[w1], v1)
            d[w0] = max(d[w0], v0)
        if a[0] <= W:
            q.append(a)
        for key in list(d.keys()):
            q.append((key, d[key]))
    ans = 0
    for key in list(d.keys()):
        ans = max(ans, d[key])
    return ans


if __name__ == "__main__":
    print((main()))

# Editorial
import sys
from itertools import accumulate


def main():
    input = sys.stdin.readline
    N, W = list(map(int, input().split()))
    V = [[] for _ in range(4)]
    w0, v0 = list(map(int, input().split()))
    V[0].append(v0)
    for _ in range(N - 1):
        w, v = list(map(int, input().split()))
        V[w - w0].append(v)
    for i in range(4):
        V[i] = list(accumulate([0] + sorted(V[i], key=lambda x: -x)))
    ans = 0
    for i in range(len(V[0])):
        for j in range(len(V[1])):
            for k in range(len(V[2])):
                weight = w0 * i + (w0 + 1) * j + (w0 + 2) * k
                if weight <= W:
                    l = min(len(V[3]) - 1, (W - weight) // (w0 + 3))
                    ans = max(ans, V[0][i] + V[1][j] + V[2][k] + V[3][l])
    return ans


if __name__ == "__main__":
    print((main()))

+# Editorial -from collections import defaultdict, deque +from itertools import accumulate -    A = [tuple(map(int, input().split())) for _ in range(N)] -    d = defaultdict(lambda: 0) -    q = deque() -    for a in A: -        while len(q) > 0: -            w0, v0 = q.popleft() -            w1 = w0 + a[0] -            v1 = v0 + a[1] -            if w1 <= W: -                d[w1] = max(d[w1], v1) -            d[w0] = max(d[w0], v0) -        if a[0] <= W: -            q.append(a) -        for key in list(d.keys()): -            q.append((key, d[key])) +    V = [[] for _ in range(4)] +    w0, v0 = list(map(int, input().split())) +    V[0].append(v0) +    for _ in range(N - 1): +        w, v = list(map(int, input().split())) +        V[w - w0].append(v) +    for i in range(4): +        V[i] = list(accumulate([0] + sorted(V[i], key=lambda x: -x))) -    for key in list(d.keys()): -        ans = max(ans, d[key]) +    for i in range(len(V[0])): +        for j in range(len(V[1])): +            for k in range(len(V[2])): +                weight = w0 * i + (w0 + 1) * j + (w0 + 2) * k +                if weight <= W: +                    l = min(len(V[3]) - 1, (W - weight) // (w0 + 3)) +                    ans = max(ans, V[0][i] + V[1][j] + V[2][k] + V[3][l])