Problem p03584 (python)

Fixed submission:
import sys
def MI(): return map(int,sys.stdin.readline().rstrip().split())


N,K = MI()
K += 1
k = K.bit_length()

AB = []

for _ in range(N):
    a,b = MI()
    if a >= K:
        continue
    else:
        AB.append(([(a >> i) & 1 for i in range(k-1,-1,-1)],b))

N = len(AB)
K_bit = [(K >> i) & 1 for i in range(k-1,-1,-1)]

ANS = [0]*k  # 

for i in range(N):
    a,b = AB[i]
    for j in range(k):
        if K_bit[j] == 1:
            if a[j] == 0:
                ANS[j] += b
        else:
            if a[j] == 1:
                break

print(max(ANS))

Buggy submission (verdict: Runtime Error):
import sys
def MI(): return map(int,sys.stdin.readline().rstrip().split())

N,K = MI()
k = K.bit_length()
AB = []
for _ in range(N):
    a,b = MI()
    if a > K:
        continue
    else:
        AB.append(([(a >> i) & 1 for i in range(k-1,-1,-1)],b))

N = len(AB)
K = [(K >> i) & 1 for i in range(k-1,-1,-1)]

ANS = [0]*(k+1)  # A[i] = 上i桁の各桁がKの上i桁の各桁以下であり、i+1桁目が0である非負整数の価値の総和

for i in range(N):
    a,b = AB[i]
    for j in range(k+1):
        if j == 0:
            if a[j] == 0:
                ANS[j] += b
        elif 1 <= j <= k-1:
            if a[j-1] <= K[j-1]:
                if a[j] == 0:
                    ANS[j] += b
            else:
                break
        else:
            if a[j-1] <= K[j-1]:
                ANS[j] += b

print(max(ANS))

Buggy submission (verdict: Runtime Error):
import sys
def MI(): return map(int,sys.stdin.readline().rstrip().split())

N,K = MI()
k = K.bit_length()
AB = []
for _ in range(N):
    a,b = MI()
    if a > K:
        continue
    else:
        AB.append(([(a >> i) & 1 for i in range(k-1,-1,-1)],b))

N = len(AB)
K = [(K >> i) & 1 for i in range(k-1,-1,-1)]

A = [[] for _ in range(k+1)]
B = [0]*(k+1)
for i in range(N):
    a,b = AB[i]
    for j in range(k):
        if a[j] == K[j] == 1:
            B[j+1] -= b
        if a[j] > K[j]:
            A[j].append(b)
            break
    else:
        A[k].append(b)


for i in range(k+1):
    A[i] = sum(A[i])

for i in range(k-1,-1,-1):
    A[i] += A[i+1]

print(max(A[i]+B[i] for i in range(1,k+1)))

Buggy submission (verdict: Runtime Error):
import sys

sys.setrecursionlimit(10**7)
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int,sys.stdin.readline().rstrip().split())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))  #空白あり
def LI2(): return list(map(int,sys.stdin.readline().rstrip()))  #空白なし
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())  #空白あり
def LS2(): return list(sys.stdin.readline().rstrip())  #空白なし

N,K = MI()
k = K.bit_length()
AB = []
for _ in range(N):
    a,b = MI()
    if a > K:
        continue
    else:
        AB.append(([(a >> i) & 1 for i in range(k-1,-1,-1)],b))

N = len(AB)
K = [(K >> i) & 1 for i in range(k-1,-1,-1)]

A = [[] for _ in range(k+1)]
B = [0]*(k+1)
for i in range(N):
    a,b = AB[i]
    for j in range(k):
        if a[j] == 1:
            B[j+1] -= b
        if a[j] > K[j]:
            A[j].append(b)
            break
    else:
        A[k].append(b)


for i in range(k+1):
    A[i] = sum(A[i])

for i in range(k-1,-1,-1):
    A[i] += A[i+1]

c = 0
for i in range(N):
    a,b = AB[i]
    if a[0] == 0:
        c += b

print(max(c,max(A[i]+B[i] for i in range(1,k+1))))

Buggy submission (verdict: Runtime Error):
import sys

sys.setrecursionlimit(10**7)
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int,sys.stdin.readline().rstrip().split())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))  #空白あり
def LI2(): return list(map(int,sys.stdin.readline().rstrip()))  #空白なし
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())  #空白あり
def LS2(): return list(sys.stdin.readline().rstrip())  #空白なし

N,K = MI()
k = K.bit_length()
AB = []
for _ in range(N):
    a,b = MI()
    if a > K:
        continue
    else:
        AB.append(([(a >> i) & 1 for i in range(k-1,-1,-1)],b))

N = len(AB)
K = [(K >> i) & 1 for i in range(k-1,-1,-1)]

A = [[] for _ in range(k+1)]
B = [0]*(k+1)
for i in range(N):
    a,b = AB[i]
    for j in range(k):
        if a[j] == 1:
            B[j+1] -= b
        if a[j] > K[j]:
            A[j].append(b)
            break
    else:
        A[k].append(b)


for i in range(k+1):
    A[i] = sum(A[i])

for i in range(k-1,-1,-1):
    A[i] += A[i+1]

print(max(A[i]+B[i] for i in range(1,k+1)))

Buggy submission (verdict: Runtime Error):
import sys

sys.setrecursionlimit(10**7)
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int,sys.stdin.readline().rstrip().split())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))  #空白あり
def LI2(): return list(map(int,sys.stdin.readline().rstrip()))  #空白なし
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())  #空白あり
def LS2(): return list(sys.stdin.readline().rstrip())  #空白なし

N,K = MI()
k = K.bit_length()
AB = []
for _ in range(N):
    a,b = MI()
    if a > K:
        continue
    else:
        AB.append(([(a >> i) & 1 for i in range(k-1,-1,-1)],b))

N = len(AB)
K = [(K >> i) & 1 for i in range(k-1,-1,-1)]

A = [[] for _ in range(k+1)]
B = [0]*(k+1)
for i in range(N):
    a,b = AB[i]
    for j in range(k):
        if a[j] == 1 and j >= 1:
            B[j] -= b
        if a[j] > K[j]:
            A[j].append(b)
            break
    else:
        A[k].append(b)


for i in range(k+1):
    A[i] = sum(A[i])

for i in range(k-1,-1,-1):
    A[i] += A[i+1]

print(max(A[i]+B[i] for i in range(1,k+1)))