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



Sample input:
4 2
2 5 2 5


Sample output:
12

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
import bisect
A=list(map(int,input().split()))
ruiseki=[0]*(N+1)
ruiseki[1]=A[0]
for i in range(1,N):
    ruiseki[i+1]=ruiseki[i]+A[i]
from collections import defaultdict
S=[]
for i in range(N):
    S+=[ruiseki[j+1]-ruiseki[i] for j in range(i,N)]
S.sort()
ans=0
for k in range(40,-1,-1):
    idx=bisect.bisect_left(S,pow(2,k))
    if len(S)-idx>=K:
        ans+=pow(2,k)
        S=[s-pow(2,k) for s in S[idx:]]
    else:
        S=[s%pow(2,k) for s in S]
        S.sort()
print(ans)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
arr=list(map(int,input().split()))
cand=[arr[0]]
for i in range(n-1):
  arr[i+1]+=arr[i]
  cand.append(arr[i+1])
for i in range(n):
  for j in range(i+1,n):
    cand.append(arr[j]-arr[i])
ans_b=''
for i in range(40,-1,-1):
  t=[]
  x=2**i
  for j in cand:
    if x&j==x:
      t.append(j)
  if len(t)>=k:
    cand=t
    ans_b+='1'
  else:
    ans_b+='0'
ans=0
for i in range(41):
  if ans_b[i]=='1':
    ans+=2**(40-i)
print(ans)

Submission status: Accepted

Solution code:
N, K =map(int, input().split())
a = list(map(int, input().split()))
cumsum = [0]
for i in range(N):
    cumsum.append( a[i] + cumsum[i] )
#print("{}".format(cumsum))

beauty = []
for l in range(N + 1):
    for r in range(l + 1, N + 1):
        beauty.append(cumsum[r] - cumsum[l])
beauty.sort()
#print("{}".format(beauty))

max = 0
for shift in range(60, -1, -1):
    mask = 1 << shift
    count = 0
#    print("{} {}".format(shift,beauty))
    for i in range(len(beauty)-1,-1,-1):
#        print("{} {}".format(i,beauty[i]))
        if (beauty[i] & mask) == 0:
            continue
        count+=1
    if count >= K:
#        print("{} {} {}".format(shift,max,mask))
        max |= mask;
#        print("{} {} {}".format(shift,max,mask))
        tmp = []
        for i in range(len(beauty) - 1, -1, -1):
            if ((beauty[i] & mask) == 0):
                continue
            tmp.append(beauty[i])
        beauty.clear()
        for i in range(len(tmp)):
            beauty.append(tmp[i])
print(max)

Submission status: Accepted

Solution code:
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]
B=[]
for i in range(1,N+1):
    for j in range(N-i+1):
        B.append(A[i+j]-A[j])
B.sort(reverse=True)
m=40
x=0
for t in range(m,0,-1):
    res=0
    for a in B:
        if ((x+2**(t-1))&a)==(x+2**(t-1)):
            res+=1
        if res>=K:
            x+=2**(t-1)
            break
print(x)

Submission status: Accepted

Solution code:
from itertools import accumulate, combinations
N, K = map(int, input().split())
A = list(map(int, input().split()))

SA = list(accumulate(A)) + [0]
B = A[:]

for l, r in combinations(range(N), 2):
    B.append(SA[r] - SA[l-1])

ans = 0
for i in range(42, -1, -1):
    ans *= 2
    B1 = [b for b in B if 2**i & b]
    if len(B1) >= K:
        ans += 1
        B = B1[:]

print(ans)

Submission status: Accepted

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

b = [0]
A = []
m = 40
for e in a:
    b.append(b[-1]+e)
for i in range(1,N+1):
    for j in range(1,N+1):
        if i <= j:
            A.append(b[j]-b[i-1])
xp = 0
for t in reversed(range(1,m+1)):
    count = 0
    for e in A:
        if e & (xp+2**(t-1)) == (xp+2**(t-1)):
            count += 1
    if count >= K:
        xp += 2**(t-1)
