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()))

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

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

ans = 0
for i in reversed(range(40)):
    b = ans + 2**i
    cnt = 0
    for c in cond:
        if c&b==b:
            cnt += 1
    if cnt>=K:
        ans = b

print(ans)

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

B=[0]
for a in arr:
    B.append(B[-1]+a)

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

ans=0
for i in range(40,-1,-1):
    log=2**i
    tmp=[x for x in C if x&log==log]
    if len(tmp)>=K:
        ans+=log
        C=tmp
print(ans)

Solution (Python):
line = str(input())
lines = line.split(' ')
 
n = int(lines[0])
k = int(lines[1])
 
d = list()
 
line = str(input())
lines = line.split(' ')
 
for l in lines:
    d.append(int(l))
 
ds = list()
 
for i in range(0,n):
    sum = 0
    for j in range(i,n):
        sum += d[j]
        ds.append(sum)
 
for i in range(40,-1,-1):
    count = 0
    o = []
 
    for dss in ds:
        if dss & (1 << i) > 0 :
            count += 1
            o.append(dss)
 
    if count >= k :
        ds = list(o)
 
sum = ds[0]
for i in range(1,k):
    sum = sum & ds[i]
 
print(sum)

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

ds = []
 
for i in range(0,n):
    sum = 0
    for j in range(i,n):
        sum += d[j]
        ds.append(sum)

ds.sort(reverse=True)
 
for i in range(40,-1,-1):
    count = 0
    o = []
 
    for dss in ds:
        if dss & (1 << i) > 0 :
            count += 1
            o.append(dss)
    if count >= k :
        ds = list(o)
  
sum = ds[0]
for i in range(1,k):
    sum = sum & ds[i]
 
print(sum)

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

ds = []

for i in range(0,n):
    sum = 0
    for j in range(i,n):
        sum += d[j]
        ds.append(sum)
        
    

ds.sort(reverse=True)

sum = 0
for i in range(40,-1,-1):
    count = 0
    o = []
    for dss in ds:
        if dss & (1 << i) > 0 :
            count += 1
            o.append(dss)

    if count >= k :
        sum += 1 << i
        ds = o

print(sum)

Solution (Python):
import itertools

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

for i in range(N):
    temp = 0
    for n in a[i:]:
        temp += n
        beautiful_num.append(temp)

ma_b_cnt = 0
target_set = None

result = 0
for i in range(40, -1, -1):
        for b in beautiful_num:
            if b & 1 << i != 0:
                ma_b_cnt += 1
        if ma_b_cnt >= K:
            result += 2**i
            beautiful_num = [b for b in beautiful_num if b & 1 << i != 0]
        ma_b_cnt = 0

print(result)

Solution (Python):
import itertools

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

for i in range(N):
    temp = 0
    for n in a[i:]:
        temp += n
        beautiful_num.append("{:040b}".format(temp))

ma_b_cnt = 0
target_set = None

result = 0
for i in range(40):
        for b in beautiful_num:
            if b[i] == "1":
                ma_b_cnt += 1
        if ma_b_cnt >= K:
            result += 2**(40-i-1)
            beautiful_num = [b for b in beautiful_num if b[i] == "1"]
        ma_b_cnt = 0

print(result)

Solution (Python):
a = input().split()
a = [int(i) for i in a]
b = input().split()
b = [int(i) for i in b]

def cao(aller, i):
	dicter = dict()
	for m in aller:
		for n in m:
			if n < i:
				try:
					dicter[n].append(m)
				except:
					dicter[n] = [m]
	for i in sorted(dicter)[::-1]:
		if len(dicter[i]) >= a[1]:
			return dicter[i], i
	return [], -1
	
aller = [0]
sumer = 0
for i in b:
	sumer += i
	aller.append(sumer)
	
