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

from collections import*

d=defaultdict(int)

d[0]=0

for i in range(N):

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

    for k,b in list(d.copy().items()):

        if k+w<=W:

            d[k+w]=max(d[k+w],b+v)

print((max(d.values())))
import functools

from collections import*

@functools.lru_cache(maxsize=None)

def Knapsack(N,W):

    for i in range(N):

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

        for k,b in list(d.copy().items()):

            if k+w<=W:

                d[k+w]=max(d[k+w],b+v)

    return max(d.values())

N,W=list(map(int,input().split()))   #pypyで実行！

d=defaultdict(int)

d[0]=0

print((Knapsack(N,W)))
N, W = list(map(int, input().split()))
from collections import *

d = defaultdict(int)
d[0] = 0
for i in range(N):
    w, v = list(map(int, input().split()))
    for k, b in list(d.copy().items()):
        if k + w <= W:
            d[k + w] = max(d[k + w], b + v)
print((max(d.values())))

import functools
from collections import *


@functools.lru_cache(maxsize=None)
def Knapsack(N, W):
    for i in range(N):
        w, v = list(map(int, input().split()))
        for k, b in list(d.copy().items()):
            if k + w <= W:
                d[k + w] = max(d[k + w], b + v)
    return max(d.values())


N, W = list(map(int, input().split()))  # pypyで実行！
d = defaultdict(int)
d[0] = 0
print((Knapsack(N, W)))

-N, W = list(map(int, input().split())) +import functools + +@functools.lru_cache(maxsize=None) +def Knapsack(N, W): +    for i in range(N): +        w, v = list(map(int, input().split())) +        for k, b in list(d.copy().items()): +            if k + w <= W: +                d[k + w] = max(d[k + w], b + v) +    return max(d.values()) + + +N, W = list(map(int, input().split()))  # pypyで実行！ -for i in range(N): -    w, v = list(map(int, input().split())) -    for k, b in list(d.copy().items()): -        if k + w <= W: -            d[k + w] = max(d[k + w], b + v) -print((max(d.values()))) +print((Knapsack(N, W)))