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






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(40)[::-1]:
  HERE = list(filter(lambda x: (x>>k)&1, SUMS))
  if len(HERE)>=K:
    ans += 1<<k
    SUMS = HERE
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]))
ans = 0
for i in reversed(range(41)):
    cou = 0
    now = ans + 2**i
    for j in b:
        if j & now >= now:
            cou += 1
    if cou >= k:
        ans += 2**i
print(ans)

Solution (Python):


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


cA = [0] * (N + 1)
for i in range(N):
    cA[i + 1] = cA[i] + A[i]
nums = []
for i in range(N + 1):
    for j in range(i + 1, N + 1):
        nums.append(cA[j] - cA[i])





now = 0
for d in range(40, -1, -1):
    count = 0
    
    
    for s in nums:
        if s & (now | (1 << d)) == now | (1 << d):
            count += 1
    if count >= K:
        now = now | (1 << d)
print(now)

Solution (Python):
from itertools import accumulate
n, k = map(int, input().split())
As = list(map(int, input().split()))

acc = [0]+list(accumulate(As))
mx = 0
sums = []
for i in range(n+1):
    for j in range(i+1,n+1):
        s = acc[j]-acc[i]
        sums.append(s)


now = 0
for d in range(40,-1,-1):
    cnt = 0
    for s in sums:
        
        
        
        if s & (now|(1<<d)) == now|(1<<d):
            cnt += 1
    
    if cnt >= k:
        now |= 1<<d
print(now)

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

from itertools import accumulate
acc = [0]+list(accumulate(As))
dp = [0] * 50
mx = 0
sums = []
for i in range(n+1):
    for j in range(i+1,n+1):
        s = acc[j]-acc[i]
        sums.append(s)


for d in range(50):
    if max(sums) & (1<<d):
        mx = max(mx, d)
        dp[d] += 1
mx = 0
for i in range(50):
    if dp[i] >= k:
        mx = max(mx,i)


ans = 0
now = 0
for d in range(50,-1,-1):
    cnt = 0
    for s in sums:
        if s & (now+(1<<d)) == now+(1<<d):
            cnt += 1
    
    if cnt >= k:
        now += 1<<d
print(now)

Solution (Python):

"""
数列Aが与えられる。
整数kが与えられる。
数列Aの空でない部分列からk個取る。
各部分列の合計を"美しさ"と言う。
この時、k個すべての"美しさ"のbit andを取った時、最大値がいくつになるか
求めよ。
n <= 1000, k <= n*(n+1)//2

解法：
まず直感的に、すべての要素の合計sum(lst1)を使うとして良い。
また、回答は必ずこれ未満の値となる。
更に、andを用いることから、上位桁のbit積を優先して良い。

となると、結局大きい値が上位桁のbitも多いことから、
和が大きい順にk個取った時の論理積ということになる。

N<1000という制約から、n*(n+1)//2が愚直に間に合うので、これを
実装すれば良い。

WA:
下位桁を優先したほうが良い場合が存在する。
解説：
下位桁を優先する、のではなく、上位桁を満たすようなk個を用意できなかった場合、
次に下位の桁を探すという解き方をする必要がある。つまり、上位桁を優先にするのは
変わらず、その桁のbitが1であるようなものをどんどん絞っていく形になる。
"""
import sys
readline = sys.stdin.buffer.readline
def even(n): return 1 if n%2==0 else 0
n,k = map(int,readline().split())
lst1 = list(map(int,readline().split()))

def cumsum(lst): 
    res = lst[:]
    for i in range(1,len(res)):
        res[i] += res[i-1]
    return res
def cumsumdif(l,r,lst): 
    if l == 0:
        return lst[r]
    else:
        return lst[r] - lst[l-1]
lst2 = cumsum(lst1)

lst3 = []

for i in range(n):
    for j in range(i,n):
        lst3.append(cumsumdif(i,j,lst2))

lst3.sort(reverse=True)