superlist = dict()
for i in range(len(b)):
	for j in range(i + 1, len(b) + 1):
		temp = bin(aller[j] - aller[i])[::-1]
		x = []
		for k in range(len(temp)):
			if temp[k] == '1':
				x.append(k)
		for m in x:
			try:
				superlist[m].append(x)
			except:
				superlist[m] = [x]
						
for i in sorted(superlist)[::-1]:
	if len(superlist[i]) >= a[1]:
		li = superlist[i]
		sumer = 2 ** i
		gan1, gan2 = cao(li, i)
		while gan2 != -1:
			sumer += 2**gan2
			gan1,gan2 = cao(gan1,gan2)
		print(sumer)
		break

Solution (Python):

"""
Created on Sat Nov 24 20:12:29 2018

@author: maezawa
"""

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

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

l=0
ndim = n*(n+1)//2
beauty = [] 
for i in range(1,n+1):
    for j in range(i,n+1):
        b = accum[j] - accum[i-1]        
        beauty.append(b)

beauty.sort(reverse=True)

ans = 0
kgroup = beauty[:]
nextkgroup = []
bt = 40
while True:
    cnt = 0
    btn = 1 << bt
    for i in kgroup:
        if i & btn != 0:
            nextkgroup.append(i)
            cnt += 1
    if cnt >= k:
        ans = ans | btn
        kgroup = nextkgroup[:]
    nextkgroup = []
    bt -= 1
    if bt < 0:
        break
    
print(ans)

Solution (Python):
N,K=map(int,input().split())
 
a=list(map(int,input().split()))
 
ruisekia=[]
keta=[0]*30
for i in range(N):
  temp=0
  for j in range(i,N):
    temp+=a[j]
    ruisekia.append(temp)
ruisekia.sort(reverse=True)
 

answer=0
for i in range(40,-1,-1):
  count=0
  ruiseki=[]
  for j in ruisekia:
    if j&(1<<i)==1<<i:
      count+=1
      ruiseki.append(j)
  if count>=K:
    answer+=2**i
    ruisekia=ruiseki

print(answer)
'''
for i in range(len(ruisekia)):
  tempo=ruisekia[i]
  while(tempo!=0):
    x=1
    count=0
    while(tempo>=x*2):
      x*=2
      count+=1
    keta[count]+=1
    tempo-=x
print(keta)
'''

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

ans=0
for i in range(40,-1,-1):
    n2=2**i
    c2=[i for i in c if i&n2==n2]
    if len(c2)>=k:
        ans+=n2
        c=c2
print(ans)

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

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

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

for i in range(40,-1,-1):
    COUNT=0
    for s in SUM:
        if (s & 1<<i)!=0:
            COUNT+=1

    if COUNT<K:
        continue
    else:
        NLIST=[]
        for s in SUM:
            if (s & 1<<i)!=0:
                NLIST.append(s)

        SUM=NLIST

ANS=SUM[0]
for s in SUM:
    ANS=ANS & s

print(ANS)

Solution (Python):
import math

def gcd(a,b):
    """Compute the greatest common divisor of a and b"""
    while b > 0:
        a, b = b, a % b
    return a
 
def lcm(a, b):
    """Compute the lowest common multiple of a and b"""
    return a * b / gcd(a, b)
 

