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):
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)
ans = 0
remains = []
for i in range(40):
  bit = 1 << (39 - i)
  l = []
  cnt = 0
  
  for j in range(len(L)):
    if bit & L[j] == bit:
      cnt += 1
      l.append(L[j])
  if cnt >= K:
    ans += bit
    L = l
print(ans)

Solution (Python):
N,K = map(int,input().split())
A = list(map(int,input().split()))
S = [0]
beauties = []
ans = 0

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

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

for i in reversed(range(64)):
    tmp = []
    for j in range(len(beauties)):
        if (beauties[j]>>i)&1:
            tmp.append(beauties[j])
    if len(tmp)>=K:
        ans += 2**i
        beauties = tmp
        
print(ans)

Solution (Python):
import math
N,K = map(int,input().split())
A = list(map(int,input().split()))
B = [0 for _ in range(N+1)]
for i in range(1,N+1):
    B[i] = B[i-1]+A[i-1]
C = A[:]
for i in range(N-1):
    for j in range(i+1,N):
        C.append(B[j+1]-B[i])
C = sorted(C,reverse=True)
n = math.ceil(math.log2(C[0]))
Cb = []
for i in range(len(C)):
    x = bin(C[i])[2:]
    k = len(x)
    x = "0"*(n-k)+x
    Cb.append(x)
inds = [i for i in range(len(C))]
nlow = 0
while len(inds)>K:
    for j in range(nlow,n):
        E = []
        cnt = 0
        for i in inds:
            if Cb[i][j]=="1":
                E.append(i)
                cnt += 1
        if cnt>=K:
            inds = E[:]
            nlow = j+1
            break
    if cnt<K:break
cnt = int("1"*n,2)
for i in inds: 
    cnt = cnt&C[i]
print(cnt)

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

csum = [0 for _ in range(N+1)]
for i in range(N):
  csum[i+1] = csum[i] + A[i]

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


ans = 0
BB = B 
for i in range(41,-1,-1):
  BB_ = []
  for b in BB:
    if (b>>i)&1 == 1:
      BB_.append(b)
  if len(BB_) >= K:
    ans += (1<<i)
    BB = BB_
print(ans)

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

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

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

ret = 0
for i in range(40, -1, -1) :
    sub_ = []
    for sv in sub :
        if sv & (1 << i) :
            sub_.append(sv)
            
    if len(sub_) >= K :
        ret += (1 << i)
        sub = sub_
    
print(ret)

Solution (Python):


import bisect
n,k=map(int, input().split())
a_ = list(map(int, input().split()))

a = []

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

a.sort()

ans = 0
for j in range(41,-1,-1):
    x = 1<<j
    t = bisect.bisect_left(a,x)
    if t<=len(a)-k:
        ans+=x
        a = a[t:]
        """
        for i in range(len(a)):
            a[i]&=(x-1)
        print(a)
        """
    for i in range(len(a)):
        a[i]&=(x-1)
        
    a.sort()
print(ans)

Solution (Python):
from fractions import gcd
from datetime import date, timedelta
from heapq import*
import math
from collections import defaultdict, Counter, deque
import sys
from bisect import *
import itertools
import copy
sys.setrecursionlimit(10 ** 7)
MOD = 10 ** 9 + 7


def main():
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    
    rui = [a[0]]
    for i in range(1, n):
        rui.append(rui[i - 1] + a[i])
    rui = [0] + rui
    L = []
    for i in range(n):
        for j in range(i,n):
            L.append(rui[j + 1] - rui[i])

    ans = 0
    for i in range(51):
        bit = (1 << (50 - i))
        LL = []
        cnt = 0
        for j in range(len(L)):
            if L[j] & bit != 0:
                cnt+=1
                LL.append(L[j])

        if cnt >= k:
            L = LL
            ans |= bit

        if len(L) < k:
            break
                

    
    print(ans)
    
                    


if __name__ == '__main__':
    main()

Solution (Python):
from fractions import gcd
from datetime import date, timedelta
from heapq import*
import math
from collections import defaultdict, Counter, deque
import sys
from bisect import *
import itertools
import copy
sys.setrecursionlimit(10 ** 7)
MOD = 10 ** 9 + 7


