import sys



inf = 1 << 32



def solve():

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

    ws = []

    vs = []



    for i in range(N):

        wi, vi = list(map(int, input().split()))

        ws.append(wi)

        vs.append(vi)



    dp = [[[-inf]*(3*N + 1) for j in range(N + 1)]

                              for i in range(N + 1)]



    dp[0][0][0] = 0



    for i in range(N):

        for j in range(N + 1):

            for k in range(3*N + 1):

                dp[i + 1][j][k] = dp[i][j][k]



                if j - 1 >= 0 and k - (ws[i] - ws[0]) >= 0:

                    dp[i + 1][j][k] = max(dp[i + 1][j][k], dp[i][j - 1][k - (ws[i] - ws[0])] + vs[i])



    ans = 0



    for j in range(N + 1):

        for k in range(3*N + 1):

            if ws[0]*j + k <= W:

                ans = max(ans, dp[N][j][k])



    print(ans)



if __name__ == '__main__':

    solve()
import sys



def solve():

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

    v = [[] for i in range(4)]



    for i in range(N):

        wi, vi = list(map(int, input().split()))



        if i == 0:

            base = wi

            v[0].append(vi)

        else:

            v[wi - base].append(vi)



    for i in range(4):

        v[i].sort(reverse=True)



    ans = 0



    for i in range(len(v[0]) + 1):

        for j in range(len(v[1]) + 1):

            for k in range(len(v[2]) + 1):

                w = base*i + (base + 1)*j + (base + 2)*k



                if w > W:

                    continue



                l = (W - w) // (base + 3)



                kakaku = sum(v[0][:i]) + sum(v[1][:j]) + sum(v[2][:k]) + sum(v[3][:l])



                ans = max(ans, kakaku)



    print(ans)





if __name__ == '__main__':

    solve()
import sys

inf = 1 << 32


def solve():
    N, W = list(map(int, input().split()))
    ws = []
    vs = []
    for i in range(N):
        wi, vi = list(map(int, input().split()))
        ws.append(wi)
        vs.append(vi)
    dp = [[[-inf] * (3 * N + 1) for j in range(N + 1)] for i in range(N + 1)]
    dp[0][0][0] = 0
    for i in range(N):
        for j in range(N + 1):
            for k in range(3 * N + 1):
                dp[i + 1][j][k] = dp[i][j][k]
                if j - 1 >= 0 and k - (ws[i] - ws[0]) >= 0:
                    dp[i + 1][j][k] = max(
                        dp[i + 1][j][k], dp[i][j - 1][k - (ws[i] - ws[0])] + vs[i]
                    )
    ans = 0
    for j in range(N + 1):
        for k in range(3 * N + 1):
            if ws[0] * j + k <= W:
                ans = max(ans, dp[N][j][k])
    print(ans)


if __name__ == "__main__":
    solve()

import sys


def solve():
    N, W = list(map(int, input().split()))
    v = [[] for i in range(4)]
    for i in range(N):
        wi, vi = list(map(int, input().split()))
        if i == 0:
            base = wi
            v[0].append(vi)
        else:
            v[wi - base].append(vi)
    for i in range(4):
        v[i].sort(reverse=True)
    ans = 0
    for i in range(len(v[0]) + 1):
        for j in range(len(v[1]) + 1):
            for k in range(len(v[2]) + 1):
                w = base * i + (base + 1) * j + (base + 2) * k
                if w > W:
                    continue
                l = (W - w) // (base + 3)
                kakaku = sum(v[0][:i]) + sum(v[1][:j]) + sum(v[2][:k]) + sum(v[3][:l])
                ans = max(ans, kakaku)
    print(ans)


if __name__ == "__main__":
    solve()

- -inf = 1 << 32 -    ws = [] -    vs = [] +    v = [[] for i in range(4)] -        ws.append(wi) -        vs.append(vi) -    dp = [[[-inf] * (3 * N + 1) for j in range(N + 1)] for i in range(N + 1)] -    dp[0][0][0] = 0 -    for i in range(N): -        for j in range(N + 1): -            for k in range(3 * N + 1): -                dp[i + 1][j][k] = dp[i][j][k] -                if j - 1 >= 0 and k - (ws[i] - ws[0]) >= 0: -                    dp[i + 1][j][k] = max( -                        dp[i + 1][j][k], dp[i][j - 1][k - (ws[i] - ws[0])] + vs[i] -                    ) +        if i == 0: +            base = wi +            v[0].append(vi) +        else: +            v[wi - base].append(vi) +    for i in range(4): +        v[i].sort(reverse=True) -    for j in range(N + 1): -        for k in range(3 * N + 1): -            if ws[0] * j + k <= W: -                ans = max(ans, dp[N][j][k]) +    for i in range(len(v[0]) + 1): +        for j in range(len(v[1]) + 1): +            for k in range(len(v[2]) + 1): +                w = base * i + (base + 1) * j + (base + 2) * k +                if w > W: +                    continue +                l = (W - w) // (base + 3) +                kakaku = sum(v[0][:i]) + sum(v[1][:j]) + sum(v[2][:k]) + sum(v[3][:l]) +                ans = max(ans, kakaku)