print(xp)

Submission status: Accepted

Solution code:
import bisect
N,K=map(int,input().split())
A=list(map(int,input().split()))

data=[]
for i in range(N):
    now=0
    for j in range(i,N):
        now+=A[j]
        data.append(now)
data.sort()
ans=0
for i in range(40,-1,-1):
    s=pow(2,i)
    idx=bisect.bisect_left(data,s)
    if len(data)-idx>=K:
        ans+=s
        data=[d-s for d in data[idx:]]
    else:
        data=[d for d in data[:idx]]+[d-s for d in data[idx:]]
        data.sort()
print(ans)

Submission status: Accepted

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

x = 0
for t in range(40, -1, -1):
    tmp = 0
    for j in d:
        if (x + 2 ** t) & j == x + 2 ** t:
            tmp += 1
    if tmp >= K:
        x += 2 ** t

print(x)

Submission status: Accepted

Solution code:
n,k = (int(i) for i in input().split())
a = [int(i) for i in input().split()]
x,y,num,ans = [0],[],40,0
for i in range(n): x.append(x[-1]+a[i])
for i in range(n):
    for j in range(i+1,n+1): y.append(x[j]-x[i])
for i in range(40,-1,-1):
    c,d = 0,ans+2**i
    for j in y:
        if d&j==d: c+=1
    if c>=k: ans = d
print(ans)

Submission status: Accepted

Solution code:
n,k,*a=map(int,open(0).read().split())
l=[]
for i in range(n):
    t=0
    for j in range(i,n):
        t+=a[j]
        l.append(t)
for i in range(40,-1,-1):
    j=2**i
    t=[]
    for x in l:
        if x&j:
            t.append(x)
    if len(t)>=k:
        l=t
a,*b=l
for t in b:
    a&=t
print(a)

Submission status: Accepted

Solution code:
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]
koho = []
for l in range(n):
    for r in range(l,n):
        koho.append(cum[r+1] - cum[l])
ret = 0
for keta in reversed(range(60)):
    currkoho = []
    for s in koho:
        if s & (1 << keta) != 0:
            currkoho.append(s)
    if len(currkoho) >= k:
        koho = currkoho
        ret += (1 << keta)
print(ret)

Submission status: Accepted

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


# 連続する部分列の和を計算
A_acc = [0] + list(accumulate(A))
SubArraysSums = []
for i in range(1, N + 1):
    for j in range(i, N + 1):
        SubArraysSums.append(A_acc[j] - A_acc[i - 1])
SubArraysSums.sort()


ans = 0
tmp = []
for i in reversed(range(40)):
    for s in SubArraysSums:
        if (1 << i) & s:
            tmp.append(s)

    if len(tmp) >= K:
        ans += (1 << i)
        SubArraysSums = tmp
    tmp = []

print(ans)

Submission status: Accepted