def main():
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    
    rui = [a[0]]
    for i in range(1, n):
        rui.append(rui[i - 1] + a[i])
    rui = [0] + rui
    ansl = []
    for i in range(n):
        for j in range(i,n):
            ansl.append(rui[j + 1] - rui[i])
    ansl = sorted(ansl, reverse=True)
    
    cnt = 0
    ansbit = 0
    L = []
    for i in range(51):
        bit = (1 << (50 - i))
        l = []
        for j in range(len(ansl)):
            if ansl[j] & bit == 0:
                l.append(ansl[j])
            else:
                L.append(ansl[j])
                cnt += 1
        if cnt >= k:
            ansbit = i
            break
        else:
            L = []
        

    ans = 0
    for i in range(51):
        bit = (1 << (50 - i))
        LL = []
        cnt = 0
        for j in range(len(L)):
            if L[j] & bit != 0:
                cnt+=1
                LL.append(L[j])

        if cnt >= k:
            L = LL
            ans |= bit

        if len(L) < k:
            break
                

    
    print(ans)
    
                    


if __name__ == '__main__':
    main()

Solution (Python):

import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(100000)
input = sys.stdin.readline
INF = 2**62-1


def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, K, A):

    sa = [0]
    for a in A:
        sa.append(sa[-1]+a)
    
    s = []
    for i in range(N+1):
        for j in range(i+1, N+1):
            s.append(sa[j]-sa[i])
    s.sort()


    pool = s
    for b in range(s[-1].bit_length(), -1, -1):
        cnd = []
        v = 1 << b
        for a in pool:
            if a & v > 0:
                cnd.append(a)
        if len(cnd) >= K:
            pool = cnd

    return reduce(lambda x, y: x&y,  pool)

        




def main():
    N, K = read_int_n()
    A = read_int_n()
    print(slv(N, K, A))


if __name__ == '__main__':
    main()

Solution (Python):

import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(100000)
input = sys.stdin.readline
INF = 2**62-1


def read_int():
    return int(input())


def read_int_n():
    return list(map(int, input().split()))


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, K, A):

    sa = [0]
    for a in A:
        sa.append(sa[-1]+a)
    
    s = []
    for i in range(N+1):
        for j in range(i+1, N+1):
            s.append(sa[j]-sa[i])
    s.sort()


    pool = s
    for b in range(s[-1].bit_length(), -1, -1):
        cnd = []
        v = 1 << b
        for a in pool:
            if a & v > 0:
                cnd.append(a)
        if len(cnd) >= K:
            pool = cnd

    return reduce(lambda x, y: x&y,  pool)

        




def main():
    N, K = read_int_n()
    A = read_int_n()
    print(slv(N, K, A))


if __name__ == '__main__':
    main()

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

a = list(map(int,input().split()))

s = [0]
q = []
r = [0] * 41

for i in range(N):

    s.append(s[-1] + a[i])

ans = 0

for i in range(N+1):

    for j in range(i):

        now = s[i]-s[j]
        q.append(now)

q.sort()
ans = 0

for i in range(41):

    i = 40 - i

    nq = []

    for j in range(len(q)):
        if q[j] & (2 ** i) > 0:
            nq.append(q[j])

    if len(nq) >= K:
        q = []
        ans += 2 ** i
        for i in nq:
            q.append(i)

print (ans)

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

r = [0] * (N + 1)
for i in range(N):
  r[i + 1] = r[i] + l[i]
z = []
for i in range(N):
  for j in range(i, N):
    z.append(r[j + 1] - r[i])
z.sort()
def check(m):
  c = 0
  for x in z:
    if x & m == m:
      c += 1
  return c >= K
x = z[-1].bit_length()
res = 0
while x > 0:
  if check(res + pow(2, x - 1)):
    res += pow(2, x - 1)
  x -= 1
print(res)

Solution (Python):
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]
lrsums = []
for i in range(N):
  for j in range(i, N):
    lrsums.append(cs[j + 1] - cs[i])
lrsums.sort()

def check(m):
  c = 0
  for x in lrsums:
    if x & m == m:
      c += 1
  return c >= K
x = lrsums[-1].bit_length()
res = 0
while x > 0:
  if check(res + pow(2, x - 1)):
    res += pow(2, x - 1)
  x -= 1
print(res)

Solution (Python):
N, K = map(int, input().split())
A = list(map(int, input().split()))
AS = [0]
for i in range(N):
    AS += [AS[i] + A[i]]
AN = []
for i in range(N):
    for j in range(i, N):
        AN += [AS[j + 1] - AS[i]]
