Problem:
 Score : 400 points    Problem Statement  One day, Niwango-kun, an employee of Dwango Co., Ltd., found an integer sequence (a_1, ..., a_N) of length N .
He is interested in properties of the sequence a .  For a nonempty contiguous subsequence a_l, ..., a_r  (1 \leq l \leq r \leq N) of the sequence a , its beauty is defined as a_l + ... + a_r . Niwango-kun wants to know the maximum possible value of the bitwise AND of the beauties of K nonempty contiguous subsequences among all N(N+1)/2 nonempty contiguous subsequences. (Subsequences may share elements.)  Find the maximum possible value for him.      Constraints   2 \leq N \leq 1000  1 \leq a_i \leq 10^9  1 \leq K \leq N(N+1)/2  All numbers given in input are integers         Input  Input is given from Standard Input in the following format:  N  K  a_1  a_2  ...  a_N       Output Print the answer.        Sample Input 1  4 2
2 5 2 5      Sample Output 1  12  There are 10 nonempty contiguous subsequences of a . Let us enumerate them:   contiguous subsequences starting from the first element: \{2\}, \{2, 5\}, \{2, 5, 2\}, \{2, 5, 2, 5\}  contiguous subsequences starting from the second element: \{5\}, \{5, 2\}, \{5, 2, 5\}  contiguous subsequences starting from the third element: \{2\}, \{2, 5\}  contiguous subsequences starting from the fourth element: \{5\}   (Note that even if the elements of subsequences are equal, subsequences that have different starting indices are considered to be different.)  The maximum possible bitwise AND of the beauties of two different contiguous subsequences is 12 .
This can be achieved by choosing \{5, 2, 5\} (with beauty 12 ) and \{2, 5, 2, 5\} (with beauty 14 ).       Sample Input 2  8 4
9 1 8 2 7 5 6 4      Sample Output 2  32

Solution (Python):
from itertools import accumulate
import numpy as np

n, k = map(int, input().split())
A = list(map(int, input().split()))
C = [0] + list(accumulate(A))
D = []
for i in range(n):
  for j in range(i+1, n+1):
    D.append(C[j] - C[i])
    
D = np.array(D)
def enough(x):
  return np.count_nonzero(x&D == x) >= k

x = 0
for i in range(39, -1, -1):
  temp = x + 2**i
  if enough(temp):
    x = temp
    
print(x)

Solution (Python):
from itertools import *
from collections import *
from bisect import *
N,K=map(int,input().split())
A=list(map(int,input().split()))

ruiA=[0 for i in range(N+1)]
for i in range(0,N):
    ruiA[i+1]=ruiA[i]+A[i]


nlst=[]

for i in range(len(ruiA)-1):
    for m in range(i+1,len(ruiA)):
        nlst.append(ruiA[m]-ruiA[i])
nlst.sort(reverse=True)

lenbin=60
for i in range(len(nlst)):
    lst=[]
    for m in range(lenbin):
        lst.append((nlst[i]>>m)&1)
    nlst[i]=list(reversed(lst))
lenbinst=0

use=set([i for i in range(len(nlst))])
while lenbinst<lenbin:
    idx=-1
    count=0
    dellst=set()
    for m in use:
        if nlst[m][lenbinst]==1:
            count+=1
    if count>=K:
        if len(nlst)!=count:
            for m in use:
                if nlst[m][lenbinst]!=1:
                    dellst.add(m)
    use=use-dellst
    lenbinst+=1
valuelst=[0 for i in range(lenbin)]
nlst=[nlst[i] for i in use]

for n in nlst:
    for i,a in enumerate(list(reversed(n))):
        if a:
            valuelst[i]+=1

for i in range(lenbin):
    if valuelst[i]>=K:
        newlst=[i for i in nlst if i[idx]==1]
        if len(newlst)>=K:
            nlst=newlst

valuelst=[0 for i in range(lenbin)]
for n in nlst:
    for i,a in enumerate(list(reversed(n))):
        if a:
            valuelst[i]+=1
value=0
for i in range(lenbin):
    if valuelst[i]>=K:
        value+=2**i
print(value)

Solution (Python):

import sys
from itertools import accumulate
import math