for i in range(40,-1,-1): 
    ct = 0
    res = []
    for j in lst3:
        if j>>i&1:
            ct += 1
            res.append(j)
    if ct >= k:
        m = i
        ans = pow(2,m)
        break

for i in range(m-1,-1,-1): 
    ct = 0
    resres = []
    for j in res:
        if j>>i&1:
            ct += 1
            resres.append(j)
    if ct >= k:
        ans += pow(2,i)
        res = resres

print(ans)

Solution (Python):
n,k=map(int,input().split())
a=list(map(int,input().split()))
tmp=0
ca=[tmp]
for ai in a:
  tmp+=ai
  ca.append(tmp)
r=[]
for i in range(n):
  for j in range(i+1,n+1):
    r.append(ca[j]-ca[i])
x=0
s=set([])
for t in range(40,-1,-1):
  s=set([])
  tmp=0
  for ri in r:
    if (ri>>t)&1:
      tmp+=1
      s.add(ri)
  if tmp>=k:
    x+=pow(2,t)
    m=t
    break
for t in range(m-1,-1,-1):
  tmp=0
  ss=set([])
  for si in s:
    if (si>>t)&1:
      tmp+=1
      ss.add(si)
  if tmp>=k:
    x+=pow(2,t)
    s=ss
print(x)

Solution (Python):
def main():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))
    B = [0]
    t = 0
    C = []
    for i, a in enumerate(A):
        t += a
        C.extend(t - b for b in B)
        B.append(t)
    r = 0
    k = B[-1].bit_length()
    for i in reversed(range(k + 1)):
        t = 0
        for c in C:
            if c & r != r:
                continue
            if c & (1 << i):
                t += 1
        if t >= K:
            r |= 1 << i
    return r
        
print(main())

Solution (Python):
def main():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))
    B = []
    r = 0
    for i in range(N):
        r = A[i]
        B.append(r)
        for j in range(i+1, N):
            r += A[j]
            B.append(r)
    for i in range(40, -1, -1):
        n = []
        for k in B:
            if k & 2**i == 2**i:
                n.append(k)
        if len(n) >= K:
                break
    else:
        return 0
    c = n
    r = [i]
    for j in range(i-1, -1, -1):
        n = []
        for i in c:
            if i & 2**j == 2**j:
                n.append(i)
        if len(n) >= K:
            r.append(j)
            c = n
    return sum([2**i for i in r])
print(main())

Solution (Python):
from itertools import accumulate
n, k = map(int, input().split())
a = [0] + list(accumulate(map(int, input().split())))

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


ans = 0
for i in range(50)[::-1]:
    num, cnt = ans + pow(2, i), 0
    for b in btfl:
        if num == num & b:
            cnt += 1
    if cnt >= k:
        ans = num
print(ans)

Solution (Python):
from itertools import accumulate

def main():
    n, k = map(int, input().split())
    a = [0] + list(accumulate(map(int, input().split())))

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

    
    ans = 0
    for i in range(50)[::-1]:
        num, cnt = ans + pow(2, i), 0
        for b in btfl:
            if num == num & b:
                cnt += 1
        if cnt >= k:
            ans = num
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
from itertools import accumulate
n, k = map(int, input().split())
a = [0] + list(accumulate(map(int, input().split())))

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


ans = 0
for i in range(50)[::-1]:
    num, cnt = ans + pow(2, i), 0
    for b in btfl:
        if num == num & b:
            cnt += 1
    if cnt >= k:
        ans = num
print(ans)

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

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

ans = 0
for i in range(44,-1,-1):
    x = 1<<i
    check = ans+x
    count = 0
    for j in l:
        if (check&j) == check:
            count += 1
    if count >= k:
        ans += x
print(ans)

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

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

ans = 0
for i in range(44,-1,-1):
    x = 1<<i
    check = ans+x
    count = 0
    for j in l:
        if (check&j) == check:
            count += 1
    if count >= k:
        ans += x
print(ans)

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

for i in range(n):
    count = 0
    for j in range(i,n):
        count += a[j]
        l.append(count)
