from collections import defaultdict as dd

from itertools import accumulate as ac

from itertools import product as pr



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

d=dd(list)

for _ in range(N):

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

  d[w].extend([v])

d=dict(d)

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

  d[i].sort(reverse=True)

  d[i]=[0]+list(ac(d[i]))



l=[]

key=list(d.keys())

for val in pr(*list(map(enumerate,list(d.values())))):

  weight=sum(map(lambda x,y:x*y[0],key,val))

  value=sum([x[1] for x in val])

  if weight<=W:

    l.extend([value])



print((max(l)))
from collections import defaultdict

from itertools import accumulate,product



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

d={}

for i in range(N):

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

    if not i:

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

            d[j]=[]

    d[w].append(v)



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

    d[i].sort(reverse=True)

    *d[i],=accumulate([0]+d[i])



ans=0

i,j,k,l=list(d.keys())

for ai,bi in enumerate(d[i]):

    for aj,bj in enumerate(d[j]):

        for ak,bk in enumerate(d[k]):

            for al,bl in enumerate(d[l]):

                if i*ai+j*aj+k*ak+l*al<=W:

                    ans=max(ans,bi+bj+bk+bl)



print(ans)

from collections import defaultdict as dd
from itertools import accumulate as ac
from itertools import product as pr

N, W = list(map(int, input().split()))
d = dd(list)
for _ in range(N):
    w, v = list(map(int, input().split()))
    d[w].extend([v])
d = dict(d)
for i in list(d.keys()):
    d[i].sort(reverse=True)
    d[i] = [0] + list(ac(d[i]))
l = []
key = list(d.keys())
for val in pr(*list(map(enumerate, list(d.values())))):
    weight = sum(map(lambda x, y: x * y[0], key, val))
    value = sum([x[1] for x in val])
    if weight <= W:
        l.extend([value])
print((max(l)))

from collections import defaultdict
from itertools import accumulate, product

N, W = list(map(int, input().split()))
d = {}
for i in range(N):
    w, v = list(map(int, input().split()))
    if not i:
        for j in range(w, w + 4):
            d[j] = []
    d[w].append(v)
for i in list(d.keys()):
    d[i].sort(reverse=True)
    (*d[i],) = accumulate([0] + d[i])
ans = 0
i, j, k, l = list(d.keys())
for ai, bi in enumerate(d[i]):
    for aj, bj in enumerate(d[j]):
        for ak, bk in enumerate(d[k]):
            for al, bl in enumerate(d[l]):
                if i * ai + j * aj + k * ak + l * al <= W:
                    ans = max(ans, bi + bj + bk + bl)
print(ans)

-from collections import defaultdict as dd -from itertools import accumulate as ac -from itertools import product as pr +from collections import defaultdict +from itertools import accumulate, product -d = dd(list) -for _ in range(N): +d = {} +for i in range(N): -    d[w].extend([v]) -d = dict(d) +    if not i: +        for j in range(w, w + 4): +            d[j] = [] +    d[w].append(v) -    d[i] = [0] + list(ac(d[i])) -l = [] -key = list(d.keys()) -for val in pr(*list(map(enumerate, list(d.values())))): -    weight = sum(map(lambda x, y: x * y[0], key, val)) -    value = sum([x[1] for x in val]) -    if weight <= W: -        l.extend([value]) -print((max(l))) +    (*d[i],) = accumulate([0] + d[i]) +ans = 0 +i, j, k, l = list(d.keys()) +for ai, bi in enumerate(d[i]): +    for aj, bj in enumerate(d[j]): +        for ak, bk in enumerate(d[k]): +            for al, bl in enumerate(d[l]): +                if i * ai + j * aj + k * ak + l * al <= W: +                    ans = max(ans, bi + bj + bk + bl) +print(ans)