def solve(N: int, K: int, a: "List[int]"):
    accum = [0] + list(accumulate(a))
    sum_list = []
    for i in range(0,N):
        for j in range(i+1,N+1):
            sum_list.append(accum[j]-accum[i])
    sum_list.sort(reverse=True)
    max_bit = int(math.log2(sum_list[0]))
    answer = 0

    for i in range(max_bit,-1,-1):
        count = 0
        tmp = []
        for j in range(len(sum_list)):
            if sum_list[j] & 1<<i:
                count +=1
                tmp.append(sum_list[j])
            
        if count >= K:
            answer += 2**i
            sum_list = tmp
    print(answer)

    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    K = int(next(tokens))  
    a = [int(next(tokens)) for _ in range(N)]  
    solve(N, K, a)

if __name__ == '__main__':
    main()

Solution (Python):
import sys
import math
from collections import defaultdict

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N,num):
    if N<=0:
        return [[] for _ in range(num)]
    elif num==1:
        return [I() for _ in range(N)]
    else:
        read_all = [tuple(II()) for _ in range(N)]
        return map(list, zip(*read_all))



N,K = II()
a = III()

cuma = [a[0]]
for i in range(1,N):
    cuma.append(cuma[-1]+a[i])

val = []
for i in range(N):
    for j in range(i,N):
        if i==0:
            val.append(cuma[j])
        else:
            val.append(cuma[j]-cuma[i-1])

val.sort(reverse=True)

ans = 0
use = [True]*len(val)
for i in range(val[0].bit_length())[::-1]:
    ok_num = 0
    ng_list = []
    for j in range(len(val)):
        if not use[j]:
            continue
        else:
            if val[j]>>i&1:
                ok_num += 1
            else:
                ng_list.append(j)
    if ok_num>=K:
        ans += 2**i
        for n in ng_list:
            val[n] = False

print(ans)

Solution (Python):
import sys
import numpy as np

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N, K = lr()
A = np.array([0] + lr())
Acum = A.cumsum()
sum_range = Acum[:, None] - Acum[None, :]
sum_range = sum_range[sum_range>0]

def check(x):
    return np.sum(sum_range&x == x) >= K

answer = 0
for i in range(40, -1, -1):
    if check(answer + (1 << i)):
        answer += 1 << i

print(answer)

Solution (Python):
import sys
import numpy as np
import itertools

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N, K = lr()
A = np.array([0] + lr())
Acum = A.cumsum()
sum_range = Acum[:, None] - Acum[None, :]
sum_range = sum_range[sum_range>0]

def check(x):
    return np.sum(sum_range&x == x) >= K

answer = 0
for i in range(40, -1, -1):
    if check(answer + (1 << i)):
        answer += 1 << i

print(answer)

Solution (Python):
import sys
import numpy as np
import itertools

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N, K = lr()
A = np.array([0] + lr())
A_cum = A.cumsum().tolist()
beauties = []
for i, j in itertools.combinations(range(N+1), 2):
    beauties.append(A_cum[j] - A_cum[i])

beauties = np.array(beauties)
j = 0
def check(x):
    return np.sum(beauties&x == x) >= K

answer = 0
for i in range(40, -1, -1):
    if check(answer + (1 << i)):
        answer += 1 << i

print(answer)

Solution (Python):
import sys
import numpy as np
import itertools

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N, K = lr()
A = np.array([0] + lr())
A_cum = A.cumsum()
beauties = []
for i, j in itertools.combinations(range(N+1), 2):
    beauties.append(A_cum[j] - A_cum[i])

beauties = np.array(beauties)
j = 0
def check(x):
    return np.sum(beauties&x == x) >= K

answer = 0
for i in range(40, -1, -1):
    if check(answer + (1 << i)):
        answer += 1 << i

print(answer)

Solution (Python):
from itertools import accumulate

N, K, *A = map(int, open(0).read().split())

S = []
for i in range(N):
    S += [0] + list(accumulate(A[i:]))

ans = 0
for i in reversed(range(41)):
    b = 2 ** i
    T = [s for s in S if b & s != 0]
    if len(T) >= K:
        S = T
        ans += b

print(ans)

Solution (Python):
from sys import stdout
printn = lambda x: stdout.write(str(x))
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
ins = lambda : input().strip()
DBG = True 
BIG = 999999999
R = 10**9 + 7

def ddprint(x):
  if DBG:
    print(x)

n,k = inm()
a = inl()
b = []
acc = [0]*(n+1) 
for i in range(n):
    acc[i+1] = acc[i]+a[i]
