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:
nk = input().split()
seisuuretu_2 = input().split()
nk1 = int(nk[1])

#seisuuretu = [int(i) for i in seisuuretu_2]
seisuuretu = list(map(int, seisuuretu_2))

list_max = set()
beautiful_list = []
for start in range(len(seisuuretu) + 1):
    for end in range(len(seisuuretu) + 1):
        if start < end:
            temp_pattern = bin(sum(seisuuretu[start:end]))[2:]
            beautiful_list.append(temp_pattern)
            list_max |= {len(temp_pattern),}

list_max = -max(list_max)

status = True
while status == True:
    temp = []
    count = 0
    for i in beautiful_list:
        try:
            if i[list_max] == "1":
                count += 1
                temp.append(i)
        except:
            continue
    if count > nk1:
        list_max += 1
        beautiful_list = temp
    elif count == nk1:
        num_count = True
        for num in temp:
            if num_count == True:
                ronriseki = int(num, 2)
            else:
                ronriseki = ronriseki & int(num, 2)
            num_count = False
        print(ronriseki)
        exit()
        status = False
    elif list_max == -1:
        num_count = True
        for num in beautiful_list:
            if num_count == True:
                ronriseki = int(num, 2)
            else:
                ronriseki = ronriseki & int(num, 2)
            num_count = False
        print(ronriseki)
        exit()
        status = False
    else:
        list_max += 1

Submission status: Accepted

Solution code:
from itertools import accumulate
import numpy as np

n, k = map(int, input().split())
A = list(map(int, input().split()))
C = [0] + list(accumulate(A))
D = []
for i in range(n):
  for j in range(i+1, n+1):
    D.append(C[j] - C[i])
    
D = np.array(D)
def enough(x):
  return np.count_nonzero(x&D == x) >= k

x = 0
for i in range(39, -1, -1):
  temp = x + 2**i
  if enough(temp):
    x = temp
    
print(x)

Submission status: Accepted

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

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


nlst=[]

for i in range(len(ruiA)-1):
    for m in range(i+1,len(ruiA)):
        nlst.append(ruiA[m]-ruiA[i])
nlst.sort(reverse=True)

lenbin=60
for i in range(len(nlst)):
    lst=[]
    for m in range(lenbin):
        lst.append((nlst[i]>>m)&1)
    nlst[i]=list(reversed(lst))
lenbinst=0

use=set([i for i in range(len(nlst))])
while lenbinst<lenbin:
    idx=-1
    count=0
    dellst=set()
    for m in use:
        if nlst[m][lenbinst]==1:
            count+=1
    if count>=K:
        if len(nlst)!=count:
            for m in use:
                if nlst[m][lenbinst]!=1:
                    dellst.add(m)
    use=use-dellst
    lenbinst+=1
valuelst=[0 for i in range(lenbin)]
nlst=[nlst[i] for i in use]

for n in nlst:
    for i,a in enumerate(list(reversed(n))):
        if a:
            valuelst[i]+=1

for i in range(lenbin):
    if valuelst[i]>=K:
        newlst=[i for i in nlst if i[idx]==1]
        if len(newlst)>=K:
            nlst=newlst

valuelst=[0 for i in range(lenbin)]
for n in nlst:
    for i,a in enumerate(list(reversed(n))):
        if a:
            valuelst[i]+=1
value=0
for i in range(lenbin):
    if valuelst[i]>=K:
        value+=2**i
print(value)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
from itertools import accumulate
import math

def solve(N: int, K: int, a: "List[int]"):
    accum = [0] + list(accumulate(a))
    sum_list = []
    for i in range(0,N):
        for j in range(i+1,N+1):
            sum_list.append(accum[j]-accum[i])
    sum_list.sort(reverse=True)
    max_bit = int(math.log2(sum_list[0]))
    answer = 0

    for i in range(max_bit,-1,-1):
        count = 0
        tmp = []
        for j in range(len(sum_list)):
            if sum_list[j] & 1<<i:
                count +=1
                tmp.append(sum_list[j])
            
        if count >= K:
            answer += 2**i
            sum_list = tmp
    print(answer)

    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    a = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, K, a)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
import math
from collections import defaultdict

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N,num):
    if N<=0:
        return [[] for _ in range(num)]
    elif num==1:
        return [I() for _ in range(N)]
    else:
        read_all = [tuple(II()) for _ in range(N)]
        return map(list, zip(*read_all))