l.sort(reverse=True)

le = 0
ri = 0

count = [0]*45
for i in range(44,-1,-1):
    x = 1<<i
    while ri < len(l) and l[ri] >= x:
        ri += 1
    
    for j in range(le,ri):
        for t in range(45):
            if l[j] >> t & 1:
                count[t] += 1
    
    if count[i] >= k:
        m = i
        break
    else:
        le = ri
    
check = []
for i in range(ri):
    if l[i] >> m & 1:
        check.append(l[i]%x)

ans = x
for i in range(m-1,-1,-1):
    count = 0
    for j in check:
        if j >> i & 1:
            count += 1
    if count >= k:
        x = 1<<i
        ans += x
        checkn = []
        for c in check:
            if c >> i & 1:
                checkn.append(c%x)
        check = checkn
print(ans)

Solution (Python):
N,K=map(int,input().split())
a=list(map(int,input().split()))
cum=[0]*(N+1)
for i in range(N):
    cum[i+1]=cum[i]+a[i]
A=[]
for i in range(N+1):
    for j in range(i):
        A.append(cum[i]-cum[j])
L=len(A)
ans=0
for j in reversed(range(46)):
    li=[]
    for i in range(L):
        if A[i]>>j&1:
            li.append(A[i])
    if len(li)<K:
        continue
    ans+=1<<j
    A=li
    L=len(A)
print(ans)

Solution (Python):
N, K = map(int, input().split())
a_list = [int(i) for i in input().split()]
A = [0, a_list[0]]
for i in range(1, N):
    A.append(A[-1] + a_list[i])
A_combination = []
for i in range(N + 1):
    for j in range(i + 1, N + 1):
        A_combination.append(A[j] - A[i])
x = 0
for i in range(40, 0, - 1):
    cnt = 0
    t = pow(2, i - 1)
    for A_i in A_combination:
        if ((x + t) & (A_i)) == (x + t):
            cnt += 1
        if cnt >= K:
            x += t
            break
print(x)

Solution (Python):
N,K=map(int,input().split())
a=list(map(int,input().split()))
from collections import defaultdict
d = defaultdict(int)
l=[0]
t=0
for i in a:
    t+=i
    l.append(t)
for i in range(N):
    for j in range(i+1,N+1):
        d[l[j]-l[i]]+=1
s=0
for i in range(39,-1,-1):
    t=0
    for j in d.keys():
        if (j>>i)%2:
            t+=d[j]
    if t<K:
        continue
    s+=2**i
    l=[]
    for j in d.keys():
        if (j>>i)%2==0:
            l.append(j)
    for j in l:
        d.pop(j)
print(s)

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

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

ans = 0

for i in range(40, -1, -1):
    cnt = 0
    for l in L:
        if (ans + (1 << i)) & l == ans + (1 << i):
            cnt += 1
    if cnt >= K:
        ans += 1 << i

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+1,n+1):
        s=sum(a[i:j])
        b.append(s)

x=0
for i in reversed(range(41)):
    cnt=0
    for j in b:
        if (x+2**i) & j ==(x+2**i):
            cnt+=1
    if cnt>=k:
        x+=2**i
print(x)

Solution (Python):
def main():
	n,k=map(int,input().split())
	a=[0]+list(map(int,input().split()))
	p=[]
	for i in range(n):
		a[i+1]+=a[i]
	for i in range(n+1):
		for j in range(i):
			p.append(a[i]-a[j])
	p.sort()
	ans=0
	from math import log,ceil
	r=ceil(log(sum(p))+10**-3)
	for i in range(r+2,-1,-1):
		cnt=0
		for j in range(len(p)):
			if (ans+2**i)&p[j]==ans+2**i:
				cnt+=1
				if cnt==k:
					break
		if cnt==k:
			ans+=2**i
	print(ans)
if __name__ == '__main__':
	main()

Solution (Python):
n,k=map(int,input().split())
a=[0]+list(map(int,input().split()))
p=[]
for i in range(n):
	a[i+1]+=a[i]