def f(b,n):
	if n < b:
		return n
	return f(b, n//b) + n%b
 

N, K = map(int,input().split())
v = list(map(int,input().split()))
bubunwa = [0]

for i in range(len(v)):
	bubunwa.append(v[i]+bubunwa[-1])

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

ans = 0
good = []
for i in range(40,-1,-1):

	bit = 1 << i

	for elem in sums:
		if elem & bit == bit:
			good.append(elem)

	if len(good) >= K:
		ans += bit
		sums = good
		good = []
	else:
		good = []

print(ans)

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

A_sum = [0]
[A_sum.append(A_sum[-1]+int(a)) for a in input().split()]

score = []
for r in range(1,N+1):
    for l in range(0,r):
        score.append(A_sum[r]-A_sum[l])

ans = 0
for i in range(40,-1,-1):
    count = 0
    score_a = []
    for j in score:
        if (j & (1<<i)):
            count += 1
            score_a.append(j)
    if count >= K:
        ans += (1<<i)
        score = [i for i in score_a]

print(ans)

Solution (Python):
n, k = [int(v) for v in input().split()]
a = [int(v) for v in input().split()]

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

MAX = 40
ans = 0
for t in range(MAX, 0, -1):
    x = ans + (1 << (t-1))
    cnt = 0
    for v in b:
        if x & v == x:
            cnt += 1
    if cnt >= k:
        ans = x

print(ans)

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

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

score = []
for r in range(1,N+1):
    for l in range(0,r):
        score.append(A_sum[r]-A_sum[l])

ans = 0
for i in range(40,-1,-1):
    count = 0
    score_a = []
    for j in score:
        if (j & (1<<i)):
            count += 1
            score_a.append(j)
    if count >= K:
        ans += (1<<i)
        score = [i for i in score_a]

print(ans)

Solution (Python):
import sys

N,K = [int(x) for x in input().split()]
A = [int(x) for x in input().split()]

C = []

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

D = C
ans = 0
for i in range(40,-1,-1):
    yes = []
    no = []
    for l in D:
        if l & (1 << i):
            yes.append(l)
        else:
            no.append(l)

    if len(yes) >= K:
        ans += 1 << i
        D = yes

print(ans)

sys.exit(0)

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

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

score = []
for r in range(1,N+1):
    for l in range(0,r):
        score.append(A_sum[r]-A_sum[l])

ans = 0
for i in range(40,-1,-1):
    count = 0
    score_a = []
    for j in score:
        if (j & (1<<i)):
            count += 1
            score_a.append(j)
    if count >= K:
        ans += (1<<i)
        score = copy.deepcopy(score_a)

print(ans)

Solution (Python):

from pprint import pprint
import itertools

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

rui = [0]
tmp = 0
for a in an:
    rui.append(tmp+a)
    tmp += a

sum_list = [] 

for l in range(1,N+1):
    for r in range(l,N+1):
        tmp = rui[r] - rui[l-1]
        sum_list.append(tmp)

max_val = max(sum_list)
max_2pow = len(bin(max_val)[2:])
ans = 0

while max_2pow > 0:
    tmp = ans + 2**(max_2pow-1)
    if sum([1 for v in sum_list if v & tmp == tmp]) >= K:
        ans += 2**(max_2pow-1)
    max_2pow -= 1

print(ans)

Solution (Python):
from math import log, ceil

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




cs = [0]
t = 0
for aa in a:
    t += aa
    cs.append(t)

ps = []
for r in range(1, n + 1):
    for l in range(r):
        ps.append(cs[r] - cs[l])

MAX_BIT_LENGTH = ceil(log(max(ps), 2))

ans = 0
for b in range(MAX_BIT_LENGTH)[::-1]:
    cnt = 0
    t = ans + (1 << b)
    for p in ps:
        if p & t == t:
            cnt += 1
            if cnt == k:
                ans = t
                break

print(ans)

Solution (Python):
from math import log, ceil

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

A_MAX = 10 ** 9
MAX_BIT_LENGTH = ceil(log(A_MAX * n, 2))

cs = [0]
t = 0
for aa in a:
    t += aa
    cs.append(t)

ps = []
for r in range(1, n + 1):
    for l in range(r):
        ps.append(cs[r] - cs[l])

ans = 0
for b in range(MAX_BIT_LENGTH)[::-1]:
    cnt = 0
    t = ans + (1 << b)
    for p in ps:
        if p & t == t:
            cnt += 1
            if cnt == k:
                ans = t
                break

print(ans)

Solution (Python):
a,b = map(int,input().split())

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

aa = []
aa.append(0)
aa.append(c[0])

l = []
for i in range(1,a):
	aa.append(aa[i]+c[i])
d = 0
p = len(aa)
for i in range(p):
	for j in range(i+1,p):
		l.append(aa[j]-aa[i])
co = 0
la = 0
for i in range(40,-1,-1):
	for j in range(len(l)):
		if l[j] & (la + 2 ** i) == (la + 2 ** i):
			co += 1
	if co >= b:
		la += (2 ** i)
	co = 0

print(la)

Solution (Python):
n, k = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]

