from collections import defaultdict

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



a=defaultdict(lambda:[])

for i in range(N):

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

	a[w]=a[w]+[v]



for i,j in list(a.items()):

	a[i]=sorted(j,reverse=True)







lst=[]

for i,j in list(a.items()):

	lst.append( (i,len(j)) )



max_val=0

if len(lst)==1:

	w1=lst[0][0]

	c1=lst[0][1]

	for i in range(c1+1):

		if i*w1<=W:

			max_val=max(max_val, sum(a[w1][:i]))



elif len(lst)==2:

	w1=lst[0][0]; w2=lst[1][0]

	c1=lst[0][1]; c2=lst[1][1]

	for i in range(c1+1):

		for j in range(c2+1):

				if i*w1+j*w2<=W:

					max_val=max(max_val, sum(a[w1][:i])+sum(a[w2][:j]))



elif len(lst)==3:

	w1=lst[0][0]; w2=lst[1][0]; w3=lst[2][0]

	c1=lst[0][1]; c2=lst[1][1]; c3=lst[2][1]

	for i in range(c1+1):

		for j in range(c2+1):

			for k in range(c3+1):

				if i*w1+j*w2+k*w3<=W:

					max_val=max(max_val, sum(a[w1][:i])+sum(a[w2][:j])+sum(a[w3][:k]))



elif len(lst)==4:

	w1=lst[0][0]; w2=lst[1][0]; w3=lst[2][0]; w4=lst[3][0]

	c1=lst[0][1]; c2=lst[1][1]; c3=lst[2][1]; c4=lst[3][1]

	for i in range(c1+1):

		for j in range(c2+1):

			for k in range(c3+1):

				for m in range(c4+1):

					if i*w1+j*w2+k*w3+m*w4<=W:

						max_val=max(max_val, sum(a[w1][:i])+sum(a[w2][:j])+sum(a[w3][:k])+sum(a[w4][:m]))



print(max_val)



from collections import defaultdict

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

wv=[]



for i in range(N):

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

	wv.append((w,v))





dp=defaultdict(lambda: 0)	# Weight:Value

dp[0]=0



for w1,v1 in wv:

	d=dp.copy()

	for w,v in list(dp.items()):

		dp[w+w1]=max( dp[w+w1], d[w]+v1 )





l1=[x for x in list(dp.items()) if x[0]<=W]

print(sorted(l1, key=lambda x: x[1],reverse=True)[0][1])

from collections import defaultdict

N, W = list(map(int, input().split()))
a = defaultdict(lambda: [])
for i in range(N):
    w, v = list(map(int, input().split()))
    a[w] = a[w] + [v]
for i, j in list(a.items()):
    a[i] = sorted(j, reverse=True)
lst = []
for i, j in list(a.items()):
    lst.append((i, len(j)))
max_val = 0
if len(lst) == 1:
    w1 = lst[0][0]
    c1 = lst[0][1]
    for i in range(c1 + 1):
        if i * w1 <= W:
            max_val = max(max_val, sum(a[w1][:i]))
elif len(lst) == 2:
    w1 = lst[0][0]
    w2 = lst[1][0]
    c1 = lst[0][1]
    c2 = lst[1][1]
    for i in range(c1 + 1):
        for j in range(c2 + 1):
            if i * w1 + j * w2 <= W:
                max_val = max(max_val, sum(a[w1][:i]) + sum(a[w2][:j]))
elif len(lst) == 3:
    w1 = lst[0][0]
    w2 = lst[1][0]
    w3 = lst[2][0]
    c1 = lst[0][1]
    c2 = lst[1][1]
    c3 = lst[2][1]
    for i in range(c1 + 1):
        for j in range(c2 + 1):
            for k in range(c3 + 1):
                if i * w1 + j * w2 + k * w3 <= W:
                    max_val = max(
                        max_val, sum(a[w1][:i]) + sum(a[w2][:j]) + sum(a[w3][:k])
                    )