Solution code:
# coding: utf-8
def calculate_msb(maximum):
    msb = 0
    while (maximum // 2) != 0:
        maximum = maximum // 2
        msb += 1
    return msb
    
def examine(beauties, msb, threshold):
    count = 0
    mask = 2 ** msb
    passed = []
    for beauty in beauties:
  	    if (beauty & mask) == mask:
  	        passed.append(beauty)
  	        count += 1
    return (passed, mask) if count >= threshold else (beauties, 0)

n, threshold = map(int, input().split())
array = [int(element) for element in input().split()]
beauties = []
for i in range(n):
    for j in range(i+1):
	    beauties.append(sum(array[j:j+n-i]))

msb = calculate_msb(beauties[0])

answer = 0
while msb >= 0:
    beauties, add = examine(beauties, msb, threshold)
    answer += add
    msb -= 1
print(answer)

Submission status: Accepted

Solution code:
import copy
n, k = map(int, input().split())
B = list(map(int, input().split()))
A = []
for l in range(0, n):
	for r in range(l+1, n+1):
		A.append(sum(B[l:r]))
#print(A)
ans = 0
for i in range(41, -1, -1):
	tmp = []
	res = 0
	for a in A:
		if (a >> i) & 1 == 1:
			tmp.append(a)
			res += 1
	if res >= k:
		ans += 1 << i
		A = copy.copy(tmp)
print(ans)

Submission status: Accepted

Solution code:
def debug():
    import itertools
    N,K = map(int,input().split())
    a = list(map(int,input().split()))
    beauty = []
    for i in range(N):
        for j in range(i+1,N+1):
            part = a[i:j]
            beauty.append(sum(part))
    choise = [e for e in itertools.combinations(beauty,K)]
    ans = 0
    for pair in choise:
        res = pair[0]
        for i in range(1,len(pair)):
            res = res & pair[i]
        if res > ans :
            ans = res
            adopt = pair
    print(ans)
    print(pair)
    print(sorted(beauty))

def main():
    N,K = map(int,input().split())
    a = list(map(int,input().split()))
    degit = sum(a).bit_length()
    beaty = []
    manage = [[] for i in range(degit)]
    for i in range(N):
        for j in range(i+1,N+1):
            part_sum = sum(a[i:j])
            manage[part_sum.bit_length()-1].append(part_sum)
    
    candidate = []
    for i in range(degit-1,-1,-1):
        if len(manage[i]) >= K:
            candidate = manage[i]
            note = i
            break

    for i in range(note,-1,-1):
        final = []
        judge = pow(2,i)
        for e in candidate:
            if judge == judge & e:
                final.append(e)
        if len(final) == K:
            break
        elif len(final) > K:
            candidate = final
        if i == 0:
            final = candidate[:K]

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

Submission status: Accepted

Solution code:
#横にn個入力
n, k = map(int, input().split())
a = input().split()
a = [int(m) for m in a]
nli = []
b = 0
for i in range(n):
    for j in range(i, n):
        b += a[j]
        nli.append(b)
    b = 0
nli.sort(reverse=True)
m = 41
x = 0

cnt = 0
candili = []
for i in range(m - 1, -1, -1):
    if x == 0:
        for j in range(len(nli)):
            if nli[j] & (x + 2 ** i) == x + 2 ** i:
                candili.append(nli[j])
                cnt += 1
        if cnt >= k:
            x += 2 ** i
            cnt = 0
        else:
            cnt = 0
            candili = []
    else:
        for j in candili:
            if j & (x + 2 ** i) == x + 2 ** i:
                cnt += 1
        if cnt >= k:
            x += 2 ** i
            cnt = 0
        else:
            cnt = 0
print(x)

Submission status: Accepted

Solution code:
# coding:utf-8

import sys
from collections import defaultdict

INF = float('inf')
MOD = 10 ** 9 + 7
dy = (0, -1, 0, 1)
dx = (1, 0, -1, 0)

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def S(): return input()


N, K = LI()
A = LI()

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

sum_sub_array = defaultdict(int)
for l in range(N):
    for r in range(l + 1, N + 1):
        sub_sum = CA[r] - CA[l]
        sum_sub_array[sub_sum] += 1

ans = 0
for i in range(40)[::-1]:
    bit = 1 << i
    remain = 0
    new_sum_sub = {}
    for v, cnt in sum_sub_array.items():
        if v & bit > 0:
            remain += cnt
            new_sum_sub[v] = cnt

    if remain >= K:
        sum_sub_array = new_sum_sub
        ans |= bit

print(ans)

Submission status: Accepted

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

acum = list(accumulate([0]+a))

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

ans = 0

for mask in range(40, -1, -1):
    satis = sum([bool(ci&(1<<mask)) for ci in cand])

    if satis >= k:
        ans += (1<<mask)
        nex = [ci for ci in cand if ci & (1<<mask)]
        cand = nex
        

print(ans)

Submission status: Accepted

Solution code:
def check(ans, s):
    ret = 0
    for k in sumdic:
        if ans & k != ans:
            continue
        ret += 1
        if ret >= s:
            return True
    return False


n, k = map(int, input().split())
A = list(map(int, input().split()))
sumA = [0]
for i in range(len(A)):
    sumA.append(sumA[i] + A[i])

sumdic = [0] * (n * (n + 1) // 2)
kk = 0
for i in range(len(A)):
    # print(kk, sumdic)
    for j in range(i + 1, len(A) + 1):
        tmp = sumA[j] - sumA[i]
        sumdic[kk + j - 1 - i] = tmp
        # print(i, j)
    kk += n - i

# print(A)
# print(Counter(sumdic))
# Sumdic = Counter(sumdic)

for i in reversed(range(len(format(sumA[-1], "b")))):
    # print(i)
    if check(2 ** i, k) is True:
        ans = 2 ** i
        tmp = i
        break
else:
    print(0)
    exit()

tmp -= 1
while(tmp != -1):
    if check(ans + 2 ** tmp, k) is True:
        ans += 2 ** tmp
    tmp -= 1
print(ans)

Submission status: Accepted

Solution code:
import sys
import collections
from collections import Counter
# from collections import deque
# from collections import defaultdict
import itertools
# from itertools import accumulate
# from itertools import permutations
# from itertools import combinations
# from itertools import takewhile
from itertools import compress
# import functools
# from functools import reduce
import math
# from math import gcd
# from math import inf
# from math import ceil
# from math import floor
# from math import log10
# from math import log2
# from math import log
from math import factorial

# import re

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

S = set()
for i in range(N):
    for j in range(i, N):
        b = A[i:j + 1]
        S.add(sum(b))

S = list(S)

ans = 0
for i in range(39, -1, -1):
    A = [s & (1 << i) for s in S]
    newS = list(compress(S, A))
    if len(newS) >= K:
        ans += (1 << i)
        S = newS

print(ans)

Submission status: Accepted

Solution code:
import sys
import collections
from collections import Counter
# from collections import deque
# from collections import defaultdict
import itertools
# from itertools import accumulate
# from itertools import permutations
# from itertools import combinations
# from itertools import takewhile
from itertools import compress
# import functools
# from functools import reduce
import math
# from math import gcd
# from math import inf
# from math import ceil
# from math import floor
# from math import log10
# from math import log2
# from math import log
from math import factorial

# import re

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

S = set()
for i in range(N):
    for j in range(i, N):
        b = A[i:j + 1]
        S.add(sum(b))

S = list(S)

ans = 0
for i in range(40, -1, -1):
    A = [s & (1 << i) for s in S]
    newS = list(compress(S, A))
    if len(newS) >= K:
        ans += (1 << i)
        S = newS

print(ans)

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
a = list(map(int,input().split()))
s = []
for i in range(N):
    total = 0
    for j in range(i,N):
        total += a[j]
        s.append(total)
s.sort(reverse=True)
m,n = 1,1
while s[0]>=m:
    n += 1
    m *= 2
for i in range(n)[::-1]:
    t = list(filter(lambda x:x-m>=0,s))
    l = len(t)
    if l>=K:
        break
    else:
        m //= 2
n = i+1
for i in range(n):
    u = list(filter(lambda x:(x//m)%2==1,t))
    l = len(u)
    if l>=K:
        t = u[:]
        if l==K:
            break
    m //= 2
ans = t[0]
for i in range(1,K):
    ans &= t[i]
print(ans)

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
a=[int(i) for i in input().split()]
S=[0]
for i in range(N):
  S.append(S[i]+a[i])
C=[]
for i in range(1,N+1):
  for j in range(i):
    C.append(S[i]-S[j])
for k in range(50)[::-1]:
  List=[]
  for c in C:
    if c&(1<<k)!=0:
      List.append(c)
  if len(List)>=K:
    C=List[::]
C.sort(reverse=True)
ans=2**50-1
for i in range(K):
  ans&=C[i]
print(ans)

Submission status: Accepted

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


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


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

ans = 0

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

print(ans)

Submission status: Accepted

Solution code:
n,kk=map(int,input().split())
ss=input().split()
a=[int(ss[i]) for i in range(n)]
s=[0]*(n+1)
for i in range(n):
    s[i+1]=s[i]+a[i]
b=[None for i in range(n*(n+1)//2)]
k=0
for i in range(n):
    for j in range(i+1,n+1):
        b[k]=s[j]-s[i]
        k+=1
res=0
for i in range(50,-1,-1):
    m=2**i
    cnt=0
    for j in b:
        if j&m>0:
            cnt+=1
    if cnt>=kk:
        bb=[0 for j in range(cnt)]
        k=0
        for j in b:
            if j&m>0:
                bb[k]=j
                k+=1
        b=list(bb)
        res+=m
print(res)

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
b = [a[0]]
for i in range(n-1):
    b.append(b[i]+a[i+1])
s = b
for i in range(n-1):
    for j in range(i,n-1):
        s.append(b[j+1]-b[i])
s.sort(reverse=True)
l = len(bin(s[0]))-2
d = 1<<l-1
t = 0
while d>0:
    s1 = []
    if s[k-1] >= d:
        t += d
        for i in s:
            if i >= d:
                s1.append(i)
            else:
                s=s1
                break
    if len(s) < k:
        break
    for i in range(len(s)):
        if s[i] >= d:
            s[i] -= d
        else:
            s.sort(reverse=True)
            break
    if len(s) == k:
        t2 = d-1
        for i in s:
            t2 = t2&i
        t = t|t2
        break
    d = d>>1
print(t)

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
b = [a[0]]
for i in range(n-1):
    b.append(b[i]+a[i+1])
s = b
for i in range(n-1):
    for j in range(i,n-1):
        s.append(b[j+1]-b[i])
s.sort(reverse=True)
l = len(bin(s[0]))-2
d = 1<<l-1
t = 0
while d>0:
    s1 = []
    if s[k-1] >= d:
        t += d
        for i in s:
            if i >= d:
                s1.append(i)
            else:
                s=s1
                break
    d2 = d-1        
    for i in range(len(s)):
        if s[i] > d2:
            s[i] = s[i]&d2
        else:
            s.sort(reverse=True)
            break
    d = d>>1
print(t)

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
b = [a[0]]
for i in range(n-1):
    b.append(b[i]+a[i+1])
s = b
for i in range(n-1):
    for j in range(i,n-1):
        s.append(b[j+1]-b[i])
s.sort(reverse=True)
l = len(bin(s[0]))-2
d = 1<<l-1
t = 0
while d>0:
    s1 = []
    if s[k-1] >= d:
        t += d
        for i in s:
            if i >= d:
                s1.append(i)
            else:
                s=s1
                break
    for i in range(len(s)):
        if s[i] >= d:
            s[i] -= d
        else:
            s.sort(reverse=True)
            break
    d = d>>1
print(t)

Submission status: Accepted

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
b = [a[0]]
for i in range(n-1):
    b.append(b[i]+a[i+1])
s = b
for i in range(n-1):
    for j in range(i,n-1):
        s.append(b[j+1]-b[i])
s.sort(reverse=True)
l = len(bin(s[0]))-2
d = 1<<l-1
t = 0
while d>0:
    s1 = []
    if s[k-1] >= d:
        t += d
        for i in s:
            if i >= d:
                s1.append(i)
            else:
                s=s1
                break
    for i in range(len(s)):
        if s[i] >= d:
            s[i] -= d
        else:
            s.sort(reverse=True)
            break
    d //= 2
print(t)

Submission status: Accepted

Solution code:
import itertools
n,k = map(int,input().split())
a = list(map(int,input().split()))
b = [a[0]]
for i in range(n-1):
    b.append(b[i]+a[i+1])
c = list(itertools.combinations(range(n),2))
s = a
for i in c:
    s.append(b[i[1]]-b[i[0]]+a[i[0]])
s.sort(reverse=True)
l = len(bin(s[0]))-2
d = 1<<l-1
t = 0
while d>0:
    s1 = []
    if s[k-1] >= d:
        t += d
        for i in s:
            if i >= d:
                s1.append(i)
            else:
                s=s1
                break
    for i in range(len(s)):
        if s[i] >= d:
            s[i] -= d
        else:
            s.sort(reverse=True)
            break
    d //= 2
print(t)

Submission status: Accepted

Solution code:
from itertools import accumulate

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

accAs = [0] + list(accumulate(As))

Bs = []
for L in range(N):
    for R in range(L, N):
        Bs.append(accAs[R + 1] - accAs[L])

ans = 0
for d in reversed(range(40)):
    num = sum([(B >> d) & 1 for B in Bs])
    if num >= K:
        ans += 1 << d
        Bs = [B for B in Bs if (B >> d) & 1]

print(ans)

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
A=[int(i) for i in input().split()]
P=[0]*(N+1)
for i in range(N):
    P[i+1]=P[i]+A[i]
    
H=set()
for i in range(N+1):
    for j in range(i+1,N+1):
        H.add(P[j]-P[i])
L=[i for i in H]
a=max(L)
s=len(bin(a))-2

K1=[0]*s
t=1<<(s-1)
A=L
for i in range(s):
    T=A
    A=[a for a in T if (a&t)!=0]
    if len(A)>=K:
        K1[i]=1
    else:
        A=T
    t=t>>1
ans=0
u=1<<(s-1)
for i in range(s):
    if K1[i]==1:
        ans+=u
    u=u>>1
print(ans)

Submission status: Accepted

Solution code:
def cumsum(l):
    c = [0] * len(l)
    for i in range(len(l)):
        c[i] = c[i-1] + l[i]
    return c

N, K = map(int, input().split())
a = [int(i) for i in input().split()]
c = [0] + cumsum(a)
s = [bin(c[j+i] - c[j]).replace('0b','').zfill(40) for i in range(1, N+1) for j in range(N-i+1)]
ans = 0

for i in range(40):
    t = [j for j in s if j[i] == '1']
    if len(t) >= K:
        ans += 2**(39-i)
        s = t
print(ans)

Submission status: Accepted

Solution code:
def cumsum(l):
    c = [0] * len(l)
    for i in range(len(l)):
        c[i] = c[i-1] + l[i]
    return c

N, K = map(int, input().split())
a = [int(i) for i in input().split()]
c = [0] + cumsum(a)
s = {bin(c[j+i] - c[j]).replace('0b','').zfill(40) for i in range(1, N+1) for j in range(N-i+1)}
ans = 0

for i in range(40):
    t = {j for j in s if j[i] == '1'}
    if len(t) >= K:
        ans += 2**(39-i)
        s = t

print(ans)

Submission status: Accepted

Solution code:
def cumsum(l):
    c = [0] * len(l)
    for i in range(len(l)):
        c[i] = c[i-1] + l[i]
    return c

N, K = map(int, input().split())
a = [int(i) for i in input().split()]
c = [0] + cumsum(a)

s = [bin(c[j+i] - c[j]).replace('0b','').zfill(40) for i in range(1, N+1) for j in range(N-i+1)]

ans = 0

for i in range(40):
    t = [j for j in s if j[i] == '1']
    if len(t) >= K:
        ans += 2**(39-i)
        s = t

print(ans)

Submission status: Accepted

Solution code:
def cumsum(l):
    c = [0] * len(l)
    for i in range(len(l)):
        c[i] = c[i-1] + l[i]
    return c

N, K = map(int, input().split())
a = [int(i) for i in input().split()]
c = [0] + cumsum(a)

s = [bin(c[j+i] - c[j]).replace('0b','').zfill(40) for i in range(1, N+1) for j in range(N-i+1)]

ans = [0] * 40

for i in range(40):
    t = [j for j in s if j[i] == '1']
    if len(t) >= K:
        ans[i] = 1
        s = t

print(sum([ans[i] * 2**(39-i) for i in range(40)]))

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
a_list = list(map(int, input().split()))
sum_list = []

for i in range(N):
    for j in range(i, N):
        sum_list.append(sum(a_list[i:j+1]))
ans = 0
count = 0
tmp = []
for i in range(40, -1, -1):
    for s in sum_list:
        check_num = 1 << i
        logical_and = check_num & s

        if logical_and != 0:
            count += 1

    if count >= K:
        ans += 1 << i
        tmp = [x for x in sum_list if (1 << i) & x != 0]
        sum_list = tmp[:]
    count = 0
print(ans)

Submission status: Accepted

Solution code:
from itertools import accumulate as ac

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

l=[a[j]-a[i] for i in range(n+1) for j in range(i+1,n+1)]
f=lambda n:sum(map(lambda x:x&n==n,l))

ans=0
for i in range(41)[::-1]:
    if f(ans|1<<i)>=k:
        ans|=1<<i

print(ans)

Submission status: Accepted

Solution code:
from copy import deepcopy
from collections import deque
N, K = map(int, input().split())
A = [int(a) for a in input().split()]
Sum = deque([0])
for i in range(N):
    Sum.append(Sum[-1] + A[i])
Asum = deque([])
for i in range(N):
    for j in range(i+1, N+1):
        Asum.append(Sum[j] - Sum[i])

Search = deepcopy(Asum)
x = 0
for i in reversed(range(1, 41)):
    new = deque([])
    for s in Search:
        if (x + 2 ** (i-1)) & s == x + 2 ** (i-1):
            new.append(s)
    if len(new) >= K:
        Search = new
        x += 2 ** (i-1)
print(x)

Submission status: Accepted

Solution code:
from copy import deepcopy
N, K = map(int, input().split())
A = [int(a) for a in input().split()]
Sum = [0]
for i in range(N):
    Sum.append(Sum[-1] + A[i])
Asum = []
for i in range(N):
    for j in range(i+1, N+1):
        Asum.append(Sum[j] - Sum[i])

Search = deepcopy(Asum)
x = 0
for i in reversed(range(1, 41)):
    new = []
    for s in Search:
        if (x + 2 ** (i-1)) & s == x + 2 ** (i-1):
            new.append(s)
    if len(new) >= K:
        Search = new
        x += 2 ** (i-1)
print(x)

Submission status: Accepted

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

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


n2 = n*(n+1)//2

res = 0
for i in range(41):
    p = res + 2**(40-i)
    
    c = 0
    for j in range(n2):
        if p == p&b[j]:
            c+=1
    
    if c >= k:
        res = p

print(res)

Submission status: Accepted

Solution code:
from sys import stdin
def main():
    N, K = map(int, stdin.readline().rstrip().split())
    A = list(map(int, stdin.readline().rstrip().split()))

    groupList=[]
    # まずは部分列をつくる
    for i in range(N):
        for j in range(N-i):
            groupList.append(sum(A[j:j+i+1]))
    
    # ビットでandをとって残った物を採用
    ans=0
    for b in range(40,-1,-1):
        checkBit=2**b
        new_group_list = []

        for g in groupList:
            if  checkBit & g == checkBit:
                new_group_list.append(g)

        if len(new_group_list) >= K:
            groupList = new_group_list
            ans+=checkBit

    print(ans)

input = lambda: stdin.readline()
main()

Submission status: Accepted

Solution code:
from math import log2
n, k = map(int, input().split())
a = list(map(int, input().split()))
b = [[a[i]] for i in range(n)]
c = list(a)

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

m = 40 #ceil(log2((10**9)*1000))=40よりbit幅を40としておけば十分
ans = 0
for i in range(m, -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)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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)
        #print(str(sum) + " ",end='')
    #print()

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)

Submission status: Accepted

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