for i in range(n+1):
	for j in range(i):
		p.append(a[i]-a[j])
p.sort(reverse=True)
ans=0
from math import log,ceil
r=ceil(log(sum(p))+10**-3)
for i in range(r+3,-1,-1):
	cnt=0
	for j in range(len(p)):
		if (ans+2**i)&p[j]==ans+2**i:
			cnt+=1
		if cnt==k:
			break
	if cnt==k:
		ans+=2**i
print(ans)

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

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7
    N,K=MI()
    a=LI()
    L=[]
    S=[0]*(N+1)
    
    for i in range(N):
        S[i+1]=S[i]+a[i]
        
    for i in range(N+1):
        for j in range(i+1,N+1):
            s=S[j]-S[i]
            L.append(s)
            
    x=0
    
    N2=43
    p=[1]*N2
    for i in range(N2-1):
        p[i+1]=p[i]*2
        
    for i in range(N2):
        temp=x+p[N2-1-i]
        cnt=0
        for a in L:
            if (a&temp)==temp:
                cnt+=1
        if cnt>=K:
            x=temp
            
    print(x)
            

main()

Solution (Python):
def main():
    N, K = map(int, input().split())
    A = [0] + list(map(int, input().split()))

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

    B = [A[j] - A[i] for i in range(N) for j in range(i + 1, N + 1)]

    ans = 0
    tmp = pow(2, 40)
    for _ in range(40):
        count = 0
        tmp //= 2
        tmp_ = ans + tmp
        for b in B:
            if ((b & (tmp_)) == (tmp_)):
                count += 1
            if count >= K:
                ans += tmp
                break

    print (ans)

if __name__ == '__main__':
    main()

Solution (Python):
def main():
    N, K = map(int, input().split())
    A = [0] + list(map(int, input().split()))

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

    B = [A[j] - A[i] for i in range(N) for j in range(i + 1, N + 1)]

    ans = 0
    t = 40
    while t:
        count = 0
        tmp = pow(2, t -1)
        for b in B:
            if ((b & (tmp + ans)) == (tmp + ans)):
                count += 1
            if count >= K:
                ans += tmp
                break
        t -= 1

    print (ans)

if __name__ == '__main__':
    main()

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]

B = [A[j] - A[i] for i in range(N) for j in range(i + 1, N + 1)]

ans = 0
t = 40
while t:
    count = 0
    tmp = pow(2, t -1)
    for b in B:
        if ((b & (tmp + ans)) == (tmp + ans)):
            count += 1
    if count >= K:
        ans += tmp
    t -= 1

print (ans)

Solution (Python):
from collections import defaultdict
from itertools import accumulate
n, k = map(int, input().split())
A = list(map(int, input().split()))
Acum = [0]
Acum.extend(accumulate(A))

cnt = defaultdict(int)
for i in range(n):
    for j in range(i+1, n+1):
        beauty = Acum[j]-Acum[i]
        cnt[beauty] += 1


def check(x):
    res = 0
    for beauty, c in cnt.items():
        if x & beauty == x:
            res += c
    return k <= res


L = 0
R = 1 << 40
while L != R-1:
    M = (L+R)//2
    if check(M):
        L = M
    else:
        R = M
print(L)

Solution (Python):
from collections import defaultdict
from itertools import accumulate
n, k = map(int, input().split())
A = list(map(int, input().split()))
Acum = [0]
Acum.extend(accumulate(A))
cnt = defaultdict(int)
for i in range(n):
    for j in range(i+1, n+1):
        beauty = Acum[j]-Acum[i]
        cnt[beauty] += 1

x = 0
for t in reversed(range(41)):
    y = 1 << t
    cnt_k = 0
    for beauty, c in cnt.items():
        if (x+y) & beauty == (x+y):
            cnt_k += c
    if k <= cnt_k:
        x += y

print(x)

Solution (Python):
import sys
import copy

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