elif len(lst) == 4:
    w1 = lst[0][0]
    w2 = lst[1][0]
    w3 = lst[2][0]
    w4 = lst[3][0]
    c1 = lst[0][1]
    c2 = lst[1][1]
    c3 = lst[2][1]
    c4 = lst[3][1]
    for i in range(c1 + 1):
        for j in range(c2 + 1):
            for k in range(c3 + 1):
                for m in range(c4 + 1):
                    if i * w1 + j * w2 + k * w3 + m * w4 <= W:
                        max_val = max(
                            max_val,
                            sum(a[w1][:i])
                            + sum(a[w2][:j])
                            + sum(a[w3][:k])
                            + sum(a[w4][:m]),
                        )
print(max_val)

from collections import defaultdict

N, W = list(map(int, input().split()))
wv = []
for i in range(N):
    w, v = list(map(int, input().split()))
    wv.append((w, v))
dp = defaultdict(lambda: 0)  # Weight:Value
dp[0] = 0
for w1, v1 in wv:
    d = dp.copy()
    for w, v in list(dp.items()):
        dp[w + w1] = max(dp[w + w1], d[w] + v1)
l1 = [x for x in list(dp.items()) if x[0] <= W]
print(sorted(l1, key=lambda x: x[1], reverse=True)[0][1])

-a = defaultdict(lambda: []) +wv = [] -    a[w] = a[w] + [v] -for i, j in list(a.items()): -    a[i] = sorted(j, reverse=True) -lst = [] -for i, j in list(a.items()): -    lst.append((i, len(j))) -max_val = 0 -if len(lst) == 1: -    w1 = lst[0][0] -    c1 = lst[0][1] -    for i in range(c1 + 1): -        if i * w1 <= W: -            max_val = max(max_val, sum(a[w1][:i])) -elif len(lst) == 2: -    w1 = lst[0][0] -    w2 = lst[1][0] -    c1 = lst[0][1] -    c2 = lst[1][1] -    for i in range(c1 + 1): -        for j in range(c2 + 1): -            if i * w1 + j * w2 <= W: -                max_val = max(max_val, sum(a[w1][:i]) + sum(a[w2][:j])) -elif len(lst) == 3: -    w1 = lst[0][0] -    w2 = lst[1][0] -    w3 = lst[2][0] -    c1 = lst[0][1] -    c2 = lst[1][1] -    c3 = lst[2][1] -    for i in range(c1 + 1): -        for j in range(c2 + 1): -            for k in range(c3 + 1): -                if i * w1 + j * w2 + k * w3 <= W: -                    max_val = max( -                        max_val, sum(a[w1][:i]) + sum(a[w2][:j]) + sum(a[w3][:k]) -                    ) -elif len(lst) == 4: -    w1 = lst[0][0] -    w2 = lst[1][0] -    w3 = lst[2][0] -    w4 = lst[3][0] -    c1 = lst[0][1] -    c2 = lst[1][1] -    c3 = lst[2][1] -    c4 = lst[3][1] -    for i in range(c1 + 1): -        for j in range(c2 + 1): -            for k in range(c3 + 1): -                for m in range(c4 + 1): -                    if i * w1 + j * w2 + k * w3 + m * w4 <= W: -                        max_val = max( -                            max_val, -                            sum(a[w1][:i]) -                            + sum(a[w2][:j]) -                            + sum(a[w3][:k]) -                            + sum(a[w4][:m]), -                        ) -print(max_val) +    wv.append((w, v)) +dp = defaultdict(lambda: 0)  # Weight:Value +dp[0] = 0 +for w1, v1 in wv: +    d = dp.copy() +    for w, v in list(dp.items()): +        dp[w + w1] = max(dp[w + w1], d[w] + v1) +l1 = [x for x in list(dp.items()) if x[0] <= W] +print(sorted(l1, key=lambda x: x[1], reverse=True)[0][1])