Problem p03215 (python)

Fixed submission:
N,K=map(int,input().split())
l=list(map(int,input().split()))
L=[0]
for i in range(N):
    L.append(L[-1]+l[i])
#print(L)
A=[]
for i in range(N):
    for j in range(i+1,N+1):
        A.append(L[j]-L[i])
#print(A)
B=[]
for i in range(len(A)):
    B.append(format(A[i],'040b'))
#print(B)


for i in range(40):
    cnt=0
    for j in B:
        if j[i]=="1":
            cnt+=1
    if cnt>=K:
        S=i
        break

ans=2**(39-S)
C=[]
for j in B:
    if j[S]=="1":
        C.append(j)
        
#print(ans)
#print(C)

for i in range(S+1,40):
    D=[]
    for j in C:
        if j[i]=="1":
            D.append(j)
    if len(D)>=K:
        ans+=2**(39-i)
        C=D
print(ans)

Buggy submission (verdict: Runtime Error):

N,K=map(int,input().split())
a=list(map(int,input().split()))
L=[]
for i in range(N):
    for j in range(i+1,N+1):
        L.append(sum(a[i:j]))

L.sort(reverse=True)
M=[]
#print(L)

for j in range(31):
    if 2**(31-j)<=L[0]:
        if 2**(31-j)<=L[K-1]:
            for l in range(N):
                if L[l]>=2**(31-j):
                    M.append(L[l])
            break
        else:
            for k in range(N):
                L[k]=L[k]%(2**(31-j))
            L.sort(reverse=True)
            
ans=0
#print(M)
#print(31-j)
for a in range(31-j):
    if len(M)<K:
        break
    M.sort(reverse=True)
    if M[K-1]>=2**(31-j-a):
        ans+=2**(31-j-a)
        for b in range(len(M)):
            if M[b]>=2**(31-j-a):
                M[b]%=2**(31-j-a)
            else:
                del M[b]
    
        
print(ans)

Buggy submission (verdict: Runtime Error):
N,K=map(int,input().split())
a=list(map(int,input().split()))
L=[]
for i in range(N):
    for j in range(i+1,N+1):
        L.append(sum(a[i:j]))

L.sort(reverse=True)
M=[]
#print(L)

for j in range(31):
    if 2**(31-j)<=L[0]:
        if 2**(31-j)<=L[K-1]:
            for l in range(N):
                if L[l]>=2**(31-j):
                    M.append(L[l])
            break
        else:
            for k in range(N):
                L[k]=L[k]%(2**(31-j))
            L.sort(reverse=True)
            
ans=0
#print(M)
#print(31-j)
for a in range(31-j):
    M.sort(reverse=True)
    if M[K-1]>=2**(31-j-a):
        ans+=2**(31-j-a)
        for b in range(len(M)):
            if M[b]>=2**(31-j-a):
                M[b]%=2**(31-j-a)
            else:
                del M[b]
    
        
print(ans)

Buggy submission (verdict: Runtime Error):
N,K=map(int,input().split())
a=list(map(int,input().split()))
L=[]
for i in range(N):
    for j in range(i+1,N+1):
        L.append(sum(a[i:j]))

L.sort(reverse=True)
M=[]
#print(L)

for j in range(31):
    if 2**(31-j)<=L[0]:
        if 2**(31-j)<=L[K-1]:
            for l in range(N):
                if L[l]>=2**(31-j):
                    M.append(L[l])
            break
        else:
            for k in range(N):
                L[k]=L[k]%(2**(31-j))
            L.sort(reverse=True)
            
ans=2**(31-j)
#print(M)

for a in range(1,31-j):
    for b in range(len(M)):
        M[b]%=(31-j-a)
    M.sort(reverse=True)
    if M[K-1]>=2**(31-j-a):
        ans+=2**(31-j-a)
        for c in range(len(M)):
            if M[c]<2**(31-j-a):
                del M[c]
print(ans)