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
N, K = map(int, input().split())
As = list(map(int, input().split()))

subsecs = []
S = [0] + list(accumulate(As))
for f in range(1, N+1):
    for t in range(f, N+1):
        subsecs.append(S[t] - S[f-1])

ans = 0
digit = 1 << 40
for d in range(41):
    nsubsecs = []
    for subsec in subsecs:
        if subsec & digit:
            nsubsecs.append(subsec)
    if len(nsubsecs) >= K:
        ans += digit
        subsecs = nsubsecs
    digit >>= 1
print(ans)

Solution (Python):
n,k = map(int, input().split())
a = list(map(int, input().split()))
lis=[]
for i in range(n):
    now=0
    for j in range(i,n):
        now+=a[j]
        lis.append(now)
lis.sort()
bi=[]
mx=40
for i in range(len(lis)):
    num=lis[i]
    keta=[0]*mx

    for j in reversed(range(mx)):
        if num%2==1:
            keta[j]+=1
        num//=2
        if num==0:
            break
    bi.append(keta)

anslis=[]
kouho=set(lis)
for j in range(mx):
    cnt=0
    nxt=set()
    for i in range(len(bi)):
        if not lis[i] in kouho:
            continue
        b=bi[i]
        if b[j]==1:
            cnt+=1
            nxt.add(lis[i])
    if cnt>=k:
        kouho &= nxt

kouho=list(kouho)
ans=kouho[0]
for num in kouho:
    ans &=num
print(ans)

Solution (Python):
def dwacon5th_prelims_b():
    n, k = (int(x) for x in input().split())
    A = [int(x) for x in input().split()]
    B = []

    for i in range(n):
        cum = 0
        for a in A[i:]:
            cum += a
            B.append(cum)
    B.sort(reverse=True)

    bit = [0]*41
    cand = B.copy()
    for i in range(40, -1, -1):
        nx = []
        for b in cand:
            if (b >> i) & 1: nx.append(b)
        if len(nx) >= k:
            cand = nx.copy()
            bit[i] = 1

    ans = int(''.join([str(x) for x in reversed(bit)]), base=2)
    print(ans)

if __name__ == '__main__':
   dwacon5th_prelims_b()

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

l=[]
for i in range(n):
  s=0
  for j in range(i,n):
    s+=a[j]
    l.append(s)
t=0
for i in range(40,-1,-1):
  t+=2**i
  c=0
  for j in l:
    if t==j&t:
      c+=1
  if c<k:
    t-=2**i
print(t)

Solution (Python):
n, k = map(int, input().split())
a = list(map(int, input().split()))
stand_bit = [set() for i in range(42)]
acc = [0 for i in range(n+1)]
for i in range(1, n+1):
    acc[i] += a[i-1] + acc[i-1]
set_ans = set()
for i in range(n):
    for j in range(i, n):
        tmp = acc[j+1] - acc[i]
        set_ans.add(tmp)
        s = tmp
        index = 41
        while s:
            if s & 1:
                stand_bit[index].add(tmp)
            s = s >> 1
            index -= 1
for i in range(42):
    if len(set_ans & stand_bit[i]) >= k:
        set_ans &= stand_bit[i]
ans_list = list(set_ans)
ans = ans_list[0]
for i in ans_list:
    ans &= i
print(ans)

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

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


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

B = []
for i in range(N+1):
  for j in range(i+1,N+1):
    wk = AS[j] - AS[i]
    if wk > 0:
      B.append(wk)

ans = 0
for m in range(38,-1,-1):
  cnt = 0
  p = pow(2,m)
  nxt = []
  for b in B:
    if p & b == p:
      nxt.append(b)
  if len(nxt) >= K:
    ans += p
    B = nxt
    
  else:
    pass
    

print(ans)

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

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


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

B = []
for i in range(N+1):
  for j in range(i+1,N+1):
    wk = AS[j] - AS[i]
    if wk > 0:
      B.append(wk)

ans = 0
for m in range(38,-1,-1):
  cnt = 0
  p = pow(2,m)
  nxt = []
  for b in B:
    if p & b == p:
      nxt.append(b)
  if len(nxt) >= K:
    ans += p
    B = nxt
    
  else:
    pass
    

print(ans)

Solution (Python):
from typing import List, Dict

INF = 10**9+1