for i in range(n):
    for j in range(i+1,n+1):
        b.append(acc[j]-acc[i])
z = 0
for e in range(42,-1,-1):
    c = []
    for x in b:
        if (x>>e)%2 == 1:
            c.append(x)
    if len(c)<k:
        continue
    z += (1<<e)
    b = c
print(z)

Solution (Python):
def main():
    from itertools import accumulate

    n, k = map(int, input().split())
    *a, = map(int, input().split())

    acc = (0,) + tuple(accumulate((a)))

    bs = [acc[r] - acc[l] for r in range(1, n + 1) for l in range(r)]

    ret = 0
    for j in range(39, -1, -1):
        t = ret | 1 << j
        if sum(1 for b in bs if t & b == t) >= k:
            ret = t
    print(ret)


if __name__ == '__main__':
    main()

Solution (Python):
from itertools import accumulate
n, k = map(int, input().split())
A = list(map(int, input().split()))
cumsum = [0] + list(accumulate(A, lambda x, y: x + y))
li = []
for i in range(n + 1):
  for j in range(i + 1, n + 1):
    li.append(cumsum[j] - cumsum[i])
li.sort(reverse=True)

ans = 0

for i in reversed(range(1, 41)):
  new = []
  for s in li:
    
    if (ans + 2 ** (i - 1)) & s == ans + 2 ** (i - 1):
      new.append(s)
  
  if len(new) >= k:
    
    li = new
    ans += 2 ** (i - 1)
print(ans)

Solution (Python):
n,k=map(int,input().split())
a=list(map(int,input().split()))
b=[]
for i in range(n):
  x=0
  for j in range(i,n):
    x+=a[j]
    b.append(x)
ans=0
l=len(b)
for i in range(40,-1,-1):
  x=0
  s=2**i
  for j in range(l):
    if b[j]&(ans+s)==ans+s:x+=1
  if x>=k:ans+=s
print(ans)

Solution (Python):
def main():
    N,K = map(int,input().split())
    A = [int(x) for x in input().split()]
    A_cumsum = [0]
    for i in range(0,N):
        A_cumsum.append(A_cumsum[-1]+A[i])
    
    digit = len(bin(sum(A)))-2

    ls = []
    for i in range(N+1):
        for j in range(i+1,N+1):
            now = bin(A_cumsum[j]-A_cumsum[i])[2:]
            now = "0"*(digit-len(now))+now
            ls.append(now)
    
    ans = [0 for _ in range(digit)]

    for i in range(digit):
        
        now = []
        for j in range(len(ls)):
            if ls[j][i]=="1":
                now.append(ls[j])
        if K<=len(now):
            ls = now
            ans[i]=1
    out = ""
    for i in ans:
        out += str(i)
    print(int(out,2))

    
        
if __name__ == '__main__':
    main()

Solution (Python):
n,k=map(int,input().split())
lists=list(map(int,input().split()))
GG=[]
sumlist=[0]
a=0
for i in range(len(lists)):
    a+=lists[i]
    sumlist.append(a) 
for i in range(len(sumlist)):
    for j in range(i+1,len(sumlist)):
        
        G=sumlist[j]-sumlist[i]
        GG.append(G)
        
GG=sorted(GG,reverse=True)      
maxi=format(GG[0],"b")
from collections import deque
d=deque()
e=deque()
for j in range(len(GG)):
    d.append(format(GG[j],"b").zfill(len(maxi)))
answer=0
index=0
 
while index!=len(maxi):
    counter=0
    for some in d:
        if some[index]=="1":
            counter+=1
            e.append(some)
    if counter>=k:
        answer+=2**(len(maxi)-1-index)
        d=e
        e=deque()
    elif counter<k:
        d=d
        e=deque()
        
    index+=1
    counter=0
print(answer)

Solution (Python):
n,k=map(int,input().split())
lists=list(map(int,input().split()))
GG=[]
sumlist=[0]
a=0
for i in range(len(lists)):
    a+=lists[i]
    sumlist.append(a) 
for i in range(len(sumlist)):
    for j in range(i+1,len(sumlist)):
        
        G=sumlist[j]-sumlist[i]
        GG.append(G)
        
GG=sorted(GG,reverse=True)      
maxi=format(GG[0],"b")
from collections import deque
d=deque()
e=deque()
for j in range(len(GG)):
    d.append(format(GG[j],"b").zfill(len(maxi)))
answer=0
index=0
 