B = [0]

for a in A:
    B.append(B[-1] + a)

C = []

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

ans = 0
flag = False
D = []

for i in range(40, -1, -1):
    if flag:
        C = D
        flag = False
    D = []
    bit = 1 << i
    for c in C:
        if bit & c:
            D.append(c)
    if len(D) >= k:
        flag = True
        ans += bit
    
print(ans)

Solution (Python):
from collections import defaultdict
from itertools import accumulate

n, k = map(int, input().split())
aaa = list(map(int, input().split()))
acc = [0] + list(accumulate(aaa))
subs = defaultdict(lambda: 0)

for l in range(n):
    acl = acc[l]
    for r in range(l + 1, n + 1):
        beu = acc[r] - acl
        subs[beu] += 1

ans = 0

for i in range(40):
    bit = 1 << (39 - i)
    new_subs = {}
    remain = 0

    for sub, cnt in subs.items():
        if sub & bit > 0:
            new_subs[sub] = cnt
            remain += cnt

    if remain >= k:
        subs = new_subs
        ans |= bit

print(ans)

Solution (Python):
import bisect
N, K = map(int, input().split())
A = list(map(int, input().split()))
S = [sum(A[:i]) for i in range(N + 1)]
sum_A = [S[r] - S[l] for l in range(N + 1) for r in range(l + 1, N + 1) if r != l]
sum_A.sort()

def f(m, A):
    if m < 0:
        return 0

    T = len(A)
    M = pow(2, m)
    r = bisect.bisect_left(A, M)
    num = T - r
    if num < K:
        return f(m - 1, sorted([a % M for a in A]))
    else:
        return M + f(m - 1, sorted([a % M for a in A[r:]]))

print(f(len(bin(sum_A[-1])) - 2, sum_A))

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

def range_sum(cuml_list, l, r):
    if l == 0:
        return cuml_list[r]
    else:
        return cuml_list[r] - cuml_list[l - 1]


N, K = [int(elem) for elem in fin().split()]
a_list = [int(elem) for elem in fin().split()]
cuml_list = []
current_sum = 0
for a in a_list:
    current_sum += a
    cuml_list.append(current_sum)

all_beautifulness = []
for l in range(N):
    for r in range(l, N):
        cuml_sum = range_sum(cuml_list, l, r)
        all_beautifulness.append(cuml_sum)

assert len(all_beautifulness) == N*(N+1) // 2

current_num = 0
for t in range(40, 0, -1):
    count = 0
    comp = current_num + 2**(t - 1)
    for b in all_beautifulness:
        if comp & b == comp:
            count += 1
    if count >= K:
        current_num += 2**(t - 1)
print(current_num)

Solution (Python):

n,k  = map(int,input().split())
ls_a  = list(map(int,input().split()))
target = int(k*(k+1)/2)
ls_target = []
i = 1
while(1):
    for j in range(i):
        temp = ls_a[j:(len(ls_a)-i+j+1)]
        ls_target.append(sum(temp))
    if i==n:
        break
    i+=1

def check(num):
    for i in range(50):
        if (num < 2**i):
            return i-1, 2**(i-1)
mx = ls_target[0]
digit, mx_bin = check(mx)
ans = 0
for d in range(digit+1):


    new_idx = []
    cnt = 0
    for idx,ls in enumerate(ls_target):
        if ls & mx_bin > 0:
            cnt += 1
            new_idx.append(idx)
    if cnt >= k:
        ls_target = [ls_target[i] for i in new_idx]
        ans += mx_bin
    mx_bin = int(mx_bin/2)
print (ans)