ans = 0
for i in range(41, -1, -1):
    c = 0
    p = ans + 2 ** i
    for x in AN:
        if p & x == p:
            c += 1
    if c >= K:
        ans += 2 ** i
print(ans)

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


cand = []
for i in range(N):
    sum = 0
    for j in range(i, N):
        sum += a[j]
        cand.append(sum)


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


ans = 0

for i in range(40, -1, -1):
    if (countMask(ans | (1 << i)) >= K):
        ans |= 1 << i

print(ans)

Solution (Python):
n,k = map(int,input().split())
a = list(map(int,input().split()))
c = [0]
x = 0
for i in a:
    x += i
    c.append(x)

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

data.sort()
ans = 0
for bit in range(45,-1,-1):
    new_data = []
    for d in data:
        if d>>bit&1:
            new_data.append(d)
    if len(new_data) >=k:
        data = new_data
        ans += 2**bit

print(ans)

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 6)
input = sys.stdin.readline

def main():
    n, k = map(int, input().split())
    aa = list(map(int, input().split()))
    ss = []
    for i in range(n):
        s = 0
        for j in range(i, n):
            s += aa[j]
            ss.append(s)
    ss.sort(reverse=True)
    
    msk = 1 << 50
    ans = 0
    for _ in range(50):
        msk >>= 1
        
        if msk > ss[0]: continue
        pen = []
        for s in ss:
            if s & msk:
                pen.append(s)
        if len(pen) >= k:
            ss = pen
            ans += msk
    print(ans)

main()

Solution (Python):
N,K = [int(x) for x in input().split()]
A = [int(x) for x in input().split()]
cumu = [0]
for i in A:
    cumu.append(cumu[-1]+i)
cand = []


for i in range(N):
    for j in range(N+1):
        if(i>=j): continue
        cand.append(cumu[j]-cumu[i])


ans = 0
for i in range(61,-1,-1):
    li = []
    for j in cand:
        if((j>>i&1)==1):
            li.append(j)
    if(len(li)>=K):
        ans += 1<<i
        cand = li
print(ans)

Solution (Python):
from itertools import accumulate
from functools import reduce

n,k = map(int, input().split())
a = list(map(int, input().split()))
acc = [0] + list(accumulate(a))
be = set()

for i in range(n):
    for j in range(i+1,n+1):
        if acc[j] - acc[i] not in be:
            be.add(acc[j] - acc[i])

be = list(be)
mx = max(be)

bin_dic = {i:[] for i in range(len(str(bin(mx)))-2)}

for b in be:
    for i,c in enumerate(str(bin(b))[-1:1:-1]):
        if c == '1':
            bin_dic[i].append(b)
mx_bin = 0
for i in bin_dic.keys():
    if k <= len(bin_dic[i]) and mx_bin < i:
        mx_bin = i
cands = set(bin_dic[mx_bin])
for i in range(mx_bin)[::-1]:
    tmp = set(bin_dic[i]) & cands
    if len(tmp) == k:
        cands = tmp
        break
    elif len(tmp) > k:
        cands = tmp
print(reduce(lambda x,y:x&y, cands))

Solution (Python):
N, K = map(int,input().split())
A = list(map(int,input().split()))
X = []
for l in range(N):
  r = l
  v = 0
  while r < N:
    v += A[r]
    X.append(v)
    r += 1
ans = 0
for i in range(60, -1, -1):
  Y = [ x for x in X if (x >> i & 1) == 1 ]
  if len(Y) >= K:
    ans += 1 << i
    X = Y
print(ans)

Solution (Python):
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])

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

B=[]
for i in range(len(A)):
    B.append(format(A[i],'040b'))



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)
        



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)

Solution (Python):
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])

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

B=[]
for i in range(len(A)):
    B.append(format(A[i],'040b'))



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)
        



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)

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

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

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

Solution (Python):
from collections import Counter, defaultdict
import sys
sys.setrecursionlimit(10 ** 5 + 10)

from math import factorial
import heapq, bisect
import math
import itertools


import queue
from collections import deque