def read_int() -> int:
    return int(input().strip())


def read_ints() -> List[int]:
    return list(map(int, input().strip().split(' ')))

def remove_if_gt(a, b):
    if a >= b:
        return a-b
    return a

def solve() -> int:
    N, K = read_ints()
    A = read_ints()
    S = []
    for i in range(N):
        prefix = 0
        for j in range(i, N):
            prefix += A[j]
            S.append(prefix)
    S.sort(reverse=True)
    highest_bit = 40
    answer = 0
    while highest_bit >= 0:
        while 1<<highest_bit > S[0]:
            highest_bit -= 1
            if highest_bit < 0:
                return answer
        highest_bit_val = 1<<highest_bit
        count = 0
        for i in range(len(S)):
            if S[i] >= highest_bit_val:
                count += 1
            else:
                break
        if count < K:
            S = [remove_if_gt(s, highest_bit_val) for s in S]
        else:
            answer += highest_bit_val
            S = [remove_if_gt(s, highest_bit_val) for s in S if s >= highest_bit_val]
        S.sort(reverse=True)
    return answer


if __name__ == '__main__':
    print(solve())

Solution (Python):
ma = lambda :map(int,input().split())
lma = lambda :list(map(int,input().split()))

n,k = ma()
A = lma()
bu = []
for i in range(n):
    tmp=0
    for j in range(i,n):
        tmp+=A[j]
        bu.append(tmp)
mb = bin(max(bu))
ans = 0
for i in range(len(mb)-3,-1,-1):
    tmp=[]
    for b in bu:
        if (b>>i &1):
            tmp.append(b)
    if len(tmp)>=k:
        ans+=2**i
        bu=tmp
    else:
        pass
print(ans)

Solution (Python):
ma = lambda :map(int,input().split())
lma = lambda :list(map(int,input().split()))
tma = lambda :tuple(map(int,input().split()))
ni = lambda:int(input())
yn = lambda fl:print("Yes") if fl else print("No")
import collections
import math
import itertools
import heapq as hq

n,k = ma()
A = lma()
bu = []
for i in range(n):
    tmp=0
    for j in range(i,n):
        tmp+=A[j]
        bu.append(tmp)
mb = bin(max(bu))
ls = [0]*(len(mb)-2)
for b in bu:
    for i in range(len(bin(b))-2):
        if (b>>i &1):
            ls[i]+=1
ans = 0
tmp=bu
for i in range(len(mb)-3,-1,-1):
    if ls[i]<k:
        continue
    tmp2=[]
    for b in tmp:
        if (b>>i &1):
            tmp2.append(b)
    if len(tmp2)>=k:
        ans+=2**i
        tmp=tmp2
    else:
        pass
print(ans)

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




def RD(): return input().rstrip().decode()
def II(): return int(input())
def FI(): return float(input())
def MI(): return map(int,input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int,input().split()))
def LF(): return list(map(float,input().split()))
def TI(): return tuple(map(int,input().split()))




def main():
	n,k=MI()
	A=LI()
	B=[0]
	C=[0]

	for i in A:
		B.append(B[-1]+i)
		C.append(C[-1]^i)
	


	ans=0

	D=[]
	E=[]
	F=[]

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

	

	ans=0

	for i in range(41,-1,-1):
		cnt=0
		E=[]
		for d in D:
			if 1<<i&d:
				E.append(d)
				cnt+=1
				

		if cnt>=k:
			ans+=1<<i
			D=E.copy()

	print(ans)
	
	









if __name__ == "__main__":
	main()

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

Solution (Python):
from copy import copy

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

