Buggy python submission (verdict: Runtime Error):
from collections import defaultdict
from copy import deepcopy
from math import log
N,K=map(int,input().split())
AB=[0]*N
dic=defaultdict(int)
#n=int(ceil(log(K,2)))+1
for i in range(N):
    a,b=map(int,input().split())
    if a in dic.keys():
        AB[dic[a]]=[a,AB[dic[a]][1]+b]
    else:
        dic[a]=i
        AB[i]=[a,b]
ans=sum([b for a,b in AB if K|a==K])
print(bin(K))
for i in range(int.bit_length(K)-1,0,-1):
    if not K &(1<<i):
        continue
    m=K & ~(1<<i)|(1<<i)-1
    print(bin(m))
    s=sum([b for a,b in AB if m | a ==m])
    ans=max(s,ans)
print(ans)


Fixed submission:
N,K=map(int,input().split())
AB=[]
for i in range(N):
    a,b=map(int,input().split())
    AB.append([a,b])
ans=sum([b for a,b in AB if K|a==K])

for i in range(int.bit_length(K)-1,0,-1):
    if not K &(1<<i):
        continue
    m=K & ~(1<<i)|(1<<i)-1
    s=sum([b for a,b in AB if m | a ==m])
    ans=max(s,ans)
print(ans)

Buggy python submission (verdict: Runtime Error):
from math import ceil
from itertools import permutations
from heapq import *
from collections import defaultdict
from copy import deepcopy
from math import log
N,K=map(int,input().split())
AB=[0]*N
dic=defaultdict(int)
n=int(ceil(log(K,2)))+1
for i in range(N):
    a,b=map(int,input().split())
    if a in dic.keys():
        AB[dic[a]]=[a,AB[dic[a]][1]+b]
    else:
        dic[a]=i
        AB[i]=[a,b]
        n=max(n,int(ceil(log(a,2)))+1)
AB_=[]
for i in AB:
    if i==0:
        continue
    k=bin(i[0])[2:].zfill(n)
    AB_.append([k,i[1]])
AB=AB_
k=bin(K)[2:].zfill(n)
l=len(AB)
Klist=[k]
for i in range(n):
    if k[i]=="1":
        if i+1>=len(k)-1:
            Klist.append(k[:i]+"0")
            continue
        Klist.append(k[:i]+"0"+"1"*len(k[i+1:]))
#print(Klist)
ans=0
for i in Klist:
    sum=0
    for j,h in AB:
        if int(i,2)|int(j,2)<=int(i,2):
            #print(i,j,int(i,2)|int(j,2),int(i,2))
            sum+=h
    ans=max(sum,ans)
print(ans)


Fixed submission:
N,K=map(int,input().split())
AB=[]
for i in range(N):
    a,b=map(int,input().split())
    AB.append([a,b])
ans=sum([b for a,b in AB if K|a==K])

for i in range(int.bit_length(K)-1,0,-1):
    if not K &(1<<i):
        continue
    m=K & ~(1<<i)|(1<<i)-1
    s=sum([b for a,b in AB if m | a ==m])
    ans=max(s,ans)
print(ans)

Buggy python submission (verdict: Runtime Error):
from math import ceil

from collections import defaultdict
from math import log
N,K=map(int,input().split())
AB=[0]*N
dic=defaultdict(int)
n=int(ceil(log(K,2)))+1
for i in range(N):
    a,b=map(int,input().split())
    if a in dic.keys():
        AB[dic[a]]=[a,AB[dic[a]][1]+b]
    else:
        dic[a]=i
        AB[i]=[a,b]
        n=max(n,int(ceil(log(a,2)))+1)
AB_=[]
for i in AB:
    if i==0:
        continue
    k=bin(i[0])[2:].zfill(n)
    AB_.append([k,i[1]])
AB=AB_
k=bin(K)[2:].zfill(n)
l=len(AB)
Klist=[k]
for i in range(n):
    if k[i]=="1":
        Klist.append(k[:i]+"0"+"1"*len(k[i+1:]))
#print(Klist)
ans=0
for i in Klist:
    sum=0
    for j,h in AB:
        if int(i,2)|int(j,2)<=int(i,2):
            #print(i,j,int(i,2)|int(j,2),int(i,2))
            sum+=h
    ans=max(sum,ans)
print(ans)


Fixed submission:
N,K=map(int,input().split())
AB=[]
for i in range(N):
    a,b=map(int,input().split())
    AB.append([a,b])
ans=sum([b for a,b in AB if K|a==K])

for i in range(int.bit_length(K)-1,0,-1):
    if not K &(1<<i):
        continue
    m=K & ~(1<<i)|(1<<i)-1
    s=sum([b for a,b in AB if m | a ==m])
    ans=max(s,ans)
print(ans)