def main():
    num, k = map(int, input().split())
    data = list(map(int, input().split()))

    a_list = [0 for i in range(((num + 1) * num) // 2)]
    ind = 0
    for i in range(num):
        for j in range(i, num):
            if j == i:
                a_list[ind] = data[j]
            else:
                a_list[ind] = a_list[ind - 1] + data[j]
            ind += 1

    ans = 0
    now_x = 2 ** 40

    for i in range(41):
        kari = ans + now_x
        count = 0
        for i in range(len(a_list)):
            if a_list[i] & kari == kari:
                count += 1

        if count >= k:
            ans = kari
        now_x //= 2

    print(ans)



if __name__ == '__main__':
    main()

Solution (Python):
NDIGIT=50
N,K=map(int,input().split())
A=list(map(int,input().split()))
acc=[0]*(N+1)
tmp=0
for i in range(N):
    tmp+=A[i]
    acc[i+1]=tmp

nums=[]
for i in range(N):
    for j in range(i+1,N+1):
        n=acc[j]-acc[i]
        s=bin(n)[2:]
        s="0"*(NDIGIT-len(s))+s
        nums.append(s)
        
ans=[]
for i in range(NDIGIT):
    tmp = [s for s in nums if s[i]=="1"]
    if len(tmp)>=K:
        ans.append("1")
        nums=tmp
    else:
        ans.append("0")

print(int("".join(ans),2))

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

sums = []
for i in range(n):
    b = a[i]
    sums.append(b)
    for j in range(i+1,n):
        b+=a[j]
        sums.append(b)
sums.sort()
b = [format(x,'040b') for x in sums]

for i in range(40):
    one = []
    for j in range(len(b)):
        if b[j][i]=='1':
            one.append(b[j])
    if len(one)>=k:
        b = []+one

ans = int(b[0],2)
for i in range(1,len(b)):
    ans = ans&int(b[i],2)
print(ans)

Solution (Python):
N,K=map(int,input().split())
A=list(map(int,input().split()))
a=[0]*(N+1)
for i in range(N):
    a[i+1]=a[i]+A[i]
from itertools import combinations
S=[a[r]-a[l] for l,r in combinations(range(N+1),2)]
x,y=0,1<<39
for i in range(40):
    n=0
    for s in S:
        if (x+y)&s==x+y:
            n+=1
    if n>=K:
        x+=y
    y>>=1
print(x)

Solution (Python):

N,K = map(int,input().split())
A = list(map(int,input().split()))
rui = [0]

for i in range(N):
    if i == 0:
        rui.append(A[0])
    else:
        rui.append(rui[-1]+A[i])
    
bubun = []
for i in range(N):
    I = i
    for j in range(N-i):
        J = i+j+1
        bubun.append(rui[J]-rui[I])
    
bubun.sort(reverse=True)
m = 40
MAX = 0
kari = 0
while m > 0:
    kari = 2**(m-1)
    count = 0
    for b in bubun:
        if (MAX+kari)&b == MAX+kari:
            count+=1
            
    if count >= K:
        MAX+=kari
    m-=1
    
print(MAX)

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

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

beauty = []
for left in range(N):
    for right in range(left + 1, N + 1):
        b = cumA[right] - cumA[left]
        beauty.append(b)

ans = 0
M = max(beauty).bit_length()

for digit in range(M)[:: -1]:
    new = ans + (1 << digit)
    cnt = 0
    for b in beauty:
        if b & new == new:
            cnt += 1
    if cnt >= K:
        ans = new

print(ans)

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

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

beauty = []
for left in range(N):
    for right in range(left + 1, N + 1):
        b = cumA[right] - cumA[left]
        beauty.append(b)

ans = 0
M = max(beauty).bit_length()

for digit in range(M)[:: -1]:
    new = ans + (1 << digit)
    cnt = 0
    for b in beauty:
        if b & new == new:
            cnt += 1
    if cnt >= K:
        ans = new

print(ans)

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

sys.setrecursionlimit(10 ** 7)

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x) - 1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

n, k = li()
a = list(li())

cum = [0]
for ai in a:
    cum.append(cum[-1] + ai)

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

MAX_BIT = 41
ans = 0
for bit in range(MAX_BIT, -1, -1):
    new_pool = []
    for poli in pool:
        if poli & (1<<bit):
            new_pool.append(poli)

    if len(new_pool) >= k:
        ans += (1<<bit)
        pool = new_pool

print(ans)

Solution (Python):
n,k = map(int, input().split())
a = list(map(int, input().split()))
b = []
for i in range(n):
    for j in range(i, n):
        b.append(sum(a[i:j+1]))
assert(len(b) == n*(n+1)//2)

b.sort(reverse=True)
m = max(b).bit_length()
mask = 0
for i in range(m-1, -1, -1):
    mask += 1<<i
    cnt = 0
    for j in b:
        if j < mask:
            break
        if j&mask == mask:
            cnt += 1
    if cnt < k:
        mask -= 1<<i
print(mask)

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

B = []
for l in range(N):
    b = 0
    for r in range(l, N):
        b += A[r]
        B.append(b)
B.sort(reverse=True)


def trans2(n):
    A = bin(n)
    return str(A[2:])

C = []
maxl = 0
for i, b in enumerate(B):
    l = len(trans2(b))
    if i+1 <= K:
        maxl = l
    if i+1 > K and maxl != l:
        break
    C.append(trans2(b))

OK = [1 for _ in range(len(C))]

ans = 0
for l in range(maxl-1, -1, -1):
    update = []
    for i, c in enumerate(C):
        if c[-l-1] == '0' and OK[i] == 1:
            update.append(i)
            OK[i] = 0
    oks = sum(OK)
    if oks < K:
        for u in update:
            OK[u] = 1
    else:
        ans += 2**l
print(ans)

Solution (Python):
import os
import sys

import numpy as np

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(2147483647)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7

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

A = np.array(A, dtype=int)

cs = A.cumsum()
nums = list(cs)
for i in range(1, N):
    nums.extend(cs[i:] - cs[i - 1])
nums.sort(reverse=True)
nums = np.array(nums, dtype=int)

ans = 0
d = int(nums[K - 1]).bit_length() - 1
while d >= 0:
    mask = 1 << d
    idx = (nums & mask).nonzero()[0]
    if len(idx) >= K:
        nums = nums[idx]
        ans += mask
    d -= 1
print(ans)

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


li = []
for i in range(n):
    num = 0
    for j in range(i, n):
        num += a[j]
        li.append(num)

ans = 0
for i in range(40)[::-1]:
    bit = 2 ** i
    new_li = [num for num in li if num&bit != 0]
    if len(new_li) >= k:
        li = new_li[0:]
        ans += bit
print(ans)

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

ruiseki = [0] * (n + 1)
for i in range(n):
    ruiseki[i+1] = ruiseki[i] + a[i]

li = []
for i in range(n):
    for j in range(i, n):
        tmp = format(ruiseki[j + 1] - ruiseki[i], '040b')
        li.append(tmp)

for i in range(40):
    cnt = 0
    new_li = []
    for j in range(len(li)):
        if li[j][i] == "1":
            new_li.append(li[j])
            cnt += 1
    if cnt >= k:
        li = new_li[0:]

ans = 2**41 -1
for i in range(k):
    ans = ans & int(li[i], 2)
print(ans)

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

ruiseki = [0] * (n + 1)
for i in range(n):
    ruiseki[i+1] = ruiseki[i] + a[i]

li = []
for i in range(n):
    for j in range(i, n):
        tmp = format(ruiseki[j + 1] - ruiseki[i], '050b')
        li.append(tmp)

for i in range(50):
    cnt = 0
    new_li = []
    for j in range(len(li)):
        if li[j][i] == "1":
            new_li.append(li[j])
            cnt += 1
    if cnt >= k:
        li = new_li[0:]

ans = 2**50 -1
for i in range(k):
    ans = ans & int(li[i], 2)
print(ans)

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

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

import numpy as np

N,K = map(int,input().split())
A = np.array(input().split(), dtype=np.int64)

Acum = A.cumsum()
range_sums = Acum[:,None] - Acum[None,:] + A[None,:]

range_sums = range_sums.flatten()
range_sums = range_sums[range_sums>0]

def test(x):
    
    return np.sum(range_sums & x == x) >= K

left = 0 
right = 1<<40 
while left+1 < right:
    mid = (left+right)//2
    if test(mid):
        left = mid
    else:
        right = mid
answer = left
print(answer)

Solution (Python):
from heapq import heappush, heappop
from collections import deque
import itertools
from itertools import permutations
import sys

import bisect
sys.setrecursionlimit(10**6)
def SI():
    return input().split()
def MI():
    return map(int,input().split())
def I():
    return int(input())
def LI():
    return [int(i) for i in input().split()]
YN=['Yes','No']
YNeos='YNeos'
GYN=['Yes','trumpet']
mo=10**9+7
imp='IMPOSSIBLE'

n,k=MI()
a=LI()
A=[0]
for i in a:
    A+=[A[-1]+i]
b=[]
for i in range(n+1):
    for j in range(i+1,n+1):
        b+=[A[j]-A[i]]
        
c=sorted(b)
ans=0
i=0
i2=2**40
while i2>0:

    ans+=i2 if sum([(j&(ans+i2))==(ans+i2) for j in c])>=k else 0
    i2//=2
    i+=1
    
print(ans)

Solution (Python):



import sys
import math
import bisect
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n,k = LI()
a = LI()
ans = 0
s = [0] + list(accumulate(a))
lst = []
for i in range(n):
    for j in range(i+1,n+1):
        lst.append(s[j] - s[i])
ans = 0
for b in range(40)[::-1]:
    num = sum([(i>>b)&1 for i in lst])
    if num >= k:
        ans += 1 << b
        lst = [i for i in lst if (i>>b) & 1]
print(ans)

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


vC = [0,]
for a in vA:
    vC.append(a + vC[-1])


vB = []
for i in range(len(vC)):
    vB.extend([vC[i+j+1] - vC[i] for j in range(N-i)])


blenB = max(vB).bit_length()



x = 0
for i in reversed(range(blenB+1)):
    x_new = x + (1<<i)
    if sum((x_new & b) == x_new for b in vB) >= K:
        x = x_new

print(x)

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

from itertools import accumulate
l=[]
for i in range(n):
    l+=list(accumulate(a[i:]))

ans=0
for i in range(40,-1,-1):
    j=2**i
    temp=[k for k in l if k&j!=0]
    if len(temp)>=k:
        l=temp
        ans+=j
        
print(ans)

Solution (Python):
N,K = map(int,input().split())
a = list(map(int,input().split()))
A = []
for l in range(N):
    ans = 0
    for r in range(l,N):
        ans += a[r]
        A.append(ans)
bi = 0
while 2**bi <= max(A):
    bi += 1
bi -= 1

def funk(x,L):
    if x == -1:
        return L
    M = []
    b = 2**x
    for i in L:
        if i&b == b:
            M.append(i)
    if len(M) >= K:
        return funk(x-1,M)
    else:
        return funk(x-1,L)

B = funk(bi,A)
ans = B[0]
for i in B[:K]:
    ans &= i
print(ans)

Solution (Python):
class cumulative_sum:
    def __init__(self,A):
        N = len(A)
        self.S = [0]*(N+1)
        for i in range(N):
            self.S[i+1] = self.S[i] + A[i]
    def get(self,i,j=None):
        if j==None:j=len(self.S)-1
        if j<=i:return 0
        return self.S[j]-self.S[i]
def main():
    N,K = map(int,input().split())
    *A, = map(int,input().split())
    SA = cumulative_sum(A)
    B = {}

    for i in range(N):
        for j in range(i+1,N+1):
            s = SA.get(i,j)
            B[s] = B.get(s,0)+1

    ans = 0
    for i in range(40,-1,-1):
        x = ans + (1<<i)
        c = sum(n for b,n in B.items() if b&x==x)
        if c>=K:ans = x
    print(ans)
main()

Solution (Python):
import sys
from collections import deque, defaultdict
import copy
input=sys.stdin.readline
sys.setrecursionlimit(10 ** 9)

N, K = list(map(int, input().split()))
a = list(map(int, input().split()))
sum_a = [0, a[0]]
for i in range(1, N):
    sum_a.append(sum_a[-1]+a[i])

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

max_bit = 2
while True:
    if sum_a[-1] < 2**max_bit:
        break
    max_bit += 1

sum = 0
for i in range(max_bit ,-1, -1):
    kouho = []
    for n in bubunwa:
        if n & 2**i == 2**i:
            kouho.append(n)
    if len(kouho) >= K:
        bubunwa = copy.deepcopy(kouho)
        sum += 2**i

print(sum)

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

sum_list=[]
for i in range(N):
  s=0
  for j in range(i,N):
    s+=alist[j]
    sum_list.append(s)
    
sum_list.sort(reverse=True)


answer=0
for i in reversed(range(41)):
  x=2**i
  cnt=0
  for s in sum_list:
    if answer+x>s:
      break
    elif (answer+x)&s==answer+x:
      cnt+=1
      if cnt>=K:
        answer+=x
        break
  
print(answer)