Buggy python submission (verdict: Time Limit Exceeded):

N,K = map(int,input().split())
K += 1

AB = []

for i in range(N):

    a,b = map(int,input().split())
    AB.append([a,b])

kb = format(K,'b')
#print (kb)
ans = 0

for i in range(len(kb)):

    now = 0
    if kb[-1-i] == "1":

        for j in range(N):

            flag = True
            at = format(AB[j][0],'b')

            if len(kb) < len(at):
                continue

            for m in range(len(at)):

                

                if m == i and at[-1-m] == "1":
                    flag = False
                    break

                elif m > i and kb[-1-m] == "0" and at[-1-m] == "1":
                    flag = False
                    break

            if flag:
                now += AB[j][1]

        #print (i,now)
        ans = max(ans,now)
        
print (ans)


Fixed submission:
N,K = map(int,input().split())
K += 1

AB = []

for i in range(N):

    a,b = map(int,input().split())
    AB.append([a,b,format(a,'b')])

kb = format(K,'b')
#print (kb)
ans = 0

for i in range(len(kb)):

    now = 0
    if kb[-1-i] == "1":

        for j in range(N):

            flag = True
            at = AB[j][2]

            if len(kb) < len(at):
                continue

            for m in range(len(at)):

                

                if m == i and at[-1-m] == "1":
                    flag = False
                    break

                elif m > i and kb[-1-m] == "0" and at[-1-m] == "1":
                    flag = False
                    break

            if flag:
                now += AB[j][1]

        #print (i,now)
        ans = max(ans,now)
        
print (ans)

Buggy python submission (verdict: Time Limit Exceeded):

N,K = map(int,input().split())
K += 1

AB = []

for i in range(N):

    a,b = map(int,input().split())
    AB.append([a,b])

kb = format(K,'b')
#print (kb)
ans = 0

for i in range(len(kb)):

    now = 0
    if kb[-1-i] == "1":

        for j in range(N):

            flag = True
            at = format(AB[j][0],'b')

            if len(kb) < len(at):
                continue

            for m in range(len(at)):

                

                if m == i and at[-1-m] == "1":
                    flag = False
                    break

                elif m > i and kb[-1-m] == "0" and at[-1-m] == "1":
                    flag = False
                    break

            if flag:
                now += AB[j][1]

        #print (i,now)
        ans = max(ans,now)
        
print (ans)


Fixed submission:
N,K = map(int,input().split())
K += 1

AB = []

for i in range(N):

    a,b = map(int,input().split())
    AB.append([a,b,format(a,'b')])

kb = format(K,'b')
#print (kb)
ans = 0

for i in range(len(kb)):

    now = 0
    if kb[-1-i] == "1":

        for j in range(N):

            flag = True
            at = AB[j][2]

            if len(kb) < len(at):
                continue

            for m in range(len(at)):

                

                if m == i and at[-1-m] == "1":
                    flag = False
                    break

                elif m > i and kb[-1-m] == "0" and at[-1-m] == "1":
                    flag = False
                    break

            if flag:
                now += AB[j][1]

        #print (i,now)
        ans = max(ans,now)
        
print (ans)