while index!=len(maxi):
    counter=0
    for some in d:
        if some[index]=="1":
            counter+=1
            e.append(some)
    if counter>=k:
        answer+=2**(len(maxi)-1-index)
        d=e
        e=deque()
    elif counter<k:
        d=d
        e=deque()
        
    index+=1
    counter=0
print(answer)

Solution (Python):
import bisect
import copy
N,K=map(int,input().split())
a=list(map(int,input().split()))
s=[0]*(N+1)
for i in range(N):
  s[i+1]=s[i]+a[i]
sumList=[]
for i in range(N):
  for j in range(i,N):
    tmp=s[j+1]-s[i]
    sumList.append(tmp)
sumList.sort()

tmp=len(bin(sumList[-1]))-2+1
tmp=2**tmp
ans=0
while(tmp != 0):
  
  tmpList=[sumList[i] % tmp for i in range(len(sumList)) if sumList[i]%tmp>=(tmp//2)]
  
  if len(tmpList)>=K:
    ans+=tmp//2
    
    sumList=copy.copy(tmpList)
  tmp = tmp // 2
  
print(ans)

Solution (Python):
def to_cumsum(arr):
    """
    1次元配列を累積和の1次元配列に変換する
    """
    cumsum = 0
    new = [0] * len(arr)
    for i, e in enumerate(arr):
        cumsum += e
        new[i] = cumsum
    return new


N, K = map(int, input().split())
tmp = list(map(int, input().split()))
A = [0]
A.extend(tmp)
cumsum = to_cumsum(A)
arr = list()
for i in range(N + 1):
    for j in range(i + 1, N + 1):
        arr.append(cumsum[j] - cumsum[i])

ans_str = ''
for i in reversed(range(1, cumsum[-1].bit_length() + 1)):
    tmp = list(filter(lambda a: (a >> (i - 1)) & 1, arr))
    cnt = len(tmp)
    if cnt >= K:
        ans_str += '1'
        arr = tmp
    else:
        ans_str += '0'
print(int(ans_str, 2))

Solution (Python):

from collections import defaultdict,deque
from heapq import heappush, heappop
from itertools import permutations
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() 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)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    n,k = LI()
    a = LI()
    s = [0]
    for i in a:
        s.append(s[-1]+i)
    S = []
    for i in range(n):
        for j in range(i+1,n+1):
            S.append(s[j]-s[i])
    ans = 0
    for i in range(max(S).bit_length())[::-1]:
        m = 0
        b = 1<<i
        s_ = []
        for j in S:
            if j&b:
                m += 1
                s_.append(j)
        if len(s_) >= k:
            ans += b
            S = [i for i in s_]
    print(ans)
    return


if __name__ == "__main__":
    solve()

Solution (Python):
N, K = map(int, input().split())
from itertools import accumulate
As = [0] + list(map(int, input().split()))
As = list(accumulate(As))
L = 40
xs = []
for i in range(N+1):
    for j in range(i+1, N+1):
        xs.append(As[j] - As[i])
xs.sort()
ts = set(range(N*(N+1)//2))
for j in range(L-1,-1,-1):
    nts = set()
    for i in ts:
        if (xs[i]>>j)%2:
            nts.add(i)
    if len(nts) >= K:
        ts = nts
c = 0
r = 0
for i in ts:
    c += 1
    if r == 0:
        r = xs[i]
    else:
        r = r&xs[i]
    if c == K:
        break
print(r)

Solution (Python):
N, K = map(int, input().split())
from itertools import accumulate
As = [0] + list(map(int, input().split()))
As = list(accumulate(As))
L = len(bin(As[-1]))-2
xs = []
for i in range(N+1):
    for j in range(i+1, N+1):
        xs.append(As[j] - As[i])
xs.sort()
ts = set(range(N*(N+1)//2))
for j in range(L-1,-1,-1):
    nts = set()
    for i in ts:
        if (xs[i]>>j)%2:
            nts.add(i)
    if len(nts) >= K:
        ts = nts
c = 0
r = 0
for i in ts:
    c += 1
    if r == 0:
        r = xs[i]
    else:
        r = r&xs[i]
    if c == K:
        break
print(r)

Solution (Python):
N,K = map(int,input().split())
A = list(map(int,input().split()))
S = []
for i in range(N):
    t = i
    s = 0
    for j in range(t,N):
        s += A[j]
        S.append(s)
n = len(S)
x = 0
for i in range(40):
    m = 40 - i
    t = m - 1
    k = 0
    for j in range(n):
        if (x+2**t)&S[j] == x+2**t:
            k += 1
    if k >= K:
        x += 2**t
print(x)

Solution (Python):
N,K = map(int, input().split())
A = [int(a) for a in input().split()]

S = [0]*(N+1)
for i in range(N):
    S[i+1] = S[i] + A[i]

M = 50
memo = [0]*M
L = []
for i in range(N):
    for j in range(i+1, N+1):
        L.append(S[j]-S[i])
        
for n in L:
    for i in range(M):
        if n&(1<<i):
            memo[i] += 1

p = -1
for i in range(M-1, -1, -1):
    if memo[i] >= K:
        p = i
        break

def f(s, List):
    
    L2 = []
    for n in List:
        if n&(1<<s):
            L2.append(n)
            
    mem = [0]*s
    for n in L2:
        for i in range(s):
            if n&(1<<i):
                mem[i] += 1
                
    p = -1
    for i in range(s-1, -1, -1):
        if mem[i] >= K:
            p = i
            break
            
    return [p, L2]
            
if p < 0:
    ans = 0
else:
    ans = 0
    L2 = L
    while p >= 0:
        ans += 1<<p
        t = f(p, L2)
        p = t[0]
        L2 = t[1]
            
print(ans)

Solution (Python):
N,K=map(int,input().split())
A=list(map(int,input().split()))

B=[]
for s in range(N):
    t=0
    for e in range(s,N):
        t+=A[e]
        B.append(t)

def check(a):
    count=0
    for b in B:
        if b&a==a:
            count+=1
            if count>=K:
                return True
    return False

ans=0
for i in range(40,-1,-1):
    tmp=ans|(1<<i)
    if check(tmp):
        ans=tmp

print(ans)

Solution (Python):
from heapq import heappush,heappop,heapify
from collections import deque,defaultdict,Counter
import itertools
from itertools import permutations,combinations,groupby
import sys
import bisect
import string
import math
import time
import random
def S_():
    return input()
def LS():
    return [i for i in input().split()]
def I():
    return int(input())
def MI():
    return map(int,input().split())
def LI():
    return [int(i) for i in input().split()]
def LI_():
    return [int(i)-1 for i in input().split()]
def NI(n):
    return [int(input()) for i in range(n)]
def NI_(n):
    return [int(input())-1 for i in range(n)]
def StoI():
    return [ord(i)-97 for i in input()]
def ItoS(nn):
    return chr(nn+97)
def LtoS(ls):
    return ''.join([chr(i+97) for i in ls])
def GI(V,E,Directed=False,index=0):
    org_inp=[]
    g=[[] for i in range(n)]
    for i in range(E):
        inp=LI()
        org_inp.append(inp)
        if index==0:
            inp[0]-=1
            inp[1]-=1
        if len(inp)==2:
            a,b=inp
            g[a].append(b)
            if not Directed:
                g[b].append(a)
        elif len(inp)==3:
            a,b,c=inp
            aa=(inp[0],inp[2])
            bb=(inp[1],inp[2])
            g[a].append(bb)
            if not Directed:
                g[b].append(aa)
    return g,org_inp
def bit_combination(k,n=2):
    rt=[]
    for tb in range(n**k):
        s=[tb//(n**bt)%n for bt in range(k)]
        rt+=[s]
    return rt
def show(*inp,end='\n'):
    if show_flg:
        print(*inp,end=end)
YN=['Yes','No']
mo=10**9+7
inf=float('inf')
l_alp=string.ascii_lowercase
u_alp=string.ascii_uppercase
ts=time.time()

input=lambda: sys.stdin.readline().rstrip()
import fractions

def ran_input():
    import random
    n=random.randint(4,16)
    rmin,rmax=1,10
    a=[random.randint(rmin,rmax) for _ in range(n)]
    return n,a
class Comb:
    def __init__(self,n,mo=10**9+7):
        self.fac=[0]*(n+1)
        self.inv=[1]*(n+1)
        self.fac[0]=1
        self.fact(n)
        for i in range(1,n+1):
            self.fac[i]=i*self.fac[i-1]%mo
            self.inv[n]*=i
            self.inv[n]%=mo
        self.inv[n]=pow(self.inv[n],mo-2,mo)
        for i in range(1,n):
            self.inv[n-i]=self.inv[n-i+1]*(n-i+1)%mo
        return
    
    def fact(self,n):
        return self.fac[n]
        
    def invf(self,n):
        return self.inv[n]

    def comb(self,x,y):
        if y<0 or y>x:
            return 0
        return self.fac[x]*self.inv[x-y]*self.inv[y]%mo
show_flg=False
show_flg=True
def gcd( x, y ):
    while y != 0:
        x, y = y, x%y
    return x

ans=0

n,k=LI()
a=LI()
ac=[0]
for i in range(n):
    ac.append(ac[-1]+a[i])

X=43
v=[]
for l in range(n):
    for r in range(l+1,n+1):
        t=ac[r]-ac[l]
        s=[1 if t&(1<<i) else 0 for i in range(X)]
        v.append(s)



for i in range(X-1,-1,-1):
    nx=[]
    for j in v:
        if j[i]==1:
            nx.append(j)
    if len(nx)>=k:
        ans+=(1<<i)
        v=[l for l in nx]
    else:
        0
    
print(ans)

Solution (Python):
import sys
input = sys.stdin.readline


N, K = map(int, input().split())
A = list(map(int, input().split()))
S = []
for i in range(N):
    s = 0
    for j in range(i, N):
        s += A[j]
        S.append(s)


def f(a):
    ret = 0
    for x in S:
        if (x & a) == a:
            ret += 1
    return ret



def main():
    import math
    m = int(math.log(sum(A)*K, 2)) + 1
    ans = 0
    for i in range(m, -1, -1):
        k = f(ans | (1 << i))
        if k >= K:
            ans |= (1 << i)
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
import itertools
n,k=map(int,input().split())
a=list(map(int,input().split()))
l=[]
c=0
for i in range(n):
    for j in range(i,n):
        b=sum(a[i:j+1])
        l.append(b)
l.sort()
for i in range(len(l)):
    l[i]=format(l[i],'b').zfill(len(format(l[-1],'b')))
m=len(l[0])
l=l[::-1]
r=[0]*m
for i in range(m):
    for j in range(len(l)):
        if l[j][i]=='1':
            r[i]+=1
for i in range(m):
    ll=[]
    lll=[]
    if r[i]>=k:
        for j in range(len(l)):
            if l[j][i]=='1':
                ll.append(l[j])
            else:
                lll.append(l[j])
        l=ll
        if len(l)<k:
            l=l+lll
        elif len(l)==k:
            c+=2**(m-i-1)
            break
        elif len(l)>k:
            c+=2**(m-i-1)
    else:
        ll=l
ans=int(l[0],2)
for i in range(len(l)):
    ans&=int(l[i],2)
if len(l)>k:
    print(c)
else:
    print(ans)

Solution (Python):
import itertools
n,k=map(int,input().split())
a=list(map(int,input().split()))
l=[]
c=0
for i in range(n):
    for j in range(i,n):
        b=sum(a[i:j+1])
        l.append(b)
l.sort()
for i in range(len(l)):
    l[i]=format(l[i],'b').zfill(len(format(l[-1],'b')))
m=len(l[0])
l=l[::-1]
r=[0]*m
for i in range(m):
    for j in range(len(l)):
        if l[j][i]=='1':
            r[i]+=1


for i in range(m):
    ll=[]
    lll=[]
    if r[i]>=k:
        for j in range(len(l)):
            if l[j][i]=='1':
                ll.append(l[j])
            else:
                lll.append(l[j])
        l=ll
        if len(l)<k:
            l=l+lll
        elif len(l)==k:
            c+=2**(m-i-1)
            break
        elif len(l)>k:
            c+=2**(m-i-1)
        
        
    else:
        ll=l


ans=int(l[0],2)
for i in range(len(l)):
    ans&=int(l[i],2)
if len(l)>k:
    print(c)
else:
    print(ans)

Solution (Python):
N, K = map(int, input().split())
A = list(map(int, input().split()))

Acum = [0]
for i in range(N):
  Acum.append(Acum[-1] + A[i])

B = []
for i in range(1, len(Acum)):
  for j in range(i):
    B.append(Acum[i] - Acum[j])


tmp = []
for i in range(41, -1, -1):
  C = []
  for j in range(len(B)):
    if (B[j] >> i) & 1:
      C.append(B[j])
  lenC = len(C)
  if lenC >= K:
    B = C
    tmp.append(i)

answer = 0
for t in tmp:
  answer += 2 ** t
print(answer)

Solution (Python):
def main():
    import sys
    input = sys.stdin.readline

    N, K = map(int, input().split())
    A = list(map(int, input().split()))

    cs = [0] * (N+1)
    for i in range(N):
        cs[i+1] = cs[i] + A[i]

    B = []
    for i in range(N):
        for j in range(i, N):
            B.append(cs[j+1] - cs[i])

    tmax = cs[-1].bit_length()
    ans = 0
    for t in range(tmax-1, -1, -1):
        k = 0
        tmp = []
        for b in B:
            if b >> t & 1:
                k += 1
                tmp.append(b)
        if k >= K:
            B = tmp
            ans += 2**t

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
N, K = map(int, input().split())
A = [0] + [int(a) for a in input().split()]
for i in range(N):
    A[i+1] += A[i]
S = []
for i in range(1, N+1):
    for j in range(i):
        S.append(A[i] - A[j])
l, r = 0, 1 << 50
while r - l > 1:
    m = (l+r) // 2
    if len([1 for i in range(len(S)) if S[i] & m == m]) >= K:
        l = m
    else:
        r = m
print(l)

Solution (Python):

import sys, math, itertools, collections, bisect
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
inf = float('inf') ;mod = 10**9+7
mans = inf ;ans = 0 ;count = 0 ;pro = 1

n,k=map(int,input().split())
data=[]
A=list(map(int,input().split()))
for i in range(n):
    for j in range(i+1,n+1):
        data.append(sum(A[i:j]))

for exp in range(41,-1,-1):
    candi=[]
    count=0
    for i,di in enumerate(data):
        if di>>exp & 1:
            count+=1
            candi.append(di)
    if count >= k:
        data = candi
p=data[0]
for ci in data:
    p=p & ci
print(p)

Solution (Python):
n,k=map(int,input().split())
a=list(map(int,input().split()))
s=[0]
for i in range(n):
  s.append(s[-1]+a[i])
l=[]
for i in range(1,n+1):
  for j in range(i):
    l.append(s[i]-s[j])
l.sort(reverse=True)
m=61
ans=0
chk=0
for i in range(m)[::-1]:
  cnt=0
  chk+=2**i
  for j in l:
    if chk & j == chk:
      cnt+=1
  if cnt < k:
    chk-=2**i
print(chk)

Solution (Python):

import sys

N,K=map(int, sys.stdin.readline().split())
A=map(int, sys.stdin.readline().split())

S=[0]
for a in A:
    S.append(S[-1]+a)

B=[]    
l=len(S)

for i in range(l):
    for j in range(i+1,l):
        B.append(S[j]-S[i])

B.sort(reverse=True)

kouho=[ 1 for _ in range(len(B))]    

for keta in range(60,-1,-1):
    new_kouho=[]
    goukei=0 
    for idx,b in enumerate(B):
        b=b>>keta&1
        if kouho[idx]==1:   
            new_kouho.append(b)
            goukei+=b
        else:
            new_kouho.append(0)       
    if K<=goukei:
        kouho=new_kouho 

ans=None
for k,b in zip(kouho,B):
    if k==1:
        if ans is None:
            ans=b
        else:
            ans&=b

print ans

Solution (Python):
from itertools import accumulate
def main():
	n, k, *a = map(int, open(0).read().split())
	acc = [0] + list(accumulate(a))
	ps = [acc[j] - acc[i] for i in range(n) for j in range(i + 1, n + 1)]
	ans = 0
	for i in range(max(ps).bit_length(), -1, -1):
		x = ans + (1 << i)
		if sum(y & x == x for y in ps) >= k:
			ans = x
	print(ans)

if __name__=="__main__":
	main()

Solution (Python):
from itertools import accumulate
n, k, *a = map(int, open(0).read().split())
acc = [0] + list(accumulate(a))
ps = [acc[j] - acc[i] for i in range(n) for j in range(i + 1, n + 1)]
ans = 0
for i in range(max(ps).bit_length(), -1, -1):
	x = ans + (1 << i)
	c = 0
	for y in ps:
		if y & x == x:
			c += 1
			if c >= k:
				ans = x
				break
print(ans)

Solution (Python):
from itertools import accumulate
n, k, *a = map(int, open(0).read().split())
acc = [0] + list(accumulate(a))
ps = [acc[j] - acc[i] for i in range(n) for j in range(i + 1, n + 1)]
ans = 0
for i in range(max(ps).bit_length(), -1, -1):
	x = ans + (1 << i)
	c = 0
	for y in ps:
		if y & x == x:
			c += 1
			if c >= k:
				ans = x
				break
print(ans)

Solution (Python):
n,k=map(int,input().split())

l1=list(map(int,input().split()))
l2=[]
for i in range(n):
  sum1=0
  for j in range(i,n):
    sum1+=l1[j]
    l2.append(sum1)
l2=sorted(l2)
d=[0 for i in range(50)]
bitlist=[]
for i in l2:
  bit=[]
  num=i
  for j in range(50):
    d[j]+=num%2
    bit.append(num%2)
    num//=2
  bitlist.append(bit)
index=-1
for i in range(50):
  if d[i]>=k:
    index=i
    
if index==-1:
  print(0)
  exit()
b=True
ans1=[index]
while b:
  bitsub=[]
  for i in bitlist:
    if i[index]==0:
      pass
    else:
      bitsub.append(i)
  d=[0 for i in range(50)]
  for i in bitsub:
    for j in range(50):
      d[j]+=i[j]
  b=False
  for i in range(1,index+1):
    if d[index-i]>=k and not b:
      index=index-i
      b=True
      ans1.append(index)
  bitlist=bitsub
ans=0
for j in ans1:
  ans+=bitlist[0][j]*2**j
print(ans)

Solution (Python):
import sys
from itertools import accumulate, combinations
n, k = [int(i) for i in sys.stdin.readline().split()]
a_ls = [int(i) for i in sys.stdin.readline().split()]
s_ls = [i for i in accumulate([0] + a_ls)]
s_ls = [s_ls[j] - s_ls[i] for i, j in combinations(range(n+1), 2)]
str_len = len(bin(max(s_ls))) - 2
def check(x):
    cnt = 0
    for s in s_ls:
        if x == (x & s):
            cnt += 1
        if cnt >= k:
            return 1
    return 0

x = 0
res = []
for i in range(str_len - 1, -1, -1):
    cnt = 0
    for s in s_ls:
        if x + 2**i == ((x + 2**i) & s):
            cnt += 1
        if cnt >= k:
            x += 2 ** i
            break
print(x)

Solution (Python):
def examA():
    N = I(); A = LI()
    sumnale = sum(A)/N
    cur = 10**9; curL = 0
    for i in range(N):
        if cur>abs(A[i]-sumnale):
            cur = abs(A[i]-sumnale)
            curL = i
    ans = curL
    print(ans)
    return

def examB():
    N, K = LI(); A = LI()
    AK = []; ans = 0
    d = defaultdict(int)
    for i in range(N):
        cur = A[i]
        AK.append(cur)
        for k in range(40):
            if cur&(1<<k)==(1<<k):
                d[k] +=1
        for j in range(i+1,N):
            cur += A[j]
            AK.append(cur)
            for k in range(40):
                if cur & (1 << k) == (1 << k):
                    d[k] += 1


    k = 40; curA = []
    while(k>0):
        k -=1
        if d[k]<K:
            continue
        for i in range(len(AK)):
            if AK[i]&(1<<k)==(1<<k):
                curA.append(AK[i])
        if len(curA)>=K:
            ans = 2**k
            break

    while(k>0):
        k -=1; neA = []
        if d[k]<K:
            continue
        for i in range(len(curA)):
            if curA[i]&(1<<k)==(1<<k):
                neA.append(curA[i])
        if len(neA)<K:
            continue
        else:
            curA = copy.deepcopy(neA)
            ans += 2**k
    print(ans)
    return

import sys,copy,bisect,itertools,heapq,math
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
mod = 10**9 + 7
inf = float('inf')

if __name__ == '__main__':
    examB()

Solution (Python):
n, k = map(int, input().split())
a = list(map(int, input().split()))
li = []
for l in range(n):
    s = 0
    i = l
    while i < n:
        s += a[i]
        li.append(s)
        i += 1

ans = 0
for i in range(40):
    j = 39 - i
    if sum(map(lambda x: (x >> j) & 1, li)) >= k:
        ans += 1 << j
        tmp = []
        for x in li:
            if (x >> j) & 1:
                tmp.append(x)
        li = tmp
print(ans)