Problem p03215 (python)

Fixed submission:
N,K=map(int,input().split())
a=[int(i) for i in input().split()]
S=[0 for i in range(N+1)]
for i in range(1,N+1):
    S[i]=S[i-1]+a[i-1]
List=[]
for i in range(N+1):
    for j in range(i):
        List.append(S[i]-S[j])
List.sort(reverse=True)
def program(Y,L):
    #print(L)
    M=len(L)
    t=[bin(i)[2:][::-1] for i in L]
    b=[i+"0"*(Y-len(i)) for i in t]
    X=[[] for i in range(Y)]
    for i in b:
        for j in range(Y):
            if i[j]=="1":
                X[j].append(i)
    #print("X",X)
    l=Y
    for i in range(Y-1,-1,-1):
        if len(X[i])>=K:
            l=i
            break
    if l==Y:
        return 0
    #print(X[l])
    R=[]
    for i in X[l]:
        if i[:l]=="":
            R.append(0)
            continue
        R.append(int(i[:l][::-1],2))
    R.sort(reverse=True)
    return program(l,R)+2**l
#print(K,N)
print(program(42,List))

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10**7)
n,k=map(int,input().split())
a=[int(i) for i in input().split()]
S=[0 for i in range(n+1)]
for i in range(1,n+1):
    S[i]=S[i-1]+a[i-1]
List=[]
for i in range(n+1):
    for j in range(i):
        List.append(S[i]-S[j])
List.sort(reverse=True)
def program(N,K,L):
    #print(N,K,L)
    M=len(L)
    b=[bin(i)[2:][::-1] for i in L]
    X=[[] for i in range(35)]
    for i in b:
        for j in range(len(i)):
            if i[j]=="1":
                X[j].append(i)
    l=35
    for i in range(34,-1,-1):
        if len(X[i])>=K:
            l=i
            break
    if l==35:
        return 0
    #print(X[l])
    R=[]
    for i in X[l]:
        if i[:-1]=="":
            R.append(0)
            continue
        R.append(int(i[:-1][::-1],2))
    R.sort(reverse=True)
    return program(len(R),K,R)+2**l
print(program(n,k,List))

Buggy submission (verdict: Runtime Error):
import sys
sys.setrecursionlimit(10**7)
n,k=map(int,input().split())
a=[int(i) for i in input().split()]
S=[0 for i in range(n+1)]
for i in range(1,n+1):
    S[i]=S[i-1]+a[i-1]
List=[]
for i in range(n+1):
    for j in range(i):
        List.append(S[i]-S[j])
List.sort(reverse=True)
def program(N,K,L):
    #print(N,K,L)
    M=len(L)
    b=[bin(i)[2:][::-1] for i in L]
    X=[[] for i in range(35)]
    for i in b:
        for j in range(len(i)):
            if i[j]=="1":
                X[j].append(i)
    l=35
    for i in range(34,-1,-1):
        if len(X[i])>=K:
            l=i
            break
    if l==35:
        return 0
    #print(X[l])
    R=[]
    for i in X[l]:
        if i[:-1]=="":
            R.append(0)
            continue
        R.append(int(i[:-1][::-1],2))
    return program(len(R),K,R)+2**l
print(program(n,k,List))

Buggy submission (verdict: Runtime Error):
n,k=map(int,input().split())
a=[int(i) for i in input().split()]
S=[0 for i in range(n+1)]
for i in range(1,n+1):
    S[i]=S[i-1]+a[i-1]
List=[]
for i in range(n+1):
    for j in range(i):
        List.append(S[i]-S[j])
List.sort(reverse=True)
def program(N,K,L):
    #print(N,K,L)
    M=len(L)
    b=[bin(i)[2:][::-1] for i in L]
    X=[[] for i in range(35)]
    for i in b:
        for j in range(len(i)):
            if i[j]=="1":
                X[j].append(i)
    l=35
    for i in range(34,-1,-1):
        if len(X[i])>=K:
            l=i
            break
    if l==35:
        return 0
    #print(X[l])
    R=[]
    for i in X[l]:
        if i[:-1]=="":
            R.append(0)
            continue
        R.append(int("0b"+i[:-1][::-1],2))
    return program(len(R),K,R)+2**l
print(program(n,k,List))

Buggy submission (verdict: Runtime Error):
N,K=map(int,input().split())
a=[int(i) for i in input().split()]
S=[0 for i in range(N+1)]
for i in range(1,N+1):
    S[i]=S[i-1]+a[i-1]
L=[]
for i in range(N+1):
    for j in range(i):
        L.append(S[i]-S[j])
X=[0 for i in range(35)]
for i in L:
    X[(len(bin(i))-2)]+=1
#print(X)
j=35
for i in range(34,-1,-1):
    if X[i]>=K:
        j=i
        break
R=[]
for i in L:
    if len(bin(i))==j+2:
        R.append(i)
R.sort(reverse=True)
ans=R[0]
#print(sorted(L,reverse=True))
#print(R,j)
for i in range(1,K):
    ans=ans&R[i]
print(ans)