X=[0]*((N*(N+1))//2)
I=0

for i in range(N):
    S=0
    for j in range(i,N):
        S+=A[j]
        X[I]=S
        I+=1

alpha=max(X)
B=1
while alpha>B:
    B<<=1

P=0
while B>0:
    C=0
    Y=[]
    for a in X:
        if a&B:
            C+=1
            Y.append(a)

    if C>=K:
        P+=B
        X=copy(Y)

    B>>=1

print(P)

Solution (Python):



import itertools

n, k = map(int, input().split())
a = list(itertools.accumulate(list(map(int, input().split()))))
a = [0] + a
s = [None] * (n*(n+1)//2)
cnt = 0
for i in range(n+1):
    for j in range(i+1,n+1):
        s[cnt] = format(a[j]-a[i], '050b')
        cnt += 1

ret = 0
sel = set(range(n*(n+1)//2))
for i in range(50):
    nxt = set([])
    for j in sel:
        if s[j][i] == '1':
            nxt.add(j)
    if len(nxt)>=k:
        sel = nxt
        ret += 2**(49-i)
print(ret)

Solution (Python):
def calc_beauty_list(N, A):
    cumsum = [0 for _ in range(N)]
    cumsum[0] = A[0]
    for n in range(1, N):
        cumsum[n] = cumsum[n - 1] + A[n]
    beauty_list = [0 for _ in range(N * (N + 1) // 2)]
    index = 0
    for left in range(N):
        for right in range(left, N):
            beauty = cumsum[right]
            if left > 0:
                beauty -= cumsum[left - 1]
            beauty_list[index] = beauty
            index += 1
    return beauty_list


def main():
    MAX_BIT = 40
    N, K = list(map(int, input().split()))
    A = list(map(int, input().split()))
    beauty_list = calc_beauty_list(N, A)
    
    
    
    
    
    
    
    usable_flag = [1 for _ in range(N * (N + 1) // 2)]
    found_k_bit = False
    for shift in range(MAX_BIT - 1, -1, -1):
        cnt = len([b for b, u in zip(beauty_list, usable_flag) if u == 1 and (b >> shift & 1 == 1)])
        if cnt >= K:
            found_k_bit = True
            for n, b in enumerate(beauty_list):
                if b >> shift & 1 == 0:
                    usable_flag[n] = 0
        if cnt == K:
            found_k_bit = True
            break
    if not found_k_bit:
        print(0)
    else:
        ans = (1 << MAX_BIT) - 1
        cnt = 0
        for n, b in enumerate(beauty_list):
            if usable_flag[n] == 1 and cnt <= K:
                ans &= b
                cnt += 1
        print(ans)


if __name__ == '__main__':
    main()

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

beauty = [a[0]]
base = [a[0]]

for ai in a[1:]:
    for i in range(len(base)):
        base[i] += ai
        beauty.append(base[i])
    beauty.append(ai)
    base.append(ai)

ans = 0
for bi in range(41,-1,-1):
    cand = []
    for i in beauty:
        if((i>>bi)&1):
            cand.append(i)
    if(len(cand) >= k):
        ans += 1<<bi
        beauty = cand

print(ans)

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

A=list(map(int,input().split()))
two=[2**i for i in range(44)][::-1]

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

log=[]

for i in range(N):
    for j in range(i,N+1):
        if sums[j]-sums[i]!=0:
            log.append(sums[j]-sums[i])
log.sort()

ans=0
for t in two:
    count=0
    for item in log:
        if item>=t:
            count+=1
    if count>=K:
        ans+=t
        log=list(filter(lambda x: x>=t, log))
    log=list(map(lambda x: x%t, log))
    

print(ans)

Solution (Python):
n,k=map(int,input().split())
a=[0]+list(map(int,input().split()))
for i in range(n):
  a[i+1]+=a[i]
d=[]
for i,x in enumerate(a):
  for y in a[:i]:
    d+=[x-y]
ans=0
t=2**40
for _ in range(41):
  cnt=0
  for j in d:
    cnt+=(ans+t)&j==(ans+t)
  if cnt>=k:ans+=t
  t//=2
print(ans)

Solution (Python):
from itertools import accumulate, combinations
from collections import Counter
def main():
    n, k = map(int, input().split())
    A = list(map(int, input().split()))
    A = [0] + list(accumulate(A))
    A = [A[j] - A[i] for i, j in combinations(range(n + 1), 2)]
    C = Counter(A)
    ans = 0
    i = pow(2, 39)
    for _ in range(40):
        cnt = 0
        for key, value in C.items():
            b = ans + i
            if b & key == b: cnt += value
        if cnt >= k: ans += i
        i //= 2
    print(ans)
    
if __name__ == '__main__':
    main()

Solution (Python):
import sys
import math
import copy
from heapq import heappush, heappop, heapify
from functools import cmp_to_key
from bisect import bisect_left, bisect_right
from collections import defaultdict, deque, Counter



input = sys.stdin.readline
getS = lambda: input().strip()
getN = lambda: int(input())
getList = lambda: list(map(int, input().split()))
getZList = lambda: [int(x) - 1 for x in input().split()]

INF = float("inf")
MOD = 10**9 + 7
divide = lambda x: pow(x, MOD-2, MOD)

class BIT():
    
    def __init__(self, n):
        self.n = n
        self.BIT = [0] * (n + 2)

    
    def query(self, idx):
        res_sum = 0
        while idx > 0:
            res_sum += self.BIT[idx]
            idx -= idx & (-idx)
        return res_sum

    
    def update(self, idx, x):
        
        while idx <= self.n:
            
            self.BIT[idx] += x
            idx += idx & (-idx)
        return

    def show(self):
        print(self.BIT)


def solve():
    n, k = getList()
    lis = [0] + getList()
    for i in range(n):
        lis[i+1] += lis[i]

    ka = []
    for i in range(n):
        for j in range(i+1, n+1):
            ka.append(lis[j]-lis[i])

    ans = 0
    for rui in range(50, -1, -1):
        ok = []
        tgt = pow(2, rui)
        for kaka in ka:
            if kaka & tgt == tgt:
                ok.append(kaka)
        if len(ok) >= k:
            ans += tgt
            ka = ok

    print(ans)





def main():
    n = getN()
    for _ in range(n):
        solve()

    return
if __name__ == "__main__":
    
    solve()

Solution (Python):
from itertools import accumulate

def main():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))
    sum_A = sum(A)
    B = [0] + list(accumulate(A))
    cand = []
    for i in range(N+1):
        for j in range(i+1,N+1):
            cand.append(B[j] - B[i])
    ans = 0
    for i in range(40):
        bit = 1 << (39 - i)
        cnt = 0
        new_cand = []
        for c in cand:
            if c & bit:
                cnt += 1
                new_cand.append(c)

        if cnt >= K:
            ans += 2 ** (39 - i)
            cand = new_cand


    print(ans)




if __name__ == "__main__":
    main()

Solution (Python):
from collections import Counter
N, K = map(int, input().split())
A = list(map(int, input().split()))
cusum = [A[0]]
for i in range(1, N):
    cusum += [cusum[-1]+A[i]]
B = []
for i in range(N):
    B += [cusum[i]]
    for j in range(i+1, N):
        B += [cusum[j] - cusum[i]]
max_bit = 43
ans = 0
while max_bit > 0:
    counter = Counter()
    for b in B:
        for i in range(44):
            if b & 1 == 1:
                counter[i] += 1
            b = b >> 1
    for i in range(max_bit, -1, -1):
        if counter[i] >= K:
            filter_bit = 2 ** i
            B = [b for b in B if b & filter_bit == filter_bit]
            max_bit = i-1
            ans += filter_bit
            break
        max_bit = i
print(ans)

Solution (Python):
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    from itertools import accumulate
    import bisect
    n, k = map(int, readline().split())
    a = list(map(int, readline().split()))
    s = []
    for i in range(n):
        nx = list(accumulate(a[i:]))
        s.extend(nx)

    s.sort()

    first = len(format(s[-1], "b")) - 1
    cur = 0

    for i in range(first, -1, -1):
        x = cur + 2 ** i
        cnt = 0
        for y in s[::-1]:
            if x & y == x:
                cnt += 1
            if x > y:
                break
        if cnt >= k:
            cur = x

    print(cur)


if __name__ == '__main__':
    main()

Solution (Python):
import math

n, k = map(int, input().split())
a = list(map(int, input().split()))
b = [0] * (n + 1)
for i in range(n):
    b[i + 1] = b[i] + a[i]
m = int(math.log2(b[n]))
x = pow(2, m)
s = [[] for _ in range(m + 2)]
for i in range(n):
    for j in range(i + 1, n + 1):
        s[0].append(b[j] - b[i])
c = 0
ans = 0
for i in range(1, m + 2):
    for j in range(len(s[c])):
        if s[c][j] >= x:
            s[i].append(s[c][j] - x)
            s[c][j] -= x
    if len(s[i]) >= k:
        c = i
        ans += x
    x //= 2
print(ans)

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

N, K = map(int, input().split())
a = list(map(int, input().split()))
acc = [0]

for ai in a:
    acc.append(acc[-1]+ai)

l = []

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

ans = 0

for i in range(45):
    nl = []
    
    for li in l:
        if (li>>(44-i))&1:
            nl.append(li)
    
    if len(nl)>=K:
        ans += 2**(44-i)
        l = nl
    
print(ans)

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

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

C = []
for i in range(N):
    for j in range(i+1, N+1):
        C.append(SA[j]-SA[i])
C.sort()


ans = 0
for i in range(41, -1, -1):
    B = 1 << i
    ind = bisect.bisect_left(C, B)
    if len(C)-ind >= K:
        ans += B
        C = C[ind:]
    for j in range(len(C)):
        C[j] &= B-1
    C.sort()
    
print(ans)

Solution (Python):
import sys
import math
import collections
import bisect
import copy



sys.setrecursionlimit(10 ** 7)
INF = 10 ** 16
MOD = 10 ** 9 + 7


ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))
na1 = lambda: list(map(lambda x: int(x) - 1, sys.stdin.readline().split()))





def main():
    n, k = ns()
    a = na()

    d = []
    for i in range(n):
        tmp = a[i]
        d.append(tmp)
        for j in range(i + 1, n):
            tmp += a[j]
            d.append(tmp)

    def maxbit(a, idx, k):
        first1 = [[] for _ in range(41)]
        for ai in a:
            for i in range(40, -1, -1):
                if ai >> i & 1:
                    first1[i].append(ai)

        for i in range(idx - 1, -1, -1):
            if len(first1[i]) >= k:
                return first1[i], i

        return first1[idx], -1

    idx = 40
    while True:
        tmp, idx = maxbit(d, idx, k)
        if idx == -1:
            break
        d = tmp

    bit1 = [0 for _ in range(41)]
    for di in d:
        for i in range(41):
            if di >> i & 1:
                bit1[i] += 1

    ans = 0
    for i, bi in enumerate(bit1):
        if bi >= k:
            ans += pow(2, i)

    print(ans)

if __name__ == '__main__':
    main()

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

lst = []
for i in range(N):
  lst.append(Xs[i])
  tmp = Xs[i]
  for j in range(i+1,N):
    tmp += Xs[j]
    lst.append(tmp)
  
x = 0
for i in range(40, 0, -1):
  cnt = 0
  t = x + 2**(i-1)
  for l in lst:
    if t&l == t:
      cnt += 1
  if cnt >= K:
    x = t
  
print(x)

Solution (Python):
import sys
from itertools import accumulate

sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    n, k = map(int, input().split())
    A = list(map(int, input().split()))

    R = [0] + list(accumulate(A))
    subs = []
    for left in range(n + 1):
        for right in range(left + 1, n + 1):
            num = R[right] - R[left]
            subs.append(num)

    for i in reversed(range(41)):
        tmp = []
        for num in subs:
            if num & (1 << i):
                tmp.append(num)
        if len(tmp) >= k:
            subs = tmp

    res = subs[0]
    for num in subs:
        res &= num
    print(res)


if __name__ == '__main__':
    resolve()

Solution (Python):
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

in_n = lambda: int(readline())
in_nn = lambda: map(int, readline().split())
in_s = lambda: readline().rstrip().decode('utf-8')
in_nl = lambda: list(map(int, readline().split()))
in_nl2 = lambda H: [in_nl() for _ in range(H)]
in_map = lambda: [s == ord('.') for s in readline() if s != ord('\n')]
in_map2 = lambda H: [in_map() for _ in range(H)]
in_all = lambda: map(int, read().split())


def main():

    N, K = in_nn()
    a = in_nl()
    a_sum = [0] * (N + 1)

    b = set()
    for i in range(N):
        a_sum[i + 1] = a_sum[i] + a[i]

    for i in range(N + 1):
        for j in range(i + 1, N + 1):
            b.add(a_sum[j] - a_sum[i])

    b = sorted(list(b))
    bit = max(b).bit_length()

    ans = 0
    select = set(b)
    for x in range(bit, 0, -1):

        cnt = 0
        t_select = set()
        for tb in select:
            
            if (tb >> (x - 1)) & 1:
                cnt += 1
                t_select.add(tb)

        
        if cnt >= K:
            ans += (1 << (x - 1))
            select = t_select

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):





from itertools import combinations
N, K = map(int, input().split())
A = list(map(int, input().split()))
B = [sum(A[s:t]) for s, t in combinations(range(N + 1), 2)]

l = max(B).bit_length()  
x = 0  
for i in range(l, -1, -1):  
    
    x_tmp = x + (1 << i)
    if sum((x_tmp & b) == x_tmp for b in B) >= K:  
        x = x_tmp
print(x)

Solution (Python):
from itertools import accumulate

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

C = [0] + list(accumulate(A))
S = []

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

for i in range(64)[::-1]:
    tmp = []
    for s in S:
        if (s >> i) & 1:
            tmp.append(s)

    if len(tmp) >= K:
        S = tmp

res = 2**64 - 1

for i in range(K):
    res &= S[i]

print(res)

Solution (Python):
from itertools import accumulate

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

C = [0] + list(accumulate(A))
S = []

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

for i in range(64)[::-1]:
    tmp = []
    for s in S:
        if (s >> i) & 1:
            tmp.append(s)

    if len(tmp) >= K:
        S = tmp[:]

res = 2**64 - 1

for i in range(K):
    res &= S[i]

print(res)

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


kouho=[]
for l in range(n):
    kouho.append(a[l])
    tmp=a[l]
    for r in range(l+1,n):
        tmp+=a[r]
        kouho.append(tmp)
kouho.sort()

cnt=0
while 2**cnt<=kouho[-1]:
    cnt+=1

flag=[1]*len(kouho)

while cnt>=0:
    tmp_cnt=0
    for i,item in enumerate(kouho):
        if 2**cnt & item and flag[i]==1:
            tmp_cnt+=1
    if tmp_cnt>=k:
        for i,item in enumerate(kouho):
            if 2**cnt & item and flag[i]==1:
                continue
            else:
                flag[i]=0
    cnt-=1

ans=-1
cnt=0


for i in range(len(kouho)):
    if flag[i]==1:
        cnt+=1
        if ans==-1:
            ans=kouho[i]
        else:
            ans=ans&kouho[i]
    if cnt==k:
        print(ans)
        exit()

Solution (Python):
import numpy as np
N, K = [int(_) for _ in input().split()]
A = np.array([0] + [int(_) for _ in input().split()])
C = np.cumsum(A)
X = np.zeros(N * (N + 1) // 2, dtype=int)
il = 0
for l in range(N):
    X[il:il + N - l] = C[l + 1:N + 1] - C[l]
    il += N - l
ans = 0
for i in range(40, -1, -1):
    Y = (X // (2**i) & 1)
    I = Y == 1
    if I.sum() >= K:
        X = X[I]
        ans += 2**i
print(ans)

Solution (Python):
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))
def getArray(intn):
    return [int(input()) for i in range(intn)]
def input():
    return sys.stdin.readline().rstrip()
def rand_N(ran1, ran2):
    return random.randint(ran1, ran2)
def rand_List(ran1, ran2, rantime):
    return [random.randint(ran1, ran2) for i in range(rantime)]
def rand_ints_nodup(ran1, ran2, rantime):
  ns = []
  while len(ns) < rantime:
    n = random.randint(ran1, ran2)
    if not n in ns:
      ns.append(n)
  return sorted(ns)

def rand_query(ran1, ran2, rantime):
  r_query = []
  while len(r_query) < rantime:
    n_q = rand_ints_nodup(ran1, ran2, 2)
    if not n_q in r_query:
      r_query.append(n_q)
  return sorted(r_query)

from collections import defaultdict, deque, Counter
from sys import exit
from decimal import *
import heapq
import math
from fractions import gcd
import random
import string
import copy
from itertools import combinations, permutations, product
from operator import mul
from functools import reduce
from bisect import bisect_left, bisect_right

import sys
sys.setrecursionlimit(1000000000)
mod = 10 ** 9 + 7





def splitbit(n):
    for i in range(61):
        if not n & (1 << i):
            xor_l[i] = 0

N, K = getNM()
A = getList()

imos = copy.deepcopy(A)
imos.insert(0, 0)

for i in range(N):
    imos[i + 1] += imos[i]

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

ans = 0
for i in range(60, -1, -1):
    cnt = 0
    for j in range(len(num)):
        if ans + (2 ** i) & num[j] == ans + (2 ** i):
            cnt += 1
    if cnt >= K:
        
        ans += (2 ** i)
print(ans)

Solution (Python):
import sys
from itertools import accumulate

sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    n, k = map(int, input().split())
    A = list(map(int, input().split()))

    R = [0] + list(accumulate(A))
    subs = []
    for left in range(n):
        for right in range(left + 1, n + 1):
            subs.append(R[right] - R[left])

    res = 0
    for mask in reversed(range(40)):
        if len(subs) == 0:
            break
        tmp = []
        for i in range(len(subs)):
            if subs[i] & (1 << mask):
                tmp.append(subs[i])
        if len(tmp) >= k:
            res += 1 << mask
            subs = tmp
    print(res)


if __name__ == '__main__':
    resolve()

Solution (Python):






import sys
input = sys.stdin.readline

import math

import bisect
import heapq
from collections import defaultdict
from collections import deque
from collections import Counter
from functools import lru_cache





MOD = 10**9+7
INF = float('inf')
AZ = "abcdefghijklmnopqrstuvwxyz"






def I(): return int(input().strip())
def S(): return input().strip()
def IL(): return list(map(int,input().split()))
def SL(): return list(map(str,input().split()))
def ILs(n): return list(int(input()) for _ in range(n))
def SLs(n): return list(input().strip() for _ in range(n))
def ILL(n): return [list(map(int, input().split())) for _ in range(n)]
def SLL(n): return [list(map(str, input().split())) for _ in range(n)]


def P(arg): print(arg); return
def Y(): print("Yes"); return
def N(): print("No"); return
def E(): exit()
def PE(arg): print(arg); exit()
def YE(): print("Yes"); exit()
def NE(): print("No"); exit()


def DD(arg): return defaultdict(arg)


def inv(n): return pow(n, MOD-2, MOD)


kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n): return kaijo_memo[n]
  if(len(kaijo_memo) == 0): kaijo_memo.append(1)
  while(len(kaijo_memo) <= n): kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n): return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0): gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n): gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if n == r: return 1
  if n < r or r < 0: return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret


def factorization(n):
  arr = []
  temp = n
  for i in range(2, int(-(-n**0.5//1))+1):
    if temp%i==0:
      cnt=0
      while temp%i==0: 
        cnt+=1 
        temp //= i
      arr.append([i, cnt])
  if temp!=1: arr.append([temp, 1])
  if arr==[]: arr.append([n, 1])
  return arr


def make_divisors(n):
  divisors = []
  for i in range(1, int(n**0.5)+1):
    if n % i == 0:
      divisors.append(i)
      if i != n // i: 
        divisors.append(n//i)
  return divisors


def make_primes(N):
  max = int(math.sqrt(N))
  seachList = [i for i in range(2,N+1)]
  primeNum = []
  while seachList[0] <= max:
    primeNum.append(seachList[0])
    tmp = seachList[0]
    seachList = [i for i in seachList if i % tmp != 0]
  primeNum.extend(seachList)
  return primeNum


def gcd(a, b):
    while b: a, b = b, a % b
    return a


def lcm(a, b):
    return a * b // gcd (a, b)


def count_bit(n):
  count = 0
  while n:
    n &= n-1
    count += 1
  return count


def base_10_to_n(X, n):
  if X//n: return base_10_to_n(X//n, n)+[X%n]
  return [X%n]

def base_n_to_10(X, n):
  return sum(int(str(X)[-i-1])*n**i for i in range(len(str(X))))

def base_10_to_n_without_0(X, n):
  X -= 1
  if X//n: return base_10_to_n_without_0(X//n, n)+[X%n]
  return [X%n]


def int_log(n, a):
  count = 0
  while n>=a:
    n //= a
    count += 1
  return count




def ACC1(l):
  N = len(l)
  ret = [0]*(N+1)
  for i in range(N):
    ret[i+1] = ret[i]+l[i]
  return ret

N,K = IL()
A = IL()
ACC = ACC1(A)
SUMS = []
for i in range(N+1):
  for j in range(i+1,N+1):
    SUMS.append(ACC[j]-ACC[i])
ans = 0

for k in range(int_log(max(SUMS),2)+1)[::-1]:
  HERE = list(filter(lambda x: (x>>k)&1, SUMS))
  if len(HERE)>=K:
    ans += 1<<k
    SUMS = HERE
print(ans)