#################

N,K = II()
a = III()

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

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

val.sort(reverse=True)

ans = 0
use = [True]*len(val)
for i in range(val[0].bit_length())[::-1]:
    ok_num = 0
    ng_list = []
    for j in range(len(val)):
        if not use[j]:
            continue
        else:
            if val[j]>>i&1:
                ok_num += 1
            else:
                ng_list.append(j)
    if ok_num>=K:
        ans += 2**i
        for n in ng_list:
            val[n] = False

print(ans)

Submission status: Accepted

Solution code:
import sys
import numpy as np

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N, K = lr()
A = np.array([0] + lr())
Acum = A.cumsum()
sum_range = Acum[:, None] - Acum[None, :]
sum_range = sum_range[sum_range>0]

def check(x):
    return np.sum(sum_range&x == x) >= K

answer = 0
for i in range(40, -1, -1):
    if check(answer + (1 << i)):
        answer += 1 << i

print(answer)

Submission status: Accepted

Solution code:
import sys
import numpy as np
import itertools

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N, K = lr()
A = np.array([0] + lr())
Acum = A.cumsum()
sum_range = Acum[:, None] - Acum[None, :]
sum_range = sum_range[sum_range>0]

def check(x):
    return np.sum(sum_range&x == x) >= K

answer = 0
for i in range(40, -1, -1):
    if check(answer + (1 << i)):
        answer += 1 << i

print(answer)
# 00

Submission status: Accepted

Solution code:
import sys
import numpy as np
import itertools

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N, K = lr()
A = np.array([0] + lr())
A_cum = A.cumsum().tolist()
beauties = []
for i, j in itertools.combinations(range(N+1), 2):
    beauties.append(A_cum[j] - A_cum[i])

beauties = np.array(beauties)
j = 0
def check(x):
    return np.sum(beauties&x == x) >= K

answer = 0
for i in range(40, -1, -1):
    if check(answer + (1 << i)):
        answer += 1 << i

print(answer)
# 00

Submission status: Accepted

Solution code:
import sys
import numpy as np
import itertools

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N, K = lr()
A = np.array([0] + lr())
A_cum = A.cumsum()
beauties = []
for i, j in itertools.combinations(range(N+1), 2):
    beauties.append(A_cum[j] - A_cum[i])

beauties = np.array(beauties)
j = 0
def check(x):
    return np.sum(beauties&x == x) >= K

answer = 0
for i in range(40, -1, -1):
    if check(answer + (1 << i)):
        answer += 1 << i

print(answer)
# 00

Submission status: Accepted

Solution code:
from itertools import accumulate

N, K, *A = map(int, open(0).read().split())

S = []
for i in range(N):
    S += [0] + list(accumulate(A[i:]))

ans = 0
for i in reversed(range(41)):
    b = 2 ** i
    T = [s for s in S if b & s != 0]
    if len(T) >= K:
        S = T
        ans += b

print(ans)

Submission status: Accepted

Solution code:
from sys import stdout
printn = lambda x: stdout.write(str(x))
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
ins = lambda : input().strip()
DBG = True # and False
BIG = 999999999
R = 10**9 + 7

def ddprint(x):
  if DBG:
    print(x)

n,k = inm()
a = inl()
b = []
acc = [0]*(n+1) # 0, a0, a0+a1, ..., a0+..+an-1
for i in range(n):
    acc[i+1] = acc[i]+a[i]
for i in range(n):
    for j in range(i+1,n+1):
        b.append(acc[j]-acc[i])
z = 0
for e in range(42,-1,-1):
    c = []
    for x in b:
        if (x>>e)%2 == 1:
            c.append(x)
    if len(c)<k:
        continue
    z += (1<<e)
    b = c
print(z)

Submission status: Accepted

Solution code:
def main():
    from itertools import accumulate

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

    acc = (0,) + tuple(accumulate((a)))

    bs = [acc[r] - acc[l] for r in range(1, n + 1) for l in range(r)]

    ret = 0
    for j in range(39, -1, -1):
        t = ret | 1 << j
        if sum(1 for b in bs if t & b == t) >= k:
            ret = t
    print(ret)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from itertools import accumulate