def input():
    return sys.stdin.readline().strip()


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

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

    s.sort(reverse=True)
    M = s[0]
    ans = 0
    n = M.bit_length()
    for i in range(n):
        tmp = []
        for v in s:
            if 1 << (n - i - 1) & v:
                tmp.append(v)
        if len(tmp) >= K:
            ans += 1 << (n - i - 1)
            s = copy.deepcopy(tmp)

    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
import math
import numpy as np
import queue
from collections import deque,defaultdict
import heapq
from sys import stdin,setrecursionlimit


ipt = stdin.readline
setrecursionlimit(10**7)

def main():
    n,k = map(int,ipt().split())
    a = [int(i) for i in ipt().split()]
    aa = np.zeros(n*(n+1)//2,dtype=int)
    ni = 0
    for i in range(n):
        sa = 0
        for j in range(i,n):
            sa += a[j]
            aa[ni] = sa
            ni += 1

    ans = 0
    for i in range(40):
        if np.count_nonzero((ans+2**(39-i))&aa==(ans+2**(39-i))) >= k:
            ans += 2**(39-i)

    print(ans)
    return

if __name__ == '__main__':
    main()

Solution (Python):
def main():
    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(-1, n-1):
        for j in range(i+1, n):
            if i == -1:
                b.append(a[j])
            else:
                b.append(a[j]-a[i])
    ans = 0
    for i in reversed(range(51)):
        cnt = 0
        for v in b:
            if ans & v == ans and (1<<i) & v != 0:
                cnt += 1
        if k <= cnt:
            ans += (1<<i)
    print(ans)

if __name__ == "__main__":
    main()

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

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

L = 40
b = [set() for _ in range(L)]
u = set(range(len(s)))
ans = 0

for i in range(L):
    for j, x in enumerate(s):
        if x >> i & 1:
            b[i].add(j)

for i in range(L)[::-1]:
    if len(u & b[i]) >= k:
        u &= b[i]
        ans += 2**i

print(ans)

Solution (Python):
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")



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


def cumsum(l):
    """a[i] = sum(l[:i]) なるaを返す
    """
    c = 0
    a = [None] * (len(l)+1)
    for i,num in enumerate(l):
        a[i] = c
        c += num
    a[-1] = c
    return a
c = cumsum(a)
l = 50
s = set()
for j in range(n+1):
    for i in range(j):
        s.add(bin(c[j]-c[i])[2:].rjust(l, "0"))
        
ans = 0
for i in range(l):
    count = 0
    tmp = set()
    for item in s:
        if item[i]=="0":
            pass
        else:
            count += 1
            tmp.add(item)
    if count>=k:
        ans+= 2**(l-i-1)
        s = tmp
print(ans)

Solution (Python):
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")

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

import numpy as np
c = np.cumsum(a)
b = c.reshape(-1,1) - c
l = int(np.log2(b.max())) + 1
s = set()
for i in range(1,n+1):
    for j in range(i):
        s.add(bin(b[i,j])[2:].rjust(l, "0"))
ans = 0
for i in range(l):
    count = 0
    tmp = set()
    for item in s:
        if item[i]=="0":
            pass
        else:
            count += 1
            tmp.add(item)
    if count>=k:
        ans+= 2**(l-i-1)
        s = tmp
print(ans)

Solution (Python):



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

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

B = []
for l in range(N):
    for r in range(l,N):
        if l == 0:
            B.append(accum[r])
        else:
            B.append(accum[r] - accum[l-1])


ans = 0
for i in range(45,-1,-1):
    used = []
    cnt = 0
    for b in B:
        if b & (1 << i):
            cnt += 1
            used.append(b)

    if cnt >= K:
        ans += pow(2,i)
        B = used


print(ans)

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

a_cum = [0 for _ in range(n+1)]
for i in range(n):
    a_cum[i+1] = a_cum[i] + a[i]

sum_list = []
for i in range(n+1):
    for j in range(i+1, n+1):
        sum_list.append(a_cum[j] - a_cum[i])
sum_list.sort()

x = len(bin(a_cum[n])) - 2


def search(x, l):
    if x == 0:
        return 0
    m = len(l)
    next_l = []
    cnt = 0
    for i in range(m):
        if (l[i] >> (x-1)) & 1:
            cnt += 1
            next_l.append(l[i])
    if cnt >= k:
        return search(x-1, next_l) + 2 ** (x-1)
    else:
        return search(x-1, l)

print(search(x, sum_list))

Solution (Python):
N,K=map(int, input().split())
A=list(map(int, input().split()))
D=[]
for i in range(N):
  for j in range(i,N):
    D.append(sum(A[i:j+1]))
ans=0
k=42
for i in range(1,k+1):
  cnt=0
  DD=[]
  for j in D:
    if (j>>(k-i))&1==1:
      cnt+=1
      DD.append(j)
  if cnt>=K:
    ans+=2**(k-i)
    D=DD
  elif cnt>=K:
    ans+=2**(k-i)
print(ans)

Solution (Python):
N,K=map(int, input().split())
A=list(map(int, input().split()))
D=[]
for i in range(N):
  for j in range(i,N):
    D.append(sum(A[i:j+1]))
ans=0
k=42
for i in range(1,k+1):
  cnt=0
  DD=[]
  for j in D:
    if (j>>(k-i))&1==1:
      cnt+=1
      DD.append(j)
  if cnt>=K:
    ans+=2**(k-i)
    D=DD
  elif cnt>=K:
    ans+=2**(k-i)
print(ans)

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

accum = [0] * N
for i in range(N):
    if i == 0:
        accum[i] = A[i]
    else:
        accum[i] = accum[i-1] + A[i]



B = []
for l in range(N):
    for r in range(l, N):
        if l == 0:
            B.append(accum[r])
        else:
            B.append(accum[r] - accum[l-1])




ans = 0
for i in range(50, -1, -1):
    
    cnt = 0
    nxt_B = []
    for b in B:
        if b & (1 << i):
            cnt += 1
            nxt_B.append(b)

    if cnt >= K:
        ans += 2**i
        B = nxt_B

print(ans)

Solution (Python):




"""


第5回 ドワンゴからの挑戦状 予選　　B - Sum AND Subarrays

N<=1000より美しさの全列挙は可能
K個選んでbitごとの論理積の最大値

K個選ぶ方法はnCk(10**6?, K)とかなので、全探索は間に合わない

K個選んだもののうちの最大、を言い換えを考える。
bitごとの論理積をとるので、選ばれたK個の中で、bitが大きいものについて、K個すべてで1が立っていると嬉しい。
また、2**0 + ... + 2**(N-1) <= 2**N なので、大きいbitが一個立ってる方が、それより小さいもの全てが立っているより嬉しい。
→大きいbitから見ていって、各美しさでそのbitに1が立っているか調べて、K個以上立っていれば、その美しさの中からK個採用される
　この時いずれの美しさも、今見ているbitが立っているので、答えにそのbit分加える。
　で、次のbitをみていって～を繰り返す。
　最下位bitまで見ても、美しさがK個以上のこっているなら、そこから適当にK個選んだら同じ答えになるので気にしなくてよい

bitごとの論理積が大きくなるようにK個選ぶ　→　大きいbitから、そのbitが立っているものがK個以上ある物が選ばれる
の言いかえ
"""

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

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

beauty = []
for l in range(N):
    for r in range(l, N):
        if l == 0:
            beauty.append(accum[r])
        else:
            beauty.append(accum[r] - accum[l-1])



ans = 0
candidate = beauty
for i in range(40, -1, -1):
    nxt_candidate = []
    cnt = 0
    for b in candidate:
        if b & (1 << i):
            cnt += 1
            nxt_candidate.append(b)
    
    
    
    if cnt >= K:
        ans += 2**i
        candidate = nxt_candidate

print(ans)

Solution (Python):




"""


第5回 ドワンゴからの挑戦状 予選　　B - Sum AND Subarrays

N<=1000より美しさの全列挙は可能
K個選んでbitごとの論理積の最大値

K個選ぶ方法はnCk(10**6?, K)とかなので、全探索は間に合わない

K個選んだもののうちの最大、を言い換えを考える。
bitごとの論理積をとるので、選ばれたK個の中で、bitが大きいものについて、K個すべてで1が立っていると嬉しい。
また、2**0 + ... + 2**(N-1) <= 2**N なので、大きいbitが一個立ってる方が、それより小さいもの全てが立っているより嬉しい。
→大きいbitから見ていって、各美しさでそのbitに1が立っているか調べて、K個以上立っていれば、その美しさの中からK個採用される
　この時いずれの美しさも、今見ているbitが立っているので、答えにそのbit分加える。
　で、次のbitをみていって～を繰り返す。
　最下位bitまで見ても、美しさがK個以上のこっているなら、そこから適当にK個選んだら同じ答えになるので気にしなくてよい

bitごとの論理積が大きくなるようにK個選ぶ　→　大きいbitから、そのbitが立っているものがK個以上ある物が選ばれる
の言いかえ
"""

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

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

beauty = []
for l in range(N):
    for r in range(l, N):
        if l == 0:
            beauty.append(accum[r])
        else:
            beauty.append(accum[r] - accum[l-1])



ans = 0
candidate = beauty
for i in range(40, -1, -1):
    nxt_candidate = []
    cnt = 0
    for b in candidate:
        if b & (1 << i):
            cnt += 1
            nxt_candidate.append(b)
    
    if cnt >= K:
        ans += 2**i
        candidate = nxt_candidate

print(ans)

Solution (Python):
n,kk= map(int, input().split())
a= list(map(int, input().split()))
x=[]
for i in range(n):
    v=0
    for j in range(i,n):
        v+=a[j]
        x.append(v)
x.sort()
r=len(bin(x[-1]))-2

ans=0
flag=False

v=[]
for i in range(r-1,-1,-1):
    cnt=0
    v.append(i)
    for j in x:
        flag=True
        for k in v:
            if not (j>>k)&1:
                flag=False
                break
        if flag:
            cnt+=1

    if cnt>=kk:
        ans+=pow(2,i)
    else:
        v.pop(-1)

print(ans)

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

n, k, *a = map(int, read().split())
now = []
ans = 0
for i, aa in enumerate(a):
    v = aa
    now.append(v)
    for aaa in a[i + 1:]:
        v += aaa
        now.append(v)
for m in range(40, -1, -1):
    ans *= 2
    cnt = 0
    bit = 1 << m
    nxt = []
    for num in now:
        if num & bit:
            cnt += 1
            nxt.append(num)
    if k <= cnt:
        ans += 1
        now = nxt
print(ans)

Solution (Python):
n,k=map(int,input().split())
a=list(map(int,input().split()))
subs=[]
for i in range(n):
    s=0
    for j in reversed(range(i+1)):
        s+=a[j]
        subs.append(s)
keta=0
smax=10**12
while smax>0:
    smax//=2
    keta+=1

ans=0
for i in reversed(range(keta+1)):
    lis=[]
    for num in subs:
        if (num>>i)&1:
            lis.append(num)
    if len(lis)>=k:
        ans+=pow(2,i)
        subs=lis
    else:
        if len(subs)==len(lis):
            ans+=pow(2,i)
print(ans)

Solution (Python):

import sys
input = sys.stdin.buffer.readline


from collections import defaultdict

con = 10 ** 9 + 7

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


def main():
	N, K = getlist()
	A = getlist()
	B = [0]
	for i in range(N):
		B.append(B[-1] + A[i])
	
	S = []
	for i in range(N + 1):
		for j in range(i + 1, N + 1):
			S.append(B[j] - B[i])
	

	z = 0
	for i in range(41):
		z += 2 ** (40 - i)
		cnt = 0
		for j in S:
			if z & j == z:
				cnt += 1
		if cnt < K:
			z -= 2 ** (40 - i)

	print(z)

if __name__ == '__main__':
	main()