import sys

input = sys.stdin.readline

from itertools import accumulate



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

WV = [[int(i) for i in input().split()] for _ in range(N)]



MINW = WV[0][0]



# それぞれの重さについての価値のリスト

W0 = sorted([0] + [v for w,v in WV if w == MINW])

W1 = sorted([0] + [v for w,v in WV if w == MINW+1])

W2 = sorted([0] + [v for w,v in WV if w == MINW+2])

W3 = sorted([0] + [v for w,v in WV if w == MINW+3])



# 累積和

W0 = tuple(accumulate(W0))

W1 = tuple(accumulate(W1))

W2 = tuple(accumulate(W2))

W3 = tuple(accumulate(W3))



ans = 0

for w0 in range(len(W0)):

    for w1 in range(len(W1)):

        for w2 in range(len(W2)):

            for w3 in range(len(W3)):

                # tmp: 状態(w0,w1,w2,w3)での重さ

                tmp = (len(W0) - (w0 + 1)) * MINW

                tmp += (len(W1) - (w1 + 1)) * (MINW+1)

                tmp += (len(W2) - (w2 + 1)) * (MINW+2)

                tmp += (len(W3) - (w3 + 1)) * (MINW+3)



                # ナップサックに入る場合

                if tmp <= W:

                    # tmp: 価値の総和

                    tmp = W0[-1] - W0[w0]

                    tmp += W1[-1] - W1[w1]

                    tmp += W2[-1] - W2[w2]

                    tmp += W3[-1] - W3[w3]

                    ans = max(ans, tmp)



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

WV = [[int(i) for i in input().split()] for _ in range(N)]



MINW = WV[0][0]



W0,W1,W2,W3 = [],[],[],[]

for w,v in WV:

    if w == MINW: W0.append(v)

    elif w == MINW + 1: W1.append(v)

    elif w == MINW + 2: W2.append(v)

    elif w == MINW + 3: W3.append(v)



W0.sort(reverse=True)

W1.sort(reverse=True)

W2.sort(reverse=True)

W3.sort(reverse=True)



W0 = [0] + W0

W1 = [0] + W1

W2 = [0] + W2

W3 = [0] + W3



from itertools import accumulate



W0 = list(accumulate(W0))

W1 = list(accumulate(W1))

W2 = list(accumulate(W2))

W3 = list(accumulate(W3))



ans = 0

for cnt0 in range(len(W0)):

    for cnt1 in range(len(W1)):

        for cnt2 in range(len(W2)):

            for cnt3 in range(len(W3)):

                SUMW = cnt0 * MINW

                SUMW += cnt1 * (MINW + 1)

                SUMW += cnt2 * (MINW + 2)

                SUMW += cnt3 * (MINW + 3)



                if SUMW <= W:

                    SUMV = W0[cnt0]

                    SUMV += W1[cnt1]

                    SUMV += W2[cnt2]

                    SUMV += W3[cnt3]

                    ans = max(ans, SUMV)



print(ans)
import sys

input = sys.stdin.readline
from itertools import accumulate

N, W = list(map(int, input().split()))
WV = [[int(i) for i in input().split()] for _ in range(N)]
MINW = WV[0][0]
# それぞれの重さについての価値のリスト
W0 = sorted([0] + [v for w, v in WV if w == MINW])
W1 = sorted([0] + [v for w, v in WV if w == MINW + 1])
W2 = sorted([0] + [v for w, v in WV if w == MINW + 2])
W3 = sorted([0] + [v for w, v in WV if w == MINW + 3])
# 累積和
W0 = tuple(accumulate(W0))
W1 = tuple(accumulate(W1))
W2 = tuple(accumulate(W2))
W3 = tuple(accumulate(W3))
ans = 0
for w0 in range(len(W0)):
    for w1 in range(len(W1)):
        for w2 in range(len(W2)):
            for w3 in range(len(W3)):
                # tmp: 状態(w0,w1,w2,w3)での重さ
                tmp = (len(W0) - (w0 + 1)) * MINW
                tmp += (len(W1) - (w1 + 1)) * (MINW + 1)
                tmp += (len(W2) - (w2 + 1)) * (MINW + 2)
                tmp += (len(W3) - (w3 + 1)) * (MINW + 3)
                # ナップサックに入る場合
                if tmp <= W:
                    # tmp: 価値の総和
                    tmp = W0[-1] - W0[w0]
                    tmp += W1[-1] - W1[w1]
                    tmp += W2[-1] - W2[w2]
                    tmp += W3[-1] - W3[w3]
                    ans = max(ans, tmp)