Solution (Python):

n,k  = map(int,input().split())
ls_a  = list(map(int,input().split()))
target = int(k*(k+1)/2)
ls_target = []
i = 1
while(1):
    for j in range(i):
        temp = ls_a[j:(len(ls_a)-i+j+1)]
        ls_target.append(sum(temp))
    if i==n:
        break
    i+=1

def check(num):
    for i in range(50):
        if (num < 2**i):
            return i-1, 2**(i-1)
mx = ls_target[0]
digit, mx_bin = check(mx)
ans = 0
for d in range(digit+1):


    new_idx = []
    cnt = 0
    for idx,ls in enumerate(ls_target):
        if ls & mx_bin > 0:
            cnt += 1
            new_idx.append(idx)
    if cnt >= k:
        ls_target = [ls_target[i] for i in new_idx]
        ans += mx_bin
    mx_bin = int(mx_bin/2)
print (ans)

Solution (Python):

n,k  = map(int,input().split())
ls_a  = list(map(int,input().split()))
target = int(k*(k+1)/2)
ls_target = []
i = 1
while(1):
    for j in range(i):
        temp = ls_a[j:(len(ls_a)-i+j+1)]
        ls_target.append(sum(temp))
    if i==n:
        break
    i+=1

def check(num):
    for i in range(50):
        if (num < 2**i):
            return i-1, 2**(i-1)
mx = ls_target[0]
digit, mx_bin = check(mx)
ans = 0
for d in range(digit+1):


    new_idx = []
    cnt = 0
    for idx,ls in enumerate(ls_target):
        if ls & mx_bin > 0:
            cnt += 1
            new_idx.append(idx)
    if cnt >= k:
        ls_target = [ls_target[i] for i in new_idx]
        ans += mx_bin
    mx_bin = int(mx_bin/2)
print (ans)

Solution (Python):

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

N, K = inpl()
A = inpl()
tmp = 0
S = [0]*N
Q = []
for i in range(N):
    tmp += A[i]
    S[i] = tmp
    Q.append(tmp)

tmp = 0
for i in range(1, N):
    tmp = S[i-1]
    for j in range(i, N):
        
        v = S[j] -S[i-1]
        Q.append(v)

res = 0
L = len(Q)
I = len(bin(sum(A))) - 2
while True:
    ok = 0
    for i in range(I):
        num = res + 2**i
        tmp = 0
        for j in range(L):
            if num & Q[j] == num:
                tmp += 1
        if tmp >= K:
            ok = num

    I = len(bin(res - ok)) - 2
    if res < ok & ok > 0:
        res = ok
    else:
        break

print(res)

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

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

ans = 0
for m in reversed(range(40)):
    cnt = 0
    for i in range(N):
        for j in range(i+1, N+1):
            sum = ru[j] - ru[i]
            if (sum & ans) < ans:
                continue
            if sum & (1 << m):
                cnt += 1
    if cnt >= K:
        ans |= (1 << m)

print(ans)