n, k = map(int, input().split())
A = list(map(int, input().split()))
cumsum = [0] + list(accumulate(A, lambda x, y: x + y))
li = []
for i in range(n + 1):
  for j in range(i + 1, n + 1):
    li.append(cumsum[j] - cumsum[i])
li.sort(reverse=True)

ans = 0
# 大きい桁から見ていく
for i in reversed(range(1, 41)):
  new = []
  for s in li:
    # sはi桁目のビット+ansと同じ位置ののビットが立っているか
    if (ans + 2 ** (i - 1)) & s == ans + 2 ** (i - 1):
      new.append(s)
  # その桁のビットが立っている要素の数がkを超えたら、その桁の値をansに足す
  if len(new) >= k:
    # 条件を満たす要素だけを対象とするように更新する
    li = new
    ans += 2 ** (i - 1)
print(ans)

Submission status: Accepted

Solution code:
def main():
    N,K = map(int,input().split())
    A = [int(x) for x in input().split()]
    A_cumsum = [0]
    for i in range(0,N):
        A_cumsum.append(A_cumsum[-1]+A[i])
    
    digit = len(bin(sum(A)))-2

    ls = []
    for i in range(N+1):
        for j in range(i+1,N+1):
            now = bin(A_cumsum[j]-A_cumsum[i])[2:]
            now = "0"*(digit-len(now))+now
            ls.append(now)
    
    ans = [0 for _ in range(digit)]

    for i in range(digit):
        
        now = []
        for j in range(len(ls)):
            if ls[j][i]=="1":
                now.append(ls[j])
        if K<=len(now):
            ls = now
            ans[i]=1
    out = ""
    for i in ans:
        out += str(i)
    print(int(out,2))

    
        
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
lists=list(map(int,input().split()))
GG=[]
sumlist=[0]
a=0
for i in range(len(lists)):
    a+=lists[i]
    sumlist.append(a) 
for i in range(len(sumlist)):
    for j in range(i+1,len(sumlist)):
        
        G=sumlist[j]-sumlist[i]
        GG.append(G)
        
GG=sorted(GG,reverse=True)      
maxi=format(GG[0],"b")
from collections import deque
d=deque()
e=deque()
for j in range(len(GG)):
    d.append(format(GG[j],"b").zfill(len(maxi)))
answer=0
index=0
 
while index!=len(maxi):
    counter=0
    for some in d:
        if some[index]=="1":
            counter+=1
            e.append(some)
    if counter>=k:
        answer+=2**(len(maxi)-1-index)
        d=e
        e=deque()
    elif counter<k:
        d=d
        e=deque()
        
    index+=1
    counter=0
print(answer)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
lists=list(map(int,input().split()))
GG=[]
sumlist=[0]
a=0
for i in range(len(lists)):
    a+=lists[i]
    sumlist.append(a) 
for i in range(len(sumlist)):
    for j in range(i+1,len(sumlist)):
        
        G=sumlist[j]-sumlist[i]
        GG.append(G)
        
GG=sorted(GG,reverse=True)      
maxi=format(GG[0],"b")
from collections import deque
d=deque()
e=deque()
for j in range(len(GG)):
    d.append(format(GG[j],"b").zfill(len(maxi)))
answer=0
index=0
 
while index!=len(maxi):
    counter=0
    for some in d:
        if some[index]=="1":
            counter+=1
            e.append(some)
    if counter>=k:
        answer+=2**(len(maxi)-1-index)
        d=e
        e=deque()
    elif counter<k:
        d=d
        e=deque()
        
    index+=1
    counter=0
print(answer)

Submission status: Accepted

Solution code:
import bisect
import copy
N,K=map(int,input().split())
a=list(map(int,input().split()))
s=[0]*(N+1)
for i in range(N):
  s[i+1]=s[i]+a[i]
sumList=[]
for i in range(N):
  for j in range(i,N):
    tmp=s[j+1]-s[i]
    sumList.append(tmp)
