from collections import defaultdict





def main():

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

    items = [list(map(int, input().split())) for _ in range(n)]

    DP = defaultdict(int)

    DP[0] = 0

    for weight, value in items:

        exists = list(DP.items())

        for key, total in exists:

            new_key = key + weight

            new_total = total + value

            if DP[new_key] < new_total:

                DP[new_key] = new_total



    candidates = [0]

    for weight, value in sorted(DP.items()):

        if weight > w:

            break

        candidates.append(value)



    print((max(candidates)))





if __name__ == "__main__":

    main()

from collections import defaultdict





def main():

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

    items = [list(map(int, input().split())) for _ in range(n)]

    DP = defaultdict(int)

    DP[0] = 0

    for weight, value in items:

        exists = list(DP.items())

        for key, total in exists:

            new_key = key + weight

            if new_key > w:

                continue

            new_total = total + value

            if DP[new_key] < new_total:

                DP[new_key] = new_total



    print((max(DP.values())))





if __name__ == "__main__":

    main()

from collections import defaultdict


def main():
    n, w = list(map(int, input().split()))
    items = [list(map(int, input().split())) for _ in range(n)]
    DP = defaultdict(int)
    DP[0] = 0
    for weight, value in items:
        exists = list(DP.items())
        for key, total in exists:
            new_key = key + weight
            new_total = total + value
            if DP[new_key] < new_total:
                DP[new_key] = new_total
    candidates = [0]
    for weight, value in sorted(DP.items()):
        if weight > w:
            break
        candidates.append(value)
    print((max(candidates)))


if __name__ == "__main__":
    main()

from collections import defaultdict


def main():
    n, w = list(map(int, input().split()))
    items = [list(map(int, input().split())) for _ in range(n)]
    DP = defaultdict(int)
    DP[0] = 0
    for weight, value in items:
        exists = list(DP.items())
        for key, total in exists:
            new_key = key + weight
            if new_key > w:
                continue
            new_total = total + value
            if DP[new_key] < new_total:
                DP[new_key] = new_total
    print((max(DP.values())))


if __name__ == "__main__":
    main()

+            if new_key > w: +                continue -    candidates = [0] -    for weight, value in sorted(DP.items()): -        if weight > w: -            break -        candidates.append(value) -    print((max(candidates))) +    print((max(DP.values())))