Problem p03138 (python)

Fixed submission:
#!usr/bin/env python3
from collections import defaultdict
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS():return list(map(list, sys.stdin.readline().split()))
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = I()
    return l
def LIR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LI()
    return l
def SR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = S()
    return l
def LSR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = SR()
    return l
mod = 1000000007

#A
"""
t,x = LI()
print(t/x)
"""

#B
"""
n= II()
a = LI()
m = max(a)
if m >= sum(a)-m:
    print("No")
else:
    print("Yes")
"""

#C
"""
n,m = LI()
x = LI()
x.sort()
l = [x[i+1]-x[i] for i in range(m-1)]
l.sort()
l = l[::-1]
ans = sum(l[max(n-1,0):])
print(ans)
"""

#D
n,k = LI()
a = LI()
k = list(map(int,list(bin(k)[2:])))
l = len(k)
for i in range(n):
    a[i] = list(map(int,list(bin(a[i])[2:])))
    l = max(l,len(a[i]))
for i in range(n):
    for j in range(l-len(a[i])):
        a[i].insert(0,0)
for j in range(l-len(k)):
    k.insert(0,0)
s = [[0,0] for i in range(l)]
pow2 = [1 for i in range(l)]
for i in range(l-1)[::-1]:
    pow2[i] = pow2[i+1]*2
for i in a:
    for j in range(l):
        s[j][0] += pow2[j]*i[j]
        s[j][1] += pow2[j]*(not i[j])
dp = [0 for i in range(l+1)]
small = False
for i in range(l):
    if not small:
        if not k[i]:
            dp[i+1] += dp[i]+s[i][0]
        else:
            if s[i][0] >= s[i][1]:
                dp[i+1] += dp[i]+s[i][0]
                small = True
            else:
                dp[i+1] += dp[i]+s[i][1]
    else:
        dp[i+1] += dp[i]+max(s[i])
print(dp[l])
#E

#F

#G

#H

#I

#J

#K

#L

#M

#N

#O

#P

#Q

#R

#S

#T

Buggy submission (verdict: Runtime Error):
#!usr/bin/env python3
from collections import defaultdict
import math
def LI(): return list(map(int, input().split()))
def II(): return int(input())
def LS(): return input().split()
def S(): return input()
def IIR(n): return [II() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
mod = 1000000007

#A
"""
t,x = LI()
print(t/x)
"""

#B
"""
n= II()
a = LI()
m = max(a)
if m >= sum(a)-m:
    print("No")
else:
    print("Yes")
"""

#C
"""
n,m = LI()
x = LI()
x.sort()
l = [x[i+1]-x[i] for i in range(m-1)]
l.sort()
l = l[::-1]
ans = sum(l[max(n-1,0):])
print(ans)
"""

#D
n,k = LI()
a = LI()
l = 0
b = [0 for i in range(n)]
k = list(map(int,list(bin(k)[2:])))
d = len(k)
for i in range(n):
    b[i] = list(bin(a[i]))[2:]
    l = max(l,len(b[i]))
s = [0 for i in range(d)]
for i in range(n):
    for j in range(d-len(b[i])):
        b[i].insert(0,"0")
for i in range(n):
    for j in range(d):
        s[j] += int(b[i][max(l-d,0)+j])
ans = 0
for i in range(d):
    p = pow(2,d-1)
    x = 0
    for j in range(i):
        x += p*s[j] if not k[j] else p*(n-s[j])
        p //= 2
    x += p*s[i]
    p //= 2
    for j in range(i+1,d):
        x += max(p*s[j], p*(n-s[j]))
        p //= 2
    ans = max(ans, x)
if l > d:
    p = pow(2,l-1)
    for i in range(l-d):
        for j in range(n):
            ans += p*int(b[j][i])
        p //= 2
print(ans)
#E

#F

#G

#H

#I

#J

#K

#L

#M

#N

#O

#P

#Q

#R

#S

#T