print(ans)

N, W = list(map(int, input().split()))
WV = [[int(i) for i in input().split()] for _ in range(N)]
MINW = WV[0][0]
W0, W1, W2, W3 = [], [], [], []
for w, v in WV:
    if w == MINW:
        W0.append(v)
    elif w == MINW + 1:
        W1.append(v)
    elif w == MINW + 2:
        W2.append(v)
    elif w == MINW + 3:
        W3.append(v)
W0.sort(reverse=True)
W1.sort(reverse=True)
W2.sort(reverse=True)
W3.sort(reverse=True)
W0 = [0] + W0
W1 = [0] + W1
W2 = [0] + W2
W3 = [0] + W3
from itertools import accumulate

W0 = list(accumulate(W0))
W1 = list(accumulate(W1))
W2 = list(accumulate(W2))
W3 = list(accumulate(W3))
ans = 0
for cnt0 in range(len(W0)):
    for cnt1 in range(len(W1)):
        for cnt2 in range(len(W2)):
            for cnt3 in range(len(W3)):
                SUMW = cnt0 * MINW
                SUMW += cnt1 * (MINW + 1)
                SUMW += cnt2 * (MINW + 2)
                SUMW += cnt3 * (MINW + 3)
                if SUMW <= W:
                    SUMV = W0[cnt0]
                    SUMV += W1[cnt1]
                    SUMV += W2[cnt2]
                    SUMV += W3[cnt3]
                    ans = max(ans, SUMV)
print(ans)

-import sys - -input = sys.stdin.readline -from itertools import accumulate - -# それぞれの重さについての価値のリスト -W0 = sorted([0] + [v for w, v in WV if w == MINW]) -W1 = sorted([0] + [v for w, v in WV if w == MINW + 1]) -W2 = sorted([0] + [v for w, v in WV if w == MINW + 2]) -W3 = sorted([0] + [v for w, v in WV if w == MINW + 3]) -# 累積和 -W0 = tuple(accumulate(W0)) -W1 = tuple(accumulate(W1)) -W2 = tuple(accumulate(W2)) -W3 = tuple(accumulate(W3)) +W0, W1, W2, W3 = [], [], [], [] +for w, v in WV: +    if w == MINW: +        W0.append(v) +    elif w == MINW + 1: +        W1.append(v) +    elif w == MINW + 2: +        W2.append(v) +    elif w == MINW + 3: +        W3.append(v) +W0.sort(reverse=True) +W1.sort(reverse=True) +W2.sort(reverse=True) +W3.sort(reverse=True) +W0 = [0] + W0 +W1 = [0] + W1 +W2 = [0] + W2 +W3 = [0] + W3 +from itertools import accumulate + +W0 = list(accumulate(W0)) +W1 = list(accumulate(W1)) +W2 = list(accumulate(W2)) +W3 = list(accumulate(W3)) -for w0 in range(len(W0)): -    for w1 in range(len(W1)): -        for w2 in range(len(W2)): -            for w3 in range(len(W3)): -                # tmp: 状態(w0,w1,w2,w3)での重さ -                tmp = (len(W0) - (w0 + 1)) * MINW -                tmp += (len(W1) - (w1 + 1)) * (MINW + 1) -                tmp += (len(W2) - (w2 + 1)) * (MINW + 2) -                tmp += (len(W3) - (w3 + 1)) * (MINW + 3) -                # ナップサックに入る場合 -                if tmp <= W: -                    # tmp: 価値の総和 -                    tmp = W0[-1] - W0[w0] -                    tmp += W1[-1] - W1[w1] -                    tmp += W2[-1] - W2[w2] -                    tmp += W3[-1] - W3[w3] -                    ans = max(ans, tmp) +for cnt0 in range(len(W0)): +    for cnt1 in range(len(W1)): +        for cnt2 in range(len(W2)): +            for cnt3 in range(len(W3)): +                SUMW = cnt0 * MINW +                SUMW += cnt1 * (MINW + 1) +                SUMW += cnt2 * (MINW + 2) +                SUMW += cnt3 * (MINW + 3) +                if SUMW <= W: +                    SUMV = W0[cnt0] +                    SUMV += W1[cnt1] +                    SUMV += W2[cnt2] +                    SUMV += W3[cnt3] +                    ans = max(ans, SUMV)