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 collections, atexit, math, sys, bisect 

sys.setrecursionlimit(1000000)
def getIntList():
    return list(map(int, input().split()))    

try :
    
    import numpy
    def dprint(*args, **kwargs):
        
        
        print(*args,  file=sys.stderr)
    dprint('debug mode')
except Exception:
    def dprint(*args, **kwargs):
        pass



inId = 0
outId = 0
if inId>0:
    dprint('use input', inId)
    sys.stdin = open('input'+ str(inId) + '.txt', 'r') 
if outId>0:
    dprint('use output', outId)
    sys.stdout = open('stdout'+ str(outId) + '.txt', 'w') 
    atexit.register(lambda :sys.stdout.close())     


    
N, K = getIntList()

za  = getIntList()
zt = []

for i in range(N):
    tot = 0
    for j in range(N-i):
        tot += za[i+j]
        zt.append(tot)
zt.sort(reverse = True)

def find(z, bh):
    g = (1<<bh)
    zr = []
    for x in z:
        if g&x:
            zr.append(x)
    return zr

nowh = 40
result= 0
while nowh>=0:
    zr = find(zt,nowh)
    if len(zr)<K:
        nowh-=1
        continue
    result |= (1<<nowh)
    nowh-=1
    zt = zr

    
print(result)

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
while True:
    ok = 0
    for i in range(40):
        num = res + int("1" + "0"*i, 2)
        tmp = 0
        for j in range(len(Q)):
            if num & Q[j] == num:
                tmp += 1
        if tmp >= K:
            ok = num
    if res < ok:
        res = ok
    else:
        break
print(res)

Solution (Python):
N,K=map(int,input().split())
a=[int(i) for i in input().split()]
S=[0 for i in range(N+1)]
for i in range(1,N+1):
    S[i]=S[i-1]+a[i-1]
List=[]
for i in range(N+1):
    for j in range(i):
        List.append(S[i]-S[j])
List.sort(reverse=True)
def program(Y,L):
    
    M=len(L)
    t=[bin(i)[2:][::-1] for i in L]
    b=[i+"0"*(Y-len(i)) for i in t]
    X=[[] for i in range(Y)]
    for i in b:
        for j in range(Y):
            if i[j]=="1":
                X[j].append(i)
    
    l=Y
    for i in range(Y-1,-1,-1):
        if len(X[i])>=K:
            l=i
            break
    if l==Y:
        return 0
    
    R=[]
    for i in X[l]:
        if i[:l]=="":
            R.append(0)
            continue
        R.append(int(i[:l][::-1],2))
    R.sort(reverse=True)
    return program(l,R)+2**l

print(program(42,List))

Solution (Python):
import bisect
import itertools

def find_ge(a, x):
    'Find leftmost item greater than or equal to x'
    i = bisect.bisect_left(a, x)
    return i
def find_le(a, x):
    'Find rightmost value less than or equal to x'
    i = bisect_right(a, x)
    return i-1

ans=0
pmax=41
N,K = [int(x) for x in raw_input().split()]
A = [int(x) for x in raw_input().split()]
B=A[:]
for i in range(1,N):
    B[i]=B[i-1]+A[i]
B = [0] + B[:]
C=[0 for x in range(N*(N+1)/2)]
i=0
for xx in itertools.combinations_with_replacement(range(N), 2):
    l,r=sorted(xx)
    r+=1
    q = B[r]-B[l]
    C[i] = q
    i+=1

while(1):
    D=[ [] for x in range(pmax)]
    for q in C:
        for j in range(pmax):
            if q & pow(2, j)==pow(2, j):
                D[j].append(q)
    for j in range(pmax-1 ,-1 ,-1):
        if len(D[j])>=K:
            ans += 2**j
            C = D[j][:]
            pmax = j
            break
    else:
        break
print ans

Solution (Python):
def count(value, P):
    cnt = 0
    for p in P:
        if p & value == value:
            cnt += 1
    return cnt