Buggy python submission (verdict: Runtime Error):
from math import sqrt
from math import ceil
from itertools import permutations
from heapq import *
from collections import defaultdict
from copy import deepcopy
from math import log
N,K=map(int,input().split())
AB=[0]*N
dic=defaultdict(int)
n=int(ceil(log(K,2)))+1
for i in range(N):
    a,b=map(int,input().split())
    if a in dic.keys():
        AB[dic[a]]=[a,AB[dic[a]][1]+b]
    else:
        dic[a]=i
        AB[i]=[a,b]
        n=max(n,int(ceil(log(a,2)))+1)
AB_=[]
for i in AB:
    if i==0:
        continue
    k=bin(i[0])[2:].zfill(n)
    AB_.append([k,i[1]])
AB=AB_
k=bin(K)[2:].zfill(n)
l=len(AB)
dp=[[0]*(n+1) for i in range(l)]
for i in range(l):
    for j in range(n):
        #print(i,j,dp[i][j]<<1,"j:",k[j],"k:",AB[i][0][j])
        dp[i][j+1]=(dp[i][j]<<1)+int(k[j])|int(AB[i][0][j])
        #print((dp[i][j]<<1)+int(k[j])|int(AB[i][0][j]))
ans=0
for i in range(n):
    if k[i]=="1":
        sum=0
        for j in range(l):
            if AB[j][0][i]=="0" and dp[j][i]<=int(k[:i],2):
                sum+=AB[j][1]
        ans=max(ans,sum)
sum=0
t=k
for j in range(l):
    if int(t)|int(AB[j][0])<=int(t):
        sum+=AB[j][1]
ans=max(ans,sum)
print(ans)


Fixed submission:
N,K=map(int,input().split())
AB=[]
for i in range(N):
    a,b=map(int,input().split())
    AB.append([a,b])
ans=sum([b for a,b in AB if K|a==K])

for i in range(int.bit_length(K)-1,0,-1):
    if not K &(1<<i):
        continue
    m=K & ~(1<<i)|(1<<i)-1
    s=sum([b for a,b in AB if m | a ==m])
    ans=max(s,ans)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict
from copy import deepcopy

N,K=map(int,input().split())
AB=[0]*N
dic=defaultdict(int)
for i in range(N):
    a,b=map(int,input().split())
    if a in dic.keys():
        AB[dic[a]]=[a,AB[dic[a]][1]+b]
    else:
        dic[a]=i
        AB[i]=[a,b]
AB_=[]
for i in AB:
    if i==0:
        continue
    k=bin(i[0])[2:].zfill(30)
    AB_.append([k,i[1]])
AB=AB_
l=len(AB)

k=bin(K)[2:].zfill(30)
ans=0
for i in range(30):
    if k[i]=="1":
        t=k[:i]+"0"
        sum=0
        for j in range(l):
            if int(t)|int(AB[j][0][:i+1])<=int(t):
                sum+=AB[j][1]
        ans=max(ans,sum)
sum=0
t=k
for j in range(l):
    if int(t)|int(AB[j][0])<=int(t):
        sum+=AB[j][1]
ans=max(ans,sum)
print(ans)



Fixed submission:
N,K=map(int,input().split())
AB=[]
for i in range(N):
    a,b=map(int,input().split())
    AB.append([a,b])
ans=sum([b for a,b in AB if K|a==K])

for i in range(int.bit_length(K)-1,0,-1):
    if not K &(1<<i):
        continue
    m=K & ~(1<<i)|(1<<i)-1
    s=sum([b for a,b in AB if m | a ==m])
    ans=max(s,ans)
print(ans)

Buggy python submission (verdict: Runtime Error):
N,K=map(int,input().split())
AB=[0]*N
dic=defaultdict(int)
for i in range(N):
    a,b=map(int,input().split())
    if a in dic.keys():
        AB[dic[a]]=[a,AB[dic[a]][1]+b]
    else:
        dic[a]=i
        AB[i]=[a,b]
AB_=[]
for i in AB:
    if i==0:
        continue
    k=bin(i[0])[2:].zfill(4)
    AB_.append([k,i[1]])
AB=AB_
l=len(AB)

k=bin(K)[2:].zfill(4)
ans=0
for i in range(4):
    if k[i]=="1":
        t=k[:i]+"0"
        sum=0
        for j in range(l):
            if int(t)|int(AB[j][0][:i+1])<=int(t):
                sum+=AB[j][1]
        ans=max(ans,sum)
sum=0
t=k
for j in range(l):
    if int(t)|int(AB[j][0])<=int(t):
        sum+=AB[j][1]
ans=max(ans,sum)
print(ans)


Fixed submission:
N,K=map(int,input().split())
AB=[]
for i in range(N):
    a,b=map(int,input().split())
    AB.append([a,b])
ans=sum([b for a,b in AB if K|a==K])

for i in range(int.bit_length(K)-1,0,-1):
    if not K &(1<<i):
        continue
    m=K & ~(1<<i)|(1<<i)-1
    s=sum([b for a,b in AB if m | a ==m])
    ans=max(s,ans)
print(ans)