Solution (Python):
import itertools
import numpy as np
def main():
    n,k = map(int, input().split())
    a = list(map(int, input().split()))

    x = [sum(a[i:j+1]) for i in range(n) for j in range(i,n)]
    x.sort()
    x = x[::-1]

    MAX = x[0]
    two = 1
    POW = 1
    while(MAX>=two*2):
        two*=2
        POW+=1


    for p in range(POW)[::-1]:
        C = list(filter(lambda y:y>=two,x))
        if len(C)>=k:
            break
        else:
            two//=2

    POW = p+1

    for p in range(POW):
        D = list(filter(lambda x:(x//two)%2==1,C))
        LEN = len(D)
        if LEN>=k:
            C = D[:]
            if LEN==k:break
        two//=2

    ans = C[0]
    for c in C[1:k]:
        ans &= c

    print(ans)

if __name__ == "__main__":
    main()

Solution (Python):
def getmaxand(bs, K):
    for k in range(40, -1, -1):
        bit = 1 << k
        avails = [v for v in bs if v & bit]
        if len(avails) < K:
            continue
        filt = ~(-1 << k)
        elims = [v & filt for v in avails]
        return (1 << k) + getmaxand(elims, K)
    return 0


N, K = map(int, input().split())
A = [int(s) for s in input().split()]

bs = []
for l in range(N):
    for r in range(l, N):
        bs.append(sum(A[l:r+1]))

print(getmaxand(bs, K))

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

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

B = []
for i in range(N):
    SUM = 0
    for j in range(i,N):
        SUM += A[j]
        B.append(SUM)
        
B = list(sorted(B,reverse=True))

MAX = B[0]
two = 1
POW = 1
while(MAX>=two*2):
    two*=2
    POW+=1



for p in range(POW)[::-1]:
    C = list(filter(lambda x:x>=two,B))
    if len(C)>=K:
        break
    else:
        two//=2
        
POW = p+1

for p in range(POW):
    D = list(filter(lambda x:(x//two)%2==1,C))
    
    LEN = len(D)
    if LEN>=K:
        C = D[:]
        if LEN==K:break
    two//=2

ans = C[0]
for c in C[1:K]:
    ans &= c
    
print(ans)

Solution (Python):

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

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

seq = []
for i in range(N):
    for sl in range(i+1):
        if sl == 0:
            seq.append(a[N-1-i+sl])
        else:
            seq.append(a[N-1-i+sl] - a[sl-1])

ans = 0
l = len(seq)
for i in range(50,-1,-1):
    cnt = 0
    s = set()
    for j in range(l):
        if seq[j] >= 0 and (seq[j] & (1<<i)) != 0:
            cnt += 1
            s.add(j)

    if cnt >= K:
        ans += (1 << i)
        for j in range(l):
            if j in s:
                seq[j] = seq[j] - (1 << i)
            else:
                seq[j] = -1


print(ans)

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

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

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







for i in range(40,-1,-1):
    COUNT=0
    for s in SUM:
        if (s & 1<<i)!=0:
            COUNT+=1

    

    if COUNT<K:
        continue
    else:
        NLIST=[]
        for s in SUM:
            if (s & 1<<i)!=0:
                NLIST.append(s)

        SUM=NLIST

    

ANS=SUM[0]
for s in SUM:
    ANS=ANS & s

print(ANS)

Solution (Python):
def solve(N, K, As):
    alls = []
    for i in range(N):
        for j in range(i + 1, N + 1):
            alls.append("{:041b}".format(sum(As[i:j])))
    def find(i, alls):
        if i > 40:
            return []
        ones = [a for a in alls if a[i] == "1"]
        if len(ones) < K:
            return find(i + 1, alls)
        elif len(ones) == K:
            return ones
        elif len(ones) > K:
            ret = find(i + 1, ones)
            if len(ret) == 0:
                return ones[0:K]
            else:
                return ret

    ans = find(0, alls)
    if len(ans):
        a = 0xFFFFFFFFFFF
        for p in ans:
            a &= int(p, 2)
        return a
    else:
        return 0

if __name__ == "__main__":
    l = input()
    N = int(l.split(" ")[0])
    K = int(l.split(" ")[1])
    As = [int(c) for c in input().split()]
    print(solve(N, K, As))

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

for i in range(n-1):
    a[i+1] += a[i]

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

ans = 0
for m in range(40, -1, -1):
    count = 0
    for i in range(len(b)):
        if ((b[i] >> m) & 1) == 1:
            count += 1
    if count >= k:
        ans += (1 << m)
        c = []
        for i in range(len(b)):
            if ((b[i] >> m) & 1) == 1:
                c.append(b[i])
        b = c

print(ans)

Solution (Python):
N, K = map(int, input().split())
a = list(map(int, input().split()))
b = []
for i in range(N):
	s = 0
	for j in range(i, N):
		s = s + a[j]
		b.append(s)
max_b = len(bin(max(b))) - 2

ans = 0
for i in range(max_b):
	p = 0
	c = 2 ** (max_b - 1 - i)
	
	for j in b:
		if (ans + c) & j == ans + c:
			
			p = p + 1
	if p >= K:
		ans = ans + 2 ** (max_b - 1 - i)
		
print(ans)

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

btyarr = [0 for i in range(N*(N+1)//2)]
cnt = 0
for i in range(N):
    bty = 0
    for j in range(i, N):
        bty += a[j]
        btyarr[cnt] = bty
        cnt += 1




l = len(btyarr)
s = 0
e = l
for i in range(40, -1, -1):
    b = 1 << i
    cnt = 0
    for j in range(s, e):
        if btyarr[j] & b:
            tmp = btyarr[s+cnt]
            btyarr[s+cnt] = btyarr[j]
            btyarr[j] = tmp
            cnt += 1
    if cnt >= K:
        e = s + cnt











ans = btyarr[s]
for i in range(K):
    ans &= btyarr[i]

print(ans)

Solution (Python):
import itertools
import bisect
n,K=map(int, input().split(" "))
A=list(map(int, input().split(" ")))

A2=[0]
sum=0
for i in A:
    A2.append(i+sum)
    sum+=i


A3=[]
for i in range(len(A2)):
    for k in range(len(A2)-i-1):
        A3.append(A2[k+1+i]-A2[i])
A3.sort()



tmparr=[]
for i in range(50):
    if 2**i-1>A3[-1]:
        break
    tmparr.append(2**i-1)



"""
for i in tmparr[::-1]:
    rem=len(A3)-bisect.bisect_right(A3,i)
    if rem>K:
        A4=A3[-rem:]
        break
"""
arr=A3
result=0
for i in tmparr[::-1]:
    count=0
    tmparr=[]
    for k in arr:
        if k & i+1 > 0:
            count+=1
            tmparr.append(k)
    if count>=K:
        arr=tmparr
        result+=i+1
print(result)

Solution (Python):
from itertools import accumulate
N, K = map( int, input().split())
A = list( map( int, input().split()))
accA = [0] + list( accumulate(A))
V = [1]*(N*(N+1)//2)
ans = 0
check = False
for i in range(40,-1,-1):
    W = [0]*(N*(N+1)//2)
    for l in range(N):
        for r in range(l+1,N+1):
            if V[l*(2*N+1-l)//2+r-l-1] == 0:
                continue
            if (accA[r]-accA[l])%(2**(i+1)) >= 2**i:
                W[l*(2*N+1-l)//2+r-l-1] = 1
    if check == False:
        if sum(W) >= K:
            check == True
        else:
            continue
    if sum( V and W) >= K:
        ans += 2**i
        V = V and W
print(ans)

Solution (Python):
N, K = map(int, input().split())
a = list(map(int, input().split()))
s = [0]
for i in range(N):
  s.append(s[i] + a[i])


b = []
for i1 in range(N):
  for i2 in range(i1+1, N+1):
    b.append(s[i2] - s[i1])

b.sort(reverse = True)


for sft in range(48):
  mask = 1 << (47 - sft)
  tmp_b = []
  for i in range(len(b)):
    if (b[i] & mask) != 0:
      tmp_b.append(b[i])
  
  if len(tmp_b) >= K:
    b = tmp_b


ans = b[0]
for i in range(1, K):
  ans &= b[i]

print(ans)

Solution (Python):
import math


def main():
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    if k == 1:
        print(sum(a))
        return
    cum = [0] * (len(a) + 1)
    for i in range(len(a)):
        cum[i+1] = cum[i] + a[i]
    b = [0] * (n * (n+1) // 2)
    index = 0
    for i in range(len(cum)):
        for j in range(i+1, len(cum)):
            b[index] = cum[j] - cum[i]
            index += 1

    ret = 0
    for i in range(45, -1, -1):
        b.sort(reverse=True, key=lambda x:x & 1 << i)
        tmp = search(b, i)
        if tmp + 1 < k:
            continue
        t = 1 << i
        ret += t
        b = b[:tmp+1]


    print(ret)


def search(array, i):
    le = 0
    ri = len(array)
    while ri - le > 1:
        mi = (ri + le) // 2
        if array[mi] & 1 << i:
            le = mi
        else:
            ri = mi
    return le


main()

Solution (Python):
import math

n, kk = tuple(map(int, input().split()))
sumnum = n * (n + 1) // 2
arr = tuple(map(int, input().split()))
sums = [0 for i in range(sumnum)]
hojo = [0 for i in range(n)]
point = 0

for i, e in enumerate(arr):
    for k in range(i):
        hojo[k] += e
        sums[point] = hojo[k]
        point += 1
    hojo[i] = e
    sums[point] = hojo[i]
    point += 1

maxlog = 0
maxe = max(sums)
while maxe >= 2**maxlog:
    maxlog += 1
maxlog -= 1

res = 0

for i in range(maxlog, -1, -1):
    ya = [0 for i in range(len(sums))]
    dab = 0
    gaga = 1 << i
    for e in sums:
        if e & gaga:
            ya[dab] = e
            dab += 1

    if dab >= kk:
        res += gaga
        sums = ya[:dab]

print(res)

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

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

ans = 0
for n in range(39,-1,-1):
    n2 = 2**n
    s_ = [s[i]%n2 for i in range(len(s)) if s[i]&n2 == n2]
    if len(s_) >= K:
        ans += n2
        s = s_

print(ans)

Solution (Python):
N, K = [int(_) for _ in input().split()]
A = [int(_) for _ in input().split()]
B = []
for i in range(N):
    B += [A[i]]
    for j in range(i+1,N):
        B += [B[-1] + A[j]]

ans = 0
for i in range(40, -1, -1):
    B_new = []
    for b in B:
        if b & 2 ** i:
            B_new += [b]
    if len(B_new) >= K:
        ans += 2 ** i
        B = [_ - 2 ** i for _ in B_new]
    else:
        B = [_ % 2 ** i for _ in B]
print(ans)

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

l_A = len(A)
nums = []
for i in range(l_A):
    sum = 0
    for j in range(i, l_A):
        sum += A[j]
        nums.append(sum)
nums.sort(reverse=True)
max_num = 0
for num in nums:
    if num > max_num:
        max_num = num

b = bin(max_num)
b_max_nums = (list(b))[2:]
l_b_max_nums = len(b_max_nums)

b_nums = []
for num in nums:
    b_nums.append(num)

target_group = b_nums
for i in range(l_b_max_nums):
    p = pow(2, l_b_max_nums-i)
    _target_group = []
    l_target_group = len(target_group)
    for t in target_group:
        if (p & t >= p):
            _target_group.append(t)
    if len(_target_group) >= K:
        target_group = _target_group
ans = target_group[0]
l_target_group = len(target_group)
for i in range(1, l_target_group):
    ans = ans & target_group[i]
print(ans)


'''
for num in nums:
    n = []
    for i in range(l_b_max_nums):
        n.append('0')
    _b = bin(num)
    _b_nums = (list(_b))[2:]
    l_b_nums = len(_b_nums)
    for i in range(l_b_nums):
        n[len(n)-1-i] = _b_nums[l_b_nums - 1 - i]
    b_nums.append(n)

target_group = b_nums
for i in range(l_b_max_nums):
    l_target = len(target_group)
    _target_group = []
    for j in range(l_target):
        if b_nums[j][i] == '1':
            _target_group.append(b_nums[j])
    if len(_target_group) >= K:
        target_group = _target_group

ans = int("0b" + ''.join(target_group[0]), 2)
l_target_group = len(target_group)
for i in range(1, l_target_group):
    ans = ans & int("0b" + ''.join(target_group[i]), 2)
print(ans)
'''