def main():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))
    P = []
    for i in range(N):
        s = 0
        for j in range(i, N):
            s += A[j]
            P.append(s)
    retval = 0
    for idx in range(40, -1, -1):
        tmp = retval + 2**idx
        if count(tmp, P) >= K:
            retval = tmp
    print(retval)


if __name__ == "__main__":
    main()

Solution (Python):


N, K = map(int, input().split())
A = list(map(int, input().split()))
"""
if A == [1, 1] and K==2:
    print(1)
    exit()
"""
cumA = [0]
for a in A:
    cumA.append(a+cumA[-1])
from itertools import combinations
AA = []
for l, r in combinations(range(N+1), 2):
    AA.append(cumA[r] - cumA[l])

AA.sort(reverse=True)
ans = 0
d = len(bin(AA[0])) - 2
from bisect import bisect_right
while d>=0:
    
    if AA[K-1] >= 1<<d:
        ans |= 1<<d
        
        
        
        i= 0
        while i < len(AA) and AA[i] & 1<<d:
            AA[i] ^= 1 << d
            i += 1
            if i==len(AA):
                break
        AA = AA[:i]

        d -= 1
        
    else:
        i=0
        
        while i < len(AA) and AA[i] & 1<<d:
            AA[i] ^= 1<<d
            i += 1
        d -= 1
    AA.sort(reverse=True)
print(ans)

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):
N,K = map(int,input().split())
A = list(map(int,input().split()))
cums = [0]
for a in A:
    cums.append(cums[-1] + a)

vs = []
for l in range(N):
    for r in range(l+1,N+1):
        vs.append(cums[r] - cums[l])
vs.sort(reverse=True)

for i in range(100,-1,-1):
    tmps = []
    for v in vs:
        if v&(1<<i):
            tmps.append(v)
    if len(tmps) >= K:
        vs = tmps

if len(vs) < K:
    print(0)
else:
    ans = vs[0]
    for v in vs[1:K]:
        ans &= v
    print(ans)

Solution (Python):
import sys
n,k = map(int, sys.stdin.readline().split())
a = list(map(int, sys.stdin.readline().split()))
b = []
for i in range(n):
  for j in range(n):
    b.append(sum(a[i:j+1]))
    if b[-1] == 0:
      b.pop()
b.sort()
res = 0
for i in range(50,-1,-1):
  x = 1<<i
  ans = []
  for j in range(len(b)):
    if x&b[j]:
      ans.append(j)
  if len(ans) >= k:
    res += 1<<i
    c = []
    for l in ans:
      c.append(b[l])
    b = c
print(res)

Solution (Python):

N, K = map(int, input().split())
a_list = list(map(int, input().split()))
NN = N*(N+1) // 2

arr = [0] * NN
res = 0


ind = 0
for i in range(N):
    r = 0
    for j in range(i, N):
        r += a_list[j]
        arr[ind] = r
        ind += 1
        