sumList.sort()
#print(sumList)
tmp=len(bin(sumList[-1]))-2+1
tmp=2**tmp
ans=0
while(tmp != 0):
  #print(tmp)
  tmpList=[sumList[i] % tmp for i in range(len(sumList)) if sumList[i]%tmp>=(tmp//2)]
  #print(tmpList)
  if len(tmpList)>=K:
    ans+=tmp//2
    #print(tmp//2)
    sumList=copy.copy(tmpList)
  tmp = tmp // 2
  
print(ans)

Submission status: Accepted

Solution code:
def to_cumsum(arr):
    """
    1次元配列を累積和の1次元配列に変換する
    """
    cumsum = 0
    new = [0] * len(arr)
    for i, e in enumerate(arr):
        cumsum += e
        new[i] = cumsum
    return new


N, K = map(int, input().split())
tmp = list(map(int, input().split()))
A = [0]
A.extend(tmp)
cumsum = to_cumsum(A)
arr = list()
for i in range(N + 1):
    for j in range(i + 1, N + 1):
        arr.append(cumsum[j] - cumsum[i])

ans_str = ''
for i in reversed(range(1, cumsum[-1].bit_length() + 1)):
    tmp = list(filter(lambda a: (a >> (i - 1)) & 1, arr))
    cnt = len(tmp)
    if cnt >= K:
        ans_str += '1'
        arr = tmp
    else:
        ans_str += '0'
print(int(ans_str, 2))

Submission status: Accepted

Solution code:
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from itertools import permutations
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    n,k = LI()
    a = LI()
    s = [0]
    for i in a:
        s.append(s[-1]+i)
    S = []
    for i in range(n):
        for j in range(i+1,n+1):
            S.append(s[j]-s[i])
    ans = 0
    for i in range(max(S).bit_length())[::-1]:
        m = 0
        b = 1<<i
        s_ = []
        for j in S:
            if j&b:
                m += 1
                s_.append(j)
        if len(s_) >= k:
            ans += b
            S = [i for i in s_]
    print(ans)
    return

#Solve
if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
from itertools import accumulate
As = [0] + list(map(int, input().split()))
As = list(accumulate(As))
L = 40
xs = []
for i in range(N+1):
    for j in range(i+1, N+1):
        xs.append(As[j] - As[i])
xs.sort()
ts = set(range(N*(N+1)//2))
for j in range(L-1,-1,-1):
    nts = set()
    for i in ts:
        if (xs[i]>>j)%2:
            nts.add(i)
    if len(nts) >= K:
        ts = nts
c = 0
r = 0
for i in ts:
    c += 1
    if r == 0:
        r = xs[i]
    else:
        r = r&xs[i]
    if c == K:
        break
print(r)

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
from itertools import accumulate
As = [0] + list(map(int, input().split()))
As = list(accumulate(As))
L = len(bin(As[-1]))-2
xs = []
for i in range(N+1):
    for j in range(i+1, N+1):
        xs.append(As[j] - As[i])
xs.sort()
ts = set(range(N*(N+1)//2))
for j in range(L-1,-1,-1):
    nts = set()
    for i in ts:
        if (xs[i]>>j)%2:
            nts.add(i)
    if len(nts) >= K:
        ts = nts
c = 0
r = 0
for i in ts:
    c += 1
    if r == 0:
        r = xs[i]
    else:
        r = r&xs[i]
    if c == K:
        break
print(r)

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
A = list(map(int,input().split()))
S = []
for i in range(N):
    t = i
    s = 0
    for j in range(t,N):
        s += A[j]
        S.append(s)
n = len(S)
x = 0
for i in range(40):
    m = 40 - i
    t = m - 1
    k = 0
    for j in range(n):
        if (x+2**t)&S[j] == x+2**t:
            k += 1
    if k >= K:
        x += 2**t
print(x)

Submission status: Accepted

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

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

M = 50
memo = [0]*M
L = []
for i in range(N):
    for j in range(i+1, N+1):
        L.append(S[j]-S[i])
        
for n in L:
    for i in range(M):
        if n&(1<<i):
            memo[i] += 1

p = -1
for i in range(M-1, -1, -1):
    if memo[i] >= K:
        p = i
        break

def f(s, List):
    #Listに含まれる要素の中に1<<sとのANDが正になるものがK個以上あるか判定
    L2 = []
    for n in List:
        if n&(1<<s):
            L2.append(n)
            
    mem = [0]*s
    for n in L2:
        for i in range(s):
            if n&(1<<i):
                mem[i] += 1
                
    p = -1
    for i in range(s-1, -1, -1):
        if mem[i] >= K:
            p = i
            break
            
    return [p, L2]
            
if p < 0:
    ans = 0
else:
    ans = 0
    L2 = L
    while p >= 0:
        ans += 1<<p
        t = f(p, L2)
        p = t[0]
        L2 = t[1]
            
print(ans)

Submission status: Accepted

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

B=[]
for s in range(N):
    t=0
    for e in range(s,N):
        t+=A[e]
        B.append(t)

def check(a):
    count=0
    for b in B:
        if b&a==a:
            count+=1
            if count>=K:
                return True
    return False

ans=0
for i in range(40,-1,-1):
    tmp=ans|(1<<i)
    if check(tmp):
        ans=tmp

print(ans)

Submission status: Accepted

Solution code:
from heapq import heappush,heappop,heapify
from collections import deque,defaultdict,Counter
import itertools
from itertools import permutations,combinations,groupby
import sys
import bisect
import string
import math
import time
import random
def S_():
    return input()
def LS():
    return [i for i in input().split()]
def I():
    return int(input())
def MI():
    return map(int,input().split())
def LI():
    return [int(i) for i in input().split()]
def LI_():
    return [int(i)-1 for i in input().split()]
def NI(n):
    return [int(input()) for i in range(n)]
def NI_(n):
    return [int(input())-1 for i in range(n)]
def StoI():
    return [ord(i)-97 for i in input()]
def ItoS(nn):
    return chr(nn+97)
def LtoS(ls):
    return ''.join([chr(i+97) for i in ls])
def GI(V,E,Directed=False,index=0):
    org_inp=[]
    g=[[] for i in range(n)]
    for i in range(E):
        inp=LI()
        org_inp.append(inp)
        if index==0:
            inp[0]-=1
            inp[1]-=1
        if len(inp)==2:
            a,b=inp
            g[a].append(b)
            if not Directed:
                g[b].append(a)
        elif len(inp)==3:
            a,b,c=inp
            aa=(inp[0],inp[2])
            bb=(inp[1],inp[2])
            g[a].append(bb)
            if not Directed:
                g[b].append(aa)
    return g,org_inp
def bit_combination(k,n=2):
    rt=[]
    for tb in range(n**k):
        s=[tb//(n**bt)%n for bt in range(k)]
        rt+=[s]
    return rt
def show(*inp,end='\n'):
    if show_flg:
        print(*inp,end=end)
YN=['Yes','No']
mo=10**9+7
inf=float('inf')
l_alp=string.ascii_lowercase
u_alp=string.ascii_uppercase
ts=time.time()
#sys.setrecursionlimit(10**5)
input=lambda: sys.stdin.readline().rstrip()
import fractions
#gcd=fractions.gcd
def ran_input():
    import random
    n=random.randint(4,16)
    rmin,rmax=1,10
    a=[random.randint(rmin,rmax) for _ in range(n)]
    return n,a
class Comb:
    def __init__(self,n,mo=10**9+7):
        self.fac=[0]*(n+1)
        self.inv=[1]*(n+1)
        self.fac[0]=1
        self.fact(n)
        for i in range(1,n+1):
            self.fac[i]=i*self.fac[i-1]%mo
            self.inv[n]*=i
            self.inv[n]%=mo
        self.inv[n]=pow(self.inv[n],mo-2,mo)
        for i in range(1,n):
            self.inv[n-i]=self.inv[n-i+1]*(n-i+1)%mo
        return
    
    def fact(self,n):
        return self.fac[n]
        
    def invf(self,n):
        return self.inv[n]

    def comb(self,x,y):
        if y<0 or y>x:
            return 0
        return self.fac[x]*self.inv[x-y]*self.inv[y]%mo
show_flg=False
show_flg=True
def gcd( x, y ):
    while y != 0:
        x, y = y, x%y
    return x

ans=0

n,k=LI()
a=LI()
ac=[0]
for i in range(n):
    ac.append(ac[-1]+a[i])

X=43
v=[]
for l in range(n):
    for r in range(l+1,n+1):
        t=ac[r]-ac[l]
        s=[1 if t&(1<<i) else 0 for i in range(X)]
        v.append(s)
#v=sorted(v)[::-1]

#ans=v[0]
for i in range(X-1,-1,-1):
    nx=[]
    for j in v:
        if j[i]==1:
            nx.append(j)
    if len(nx)>=k:
        ans+=(1<<i)
        v=[l for l in nx]
    else:
        0
    
print(ans)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline


N, K = map(int, input().split())
A = list(map(int, input().split()))
S = []
for i in range(N):
    s = 0
    for j in range(i, N):
        s += A[j]
        S.append(s)


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


# kaisetsu
def main():
    import math
    m = int(math.log(sum(A)*K, 2)) + 1
    ans = 0
    for i in range(m, -1, -1):
        k = f(ans | (1 << i))
        if k >= K:
            ans |= (1 << i)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import itertools
n,k=map(int,input().split())
a=list(map(int,input().split()))
l=[]
c=0
for i in range(n):
    for j in range(i,n):
        b=sum(a[i:j+1])
        l.append(b)
l.sort()
for i in range(len(l)):
    l[i]=format(l[i],'b').zfill(len(format(l[-1],'b')))
m=len(l[0])
l=l[::-1]
r=[0]*m
for i in range(m):
    for j in range(len(l)):
        if l[j][i]=='1':
            r[i]+=1
for i in range(m):
    ll=[]
    lll=[]
    if r[i]>=k:
        for j in range(len(l)):
            if l[j][i]=='1':
                ll.append(l[j])
            else:
                lll.append(l[j])
        l=ll
        if len(l)<k:
            l=l+lll
        elif len(l)==k:
            c+=2**(m-i-1)
            break
        elif len(l)>k:
            c+=2**(m-i-1)
    else:
        ll=l
ans=int(l[0],2)
for i in range(len(l)):
    ans&=int(l[i],2)
if len(l)>k:
    print(c)
else:
    print(ans)

Submission status: Accepted

Solution code:
import itertools
n,k=map(int,input().split())
a=list(map(int,input().split()))
l=[]
c=0
for i in range(n):
    for j in range(i,n):
        b=sum(a[i:j+1])
        l.append(b)
l.sort()
for i in range(len(l)):
    l[i]=format(l[i],'b').zfill(len(format(l[-1],'b')))
m=len(l[0])
l=l[::-1]
r=[0]*m
for i in range(m):
    for j in range(len(l)):
        if l[j][i]=='1':
            r[i]+=1
#print(l)
#print(r)
for i in range(m):
    ll=[]
    lll=[]
    if r[i]>=k:
        for j in range(len(l)):
            if l[j][i]=='1':
                ll.append(l[j])
            else:
                lll.append(l[j])
        l=ll
        if len(l)<k:
            l=l+lll
        elif len(l)==k:
            c+=2**(m-i-1)
            break
        elif len(l)>k:
            c+=2**(m-i-1)
        #print(c)
        #print(l)
    else:
        ll=l

#print(l)
ans=int(l[0],2)
for i in range(len(l)):
    ans&=int(l[i],2)
if len(l)>k:
    print(c)
else:
    print(ans)

Submission status: Accepted

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

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

B = []
for i in range(1, len(Acum)):
  for j in range(i):
    B.append(Acum[i] - Acum[j])
#print("B:", B)

tmp = []
for i in range(41, -1, -1):
  C = []
  for j in range(len(B)):
    if (B[j] >> i) & 1:
      C.append(B[j])
  lenC = len(C)
  if lenC >= K:
    B = C
    tmp.append(i)

answer = 0
for t in tmp:
  answer += 2 ** t
print(answer)

Submission status: Accepted

Solution code:
def main():
    import sys
    input = sys.stdin.readline

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

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

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

    tmax = cs[-1].bit_length()
    ans = 0
    for t in range(tmax-1, -1, -1):
        k = 0
        tmp = []
        for b in B:
            if b >> t & 1:
                k += 1
                tmp.append(b)
        if k >= K:
            B = tmp
            ans += 2**t

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys, math, itertools, collections, bisect
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
inf = float('inf') ;mod = 10**9+7
mans = inf ;ans = 0 ;count = 0 ;pro = 1

n,k=map(int,input().split())
data=[]
A=list(map(int,input().split()))
for i in range(n):
    for j in range(i+1,n+1):
        data.append(sum(A[i:j]))
# print(sorted(data))
for exp in range(41,-1,-1):
    candi=[]
    count=0
    for i,di in enumerate(data):
        if di>>exp & 1:
            count+=1
            candi.append(di)
    if count >= k:
        data = candi
p=data[0]
for ci in data:
    p=p & ci
print(p)
# print(data)
# def test():
#     ans=inf
#     data=[]
#     for i in range(n):
#         for j in range(i+1,n+1):
#             data.append(sum(A[i:j]))
#     nd=len(data)
#     for i in range(nd-1):
#         for j in range(i+1,nd):
#             ans=max(ans,data[i] & data[j])
#     return ans
# if p!=test():
#     print(p,test())

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])
l=[]
for i in range(1,n+1):
  for j in range(i):
    l.append(s[i]-s[j])
l.sort(reverse=True)
m=61
ans=0
chk=0
for i in range(m)[::-1]:
  cnt=0
  chk+=2**i
  for j in l:
    if chk & j == chk:
      cnt+=1
  if cnt < k:
    chk-=2**i
print(chk)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys

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

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

B=[]    #美しさの全列挙
l=len(S)

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

B.sort(reverse=True)

kouho=[ 1 for _ in range(len(B))]    #部分和の合計値で、候補となる値を1のbitで表現する

for keta in range(60,-1,-1):
    new_kouho=[]
    goukei=0 #1の出現回数
    for idx,b in enumerate(B):
        b=b>>keta&1
        if kouho[idx]==1:   #元々の候補に入っていれば、値を次の候補に残す
            new_kouho.append(b)
            goukei+=b
        else:
            new_kouho.append(0)       
    if K<=goukei:
        kouho=new_kouho #goukeiがK以上だったら、絞った候補を元の候補列に代入

ans=None
for k,b in zip(kouho,B):
    if k==1:
        if ans is None:
            ans=b
        else:
            ans&=b

print ans

Submission status: Accepted

Solution code:
from itertools import accumulate
def main():
	n, k, *a = map(int, open(0).read().split())
	acc = [0] + list(accumulate(a))
	ps = [acc[j] - acc[i] for i in range(n) for j in range(i + 1, n + 1)]
	ans = 0
	for i in range(max(ps).bit_length(), -1, -1):
		x = ans + (1 << i)
		if sum(y & x == x for y in ps) >= k:
			ans = x
	print(ans)

if __name__=="__main__":
	main()

Submission status: Accepted

Solution code:
from itertools import accumulate
n, k, *a = map(int, open(0).read().split())
acc = [0] + list(accumulate(a))
ps = [acc[j] - acc[i] for i in range(n) for j in range(i + 1, n + 1)]
ans = 0
for i in range(max(ps).bit_length(), -1, -1):
	x = ans + (1 << i)
	c = 0
	for y in ps:
		if y & x == x:
			c += 1
			if c >= k:
				ans = x
				break
print(ans)

Submission status: Accepted

Solution code:
from itertools import accumulate
n, k, *a = map(int, open(0).read().split())
acc = [0] + list(accumulate(a))
ps = [acc[j] - acc[i] for i in range(n) for j in range(i + 1, n + 1)]
ans = 0
for i in range(max(ps).bit_length(), -1, -1):
	x = ans + (1 << i)
	c = 0
	for y in ps:
		if y & x == x:
			c += 1
			if c >= k:
				ans = x
				break
print(ans)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
#l[i][j]でi~jの美しさ
l1=list(map(int,input().split()))
l2=[]
for i in range(n):
  sum1=0
  for j in range(i,n):
    sum1+=l1[j]
    l2.append(sum1)
l2=sorted(l2)
d=[0 for i in range(50)]
bitlist=[]
for i in l2:
  bit=[]
  num=i
  for j in range(50):
    d[j]+=num%2
    bit.append(num%2)
    num//=2
  bitlist.append(bit)
index=-1
for i in range(50):
  if d[i]>=k:
    index=i
    
if index==-1:
  print(0)
  exit()
b=True
ans1=[index]
while b:
  bitsub=[]
  for i in bitlist:
    if i[index]==0:
      pass
    else:
      bitsub.append(i)
  d=[0 for i in range(50)]
  for i in bitsub:
    for j in range(50):
      d[j]+=i[j]
  b=False
  for i in range(1,index+1):
    if d[index-i]>=k and not b:
      index=index-i
      b=True
      ans1.append(index)
  bitlist=bitsub
ans=0
for j in ans1:
  ans+=bitlist[0][j]*2**j
print(ans)