for m in range(40, -1, -1):
    cnt = 0
    for i in range(NN):
        if (arr[i] // (2**m)) % 2 == 1:
            cnt += 1
    if cnt >= K:
        res += 2**m
        for i in range(NN):
            if (arr[i] // (2**m)) % 2 == 0:
                arr[i] = 0

print(res)

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

asum = [0]
for i in range(n):
    asum.append(asum[i] + aa[i])

def getsum(l, r):
    return asum[r + 1] - asum[l]

active = []
for l in range(n):
    for r in range(l, n):
        active.append(getsum(l, r))

ans = 0
for i in range(40, -1, -1):
    ans *= 2
    bit = 1 << i
    cnt = 0
    nxt = []
    for num in active:
        if num & bit:
            cnt += 1
            nxt.append(num)
    if cnt >= k:
        ans += 1
        active = nxt

print(ans)

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

a = np.array(a)

s = 0
e = 0

arr = []
aa = np.append(0,np.cumsum(a))
for s in range(len(aa)):
    for e in range(s+1, len(aa)):
        arr.append(aa[e] - aa[s])

max_len = len(bin(np.sum(a))[2:])

su = 0
b = [bin(aa)[2:] for aa in arr]
b = ['0' * (max_len - len(bb)) + bb for bb in b]
while True:
    c = [bb for bb in b if bb[0] == '1']
    if len(c) >= K:
        su += 2 ** (len(c[0]) - 1)
        b = [cc[1:] for cc in c]
    else:
        b = [bb[1:] for bb in b]
    if len(b[0]) == 0:
        break
print(su)

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**13
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    n,k = LI()
    a = LI()
    ii = [2**i for i in range(41)]
    u = [a[0]]
    for c in a[1:]:
        u.append(c+u[-1])
    for i in range(n-1):
        for j in range(i+1,n):
            u.append(u[j]-u[i])

    r = 0
    for t in ii[::-1]:
        b = [c for c in u if c >= t]
        if len(b) >= k:
            r += t
            u = [c - t for c in b]
        else:
            u = [c - (c & t) for c in u]

    return r


print(main())

Solution (Python):

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

    accA = [0] + list(accumulate(A))
    Bin = []

    for i in range(N + 1):
        for j in range(i + 1, N + 1):
            par_sum = accA[j] - accA[i]
            b = bin(par_sum)
            b = b[2:].zfill(40)
            Bin.append(b)

    ans = 0
    for n in range(40):
        sifted_B = [b for b in Bin if b[n] == "1"]
        if len(sifted_B) >= K:
            ans += 2 ** (39 - n)
            Bin = sifted_B

    print(ans)

    
main()

Solution (Python):
n,k=map(int,input().split())
a=list(map(int,input().split()))
x=[0,a[0]]
for i in range(1,n):
    x.append(x[-1]+a[i])
y=[]
for i in range(n+1):
    for j in range(i+1,n+1):
        y.append(x[j]-x[i])
y.sort()
y=y[::-1]
m=len(bin(y[0]))
y=["0"*(m-len(bin(y[i])))+bin(y[i])[2::] for i in range(len(y))]
res=["0" for _ in range(m-2)]
for x in range(m-2):
    t=0
    z=[]
    for i in range(len(y)):
        if y[i][x]=="1":
            t+=1
            z.append(y[i])
    if t>=k:
        res[x]="1"
        y=z[::]
print(int("".join(res),2))

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

L = []
for i in range(N):
    v = A[i]
    L.append(v)
    for j in range(i+1, N):
        v += A[j]
        L.append(v)
ans = 0
for i in range(64):
    b = 1 << (63-i)
    c = 0
    R = []
    for j in range(len(L)):
        if b & L[j]:
            R.append(L[j])
    if len(R) >= K:
        L = R
        ans |= b
print(ans)

Solution (Python):
from bisect import bisect_left
def main():
    n, k = map(int, raw_input().split())
    a = map(int, raw_input().split())
    s = [0] * (n + 1)
    for i, x in enumerate(a):
        s[i+1] = s[i] + x
    b = [s[i] - s[j] for i in xrange(1, n + 1) for j in xrange(i)]
    b.sort()
    l, r = 0, len(b)
    y = z = 0
    for i in xrange(39, -1, -1):
        x = z + (1 << i)
        p = bisect_left(b, x, l, r)
        if r - p >= k:
            z = x
            y += 1 << i
            l = p
        else:
            b = [((1 << i) - 1) & b[j] for j in xrange(l, r)]
            b.sort()
            l, r = 0, len(b)
            z = 0
    print y
main()

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

sys.setrecursionlimit(10**5)

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

from itertools import accumulate

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

a_cum = [0] + list(accumulate(a))
sm_lst = []
for i in range(n+1):
    for j in range(i+1,n+1):
        sm_lst.append(a_cum[j] - a_cum[i])
        
pop = sm_lst
ans = 0
for bit in range(40,-1,-1):
    yes=[]
    no=[]
    
    for s in pop:
        if s & (1<<bit):
            yes.append(s)
        else:
            no.append(s)
            
    if len(yes) >= k:
        ans += (1<<bit)
        pop = yes
        

print(ans)