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: Time Limit Exceeded

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
AS = [0]
for i in range(N):
    AS += [AS[i] + A[i]]
AN = []
for i in range(N):
    for j in range(i, N):
        AN += [AS[j + 1] - AS[i]]
ans = 0
for i in range(41, -1, -1):
    c = 0
    p = ans + 2 ** i
    for x in AN:
        if p & x == p:
            c += 1
    if c >= K:
        ans += 2 ** i
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
AS = [0]
for i in range(N):
    AS += [AS[i] + A[i]]
AN = []
for i in range(N):
    for j in range(i, N):
        AN += [AS[j + 1] - AS[i]]
selected = [0] * len(AN)
ans = 0
for i in range(100, -1, -1):
    c = 0
    p = ans + 2 ** i
    for x in AN:
        if p & x == p:
            c += 1
            selected[i]
    if c >= K:
        ans += 2 ** i
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from itertools import accumulate
from functools import reduce

n,k = map(int, input().split())
a = list(map(int, input().split()))
acc = [0] + list(accumulate(a))
be = set()
# print(acc)
for i in range(n):
    for j in range(i+1,n+1):
        if acc[j] - acc[i] not in be:
            be.add(acc[j] - acc[i])
# print(be)
be = list(be)
mx = max(be)
# print(bin(mx))
bin_dic = {i:[] for i in range(len(str(bin(mx)))-2)}
# print(binary)
for b in be:
    for i,c in enumerate(str(bin(b))[-1:1:-1]):
        if c == '1':
            bin_dic[i].append(b)
mx_bin = 0
for i in bin_dic.keys():
    if k <= len(bin_dic[i]) and mx_bin < i:
        mx_bin = i
cands = set(bin_dic[mx_bin])
for i in range(mx_bin)[::-1]:
    tmp = set(bin_dic[i]) & cands
    if len(tmp) == k:
        cands = tmp
        break
    elif len(tmp) > k:
        cands = tmp
print(reduce(lambda x,y:x&y, cands))

Submission status: Time Limit Exceeded

Solution code:
def main():
  n,k = map(int, input().split())
  l = [0] + list(map(int, input().split()))
  for i in range(n):
    l[i+1] += l[i]
  a = [l[j]-l[i] for i in range(n) for j in range(i+1,n+1)]
  x = 0
  for i in range(40,-1,-1):
    t = 2**i
    if sum(y&(t+x)==t+x for y in a) >= k:
      x += t
  return x

if __name__ == "__main__":
  print(main())

Submission status: Time Limit Exceeded

Solution code:
n,k = map(int, input().split())
l = [0] + list(map(int, input().split()))
a = list()
for i in range(n):
  l[i+1] += l[i]
a = [l[j]-l[i] for i in range(n) for j in range(i+1,n+1)]
x = 0
for i in range(40,-1,-1):
  t = 2**i
  if sum(1 if y&(t+x)==t+x else 0 for y in a) >= k:
    x += t
print(x)

Submission status: Time Limit Exceeded

Solution code:
n,k = map(int, input().split())
l = [0] + list(map(int, input().split()))
a = list()
for i in range(n):
  l[i+1] += l[i]
a = [l[j]-l[i] for i in range(n) for j in range(i+1,n+1)]
x = 0
for i in range(40,-1,-1):
  t = 2**i
  if sum(map(lambda y:((y&(t+x)) == t+x),a)) >= k:
    x += t
print(x)

Submission status: Time Limit Exceeded

Solution code:
N,K=map(int,input().split())
A=list(map(int,input().split()))
a=[0]*(N+1)
for i in range(N):
    a[i+1]=a[i]+A[i]
from itertools import combinations
S=[a[r]-a[l] for l,r in combinations(range(N+1),2)]
x,y=0,1<<39
for i in range(40):
    n=0
    for s in S:
        if (x+y)&s==x+y:
            n+=1
    if n>=K:
        x+=y
    y>>=1
print(x)

Submission status: Time Limit Exceeded

Solution code:
#B問題
N,K = map(int,input().split())
A = list(map(int,input().split()))
rui = [0]

for i in range(N):
    if i == 0:
        rui.append(A[0])
    else:
        rui.append(rui[-1]+A[i])
    
bubun = []
for i in range(N):
    I = i
    for j in range(N-i):
        J = i+j+1
        bubun.append(rui[J]-rui[I])
    
bubun.sort(reverse=True)
m = 40
MAX = 0
kari = 0
while m > 0:
    kari = 2**(m-1)
    count = 0
    for b in bubun:
        if (MAX+kari)&b == MAX+kari:
            count+=1
            
    if count >= K:
        MAX+=kari
    m-=1
    
print(MAX)

Submission status: Time Limit Exceeded

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

bitflag = 0

utukusisa = []


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


def andd(comb):
    res = comb[0]
    for i in comb[1:]:
        res &= i
    return res

res = 0
for a in combinations(utukusisa,K):
    res = max(res, andd(a))
print(res)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
class cumulative_sum:
    def __init__(self,A):
        N = len(A)
        self.S = [0]*(N+1)
        for i in range(N):
            self.S[i+1] = self.S[i] + A[i]
    def get(self,i,j=None):
        if j==None:j=len(self.S)-1
        if j<=i:return 0
        return self.S[j]-self.S[i]
def main():
    N,K = map(int,input().split())
    *A, = map(int,input().split())
    SA = cumulative_sum(A)
    B = {}

    for i in range(N):
        for j in range(i+1,N+1):
            s = SA.get(i,j)
            B[s] = B.get(s,0)+1

    ans = 0
    for i in range(40,-1,-1):
        x = ans + (1<<i)
        c = sum(n for b,n in B.items() if b&x==x)
        if c>=K:ans = x
    print(ans)
main()

Submission status: Time Limit Exceeded

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

sum_list=[]
for i in range(N):
  s=0
  for j in range(i,N):
    s+=alist[j]
    sum_list.append(s)
    
sum_list.sort(reverse=True)
#print(sum_list)

answer=0
for i in reversed(range(41)):
  x=2**i
  cnt=0
  for s in sum_list:
    if (answer+x)&s==answer+x:
      cnt+=1
      if cnt>=K:
        answer+=x
        break
  
print(answer)

Submission status: Time Limit Exceeded

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

num=0
a_sum=[0]
for i in range(N):
  num+=a[i]
  a_sum.append(num)
  

a_mat=[[0 for i in range(N)] for j in range(N)]
non_zero=[]
for i in range(N):
  for j in range(i,N):
    a_mat[i][j] = a_sum[j+1]-a_sum[i]
    non_zero.append([i,j])

a_mat2=copy.deepcopy(a_mat)

maxbit=1
while True:
  if a_sum[N]<2**maxbit and a_sum[N]>=2**(maxbit-1): 
    break
  else:
    maxbit+=1
maxnum=0
num_rest=int(N*(N+1)/2)
num_rest_now=int(N*(N+1)/2)
for bit in range(maxbit):
  non_zero2=[]
  for i, j in non_zero:
    if a_mat[i][j]&(2**(maxbit-bit-1)) == 0 and a_mat[i][j]!=0:
      num_rest_now-=1
      a_mat2[i][j]=0
    else:
      non_zero2.append([i,j])
  #print(non_zero)
  #print(a_mat)
  #print(num_rest_now)
  
  if num_rest_now >= K:
    maxnum+=2**(maxbit-bit-1)
    a_mat=copy.deepcopy(a_mat2)
    num_rest=num_rest_now
    non_zero=copy.deepcopy(non_zero2)
  else:
    a_mat2=copy.deepcopy(a_mat)
    num_rest_now=num_rest
    
  
  
print(maxnum)

Submission status: Time Limit Exceeded

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

num=0
a_sum=[0]
for i in range(N):
  num+=a[i]
  a_sum.append(num)
  

#print(a_sum)

a_mat=[[0 for i in range(N)] for j in range(N)]
for i in range(N):
  for j in range(i,N):
    a_mat[i][j] = a_sum[j+1]-a_sum[i]
    

a_mat2=copy.deepcopy(a_mat)

maxbit=1
while True:
  if a_sum[N]<2**maxbit and a_sum[N]>=2**(maxbit-1): 
    break
  else:
    maxbit+=1
maxnum=0
num_rest=int(N*(N+1)/2)
num_rest_now=int(N*(N+1)/2)
for bit in range(maxbit):
  for i in range(N):
    for j in range (i,N):
      if a_mat[i][j]&(2**(maxbit-bit-1)) == 0 and a_mat[i][j]!=0:
        num_rest_now-=1
        a_mat2[i][j]=0
  #print(a_mat)
  #print(num_rest_now)
  if num_rest_now >= K:
    maxnum+=2**(maxbit-bit-1)
    a_mat=copy.deepcopy(a_mat2)
    num_rest=num_rest_now
  else:
    a_mat2=copy.deepcopy(a_mat)
    num_rest_now=num_rest
  
print(maxnum)

Submission status: Time Limit Exceeded

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


def main():
    from itertools import accumulate

    n, k = map(int, input().split())
    a = list(accumulate([0] + list(map(int, input().split()))))
    bits = [[0 for _ in range(n * (n + 1) // 2)] for _ in range(42)]
    case_count = 0

    for j in range(1, n + 1):
        for i in range(j, n + 1):
            bit = bin(a[i] - a[j - 1])[2:][::-1]

            for index, b in enumerate(bit):
                if b == '1':
                    bits[41 - index][case_count] = int(b)

            case_count += 1

    indexes = {i for i in range(n * (n + 1) // 2)}
    ans = 0

    for i in range(42):
        one_count = 0
        tmp_index = set()

        for j in range(n * (n + 1) // 2):
            if bits[i][j] == 1:
                one_count += 1
                tmp_index.add(j)

        if len(indexes & tmp_index) >= k:
            ans += 2 ** (41 - i)
            indexes &= tmp_index

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

# 部分和の作成
l = []
for i in range(n):
    t=0
    for j in range(i, n):
        t += a[j]
        l.append(t)
ans = 0
for i in range(40, -1, -1):
    x = 2 ** i
    s = []
    for ll in l:
        if (ans + x) & ll == (ans + x):
            s.append(ll)
    if len(s) >= k:
        ans += x
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
s=set()
for i in range(n):
     su=0
     for j in range(i,n):
          su+=a[j]
          s.add(su)
s=list(s)
x=0
for i in range(40):
     x_=x+2**(39-i)
     cnt=0
     for a in s:
          if x_&a==x_:
               cnt+=1
     if cnt>=k:
          x=x_
print(x)

Submission status: Time Limit Exceeded

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
s=set()
for i in range(n):
     for j in range(i,n):
          s.add(sum(a[i:j+1]))
s=list(s)
x=0
for i in range(40):
     x_=x+2**(39-i)
     cnt=0
     for a in s:
          if x_&a==x_:
               cnt+=1
     if cnt>=k:
          x=x_
print(x)

Submission status: Time Limit Exceeded

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
s=set()
for i in range(n):
     for j in range(i,n):
          s.add(bin(sum(a[i:j+1]))[2:].zfill(40))
s=list(s)
d={i:set() for i in range(40)}
for i in range(len(s)):
     for j in range(40):
          if s[i][39-j]=='1':
               d[j].add(i)
f=True
for i in range(40):
     s_=d[39-i]
     if len(s_)>=k:
          if f:
               se=s_
               f=False
          else:
               if len(se&s_)>k:
                    se&=s_
               elif len(se&s_)==k:
                    se&=s_
                    break
se=list(se)
ans=int(s[se[0]],base=2)
for i in range(1,k):
     ans&=int(s[se[i]],base=2)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations
n,k=map(int,input().split())
a=list(map(int,input().split()))
s=set()
for i in range(n):
     for j in range(i,n):
          s.add(sum(a[i:j+1]))
x=combinations(s,k)
m=0
for x_ in x:
     p=x_[0]
     for x__ in x_:
          p&=x__
     m=max(m,p)
print(m)

Submission status: Time Limit Exceeded

Solution code:
import itertools
import functools

N, K = input().strip().split()
N, K = [int(N), int(K)]

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

sum_list = []


for i in range(N):
  for j in range(i+1,N+1,1):
    sum_list.append(sum(an[i:j]))

c_list = list(itertools.combinations(sum_list,K))

ronriwa_list = []

for c in c_list:
  ronriwa_list.append(functools.reduce(lambda x, y: x & y, c))
  
print(max(ronriwa_list))

Submission status: Time Limit Exceeded

Solution code:
import itertools

N, K = input().strip().split()
N, K = [int(N), int(K)]

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

sum_list = []

for i in range(N):
  for j in range(i+1,N+1,1):
    sum_list.append(sum(an[i:j]))
    

c_list = list(itertools.combinations(sum_list,K))

ronriwa_list = []

for c in c_list:
  r = c[0]
  for i in range(1,K,1):
    r = r & c[i]
  ronriwa_list.append(r)
  
print(max(ronriwa_list))

Submission status: Time Limit Exceeded

Solution code:
import itertools

N, K = input().strip().split()
N, K = [int(N), int(K)]

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

sum_list = []

for i in range(N):
  for j in range(i+1,N+1,1):
    sum_list.append(sum(an[i:j]))
    
ronriwa = 0

c_list = list(itertools.combinations(sum_list,K))
for c in c_list:
  r = c[0]
  for i in range(1,K,1):
    r = r & c[i]
  if r >= ronriwa:
    ronriwa = r
  
print(ronriwa)

Submission status: Time Limit Exceeded

Solution code:

from collections import defaultdict

def search(binaries, K):
    max_b = max(binaries)
    max_bit = 0
    while max_b > 0:
        max_b = max_b >>1
        max_bit += 1

    ans = 0
    for i in reversed(range(max_bit + 1)):
        a = ans + (1 << i)
        if sum([int((b & a) == a) for b in binaries]) >= K:
            ans += 1 << i
    return ans

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

    # make binaries
    cumsum = [0, a[0]]
    for i in range(1, len(a)):
        cumsum.append(a[i]+cumsum[-1])

    binaries = [cumsum[j+1] - cumsum[i]for i in range(N) for j in range(i, N)]
    print(search(binaries, K))

main()

Submission status: Time Limit Exceeded

Solution code:

from collections import defaultdict

def search(binaries, K):
    max_b = max(binaries)
    max_bit = 0
    while max_b > 0:
        max_b = max_b >>1
        max_bit += 1

    ans = 0
    for i in reversed(range(max_bit + 1)):
        a = ans + (1 << i)
        if sum([int((b & a) == a) for b in binaries]) >= K:
            ans += 1 << i
    return ans

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

    # make binaries
    binaries = []
    for i in range(N):
        for j in range(i, N):
            binaries.append(sum(a[i:j+1]))

    print(search(binaries, K))


main()

Submission status: Time Limit Exceeded

Solution code:

from collections import defaultdict

def search(binaries, K):
    max_bit = 50
    counts = [0] * max_bit
    bin_d = defaultdict(list)
    for binary in binaries:
        i = 0
        b = binary
        while b > 0:
            if b & 1:
                counts[i] += 1
                bin_d[i].append(binary)
            i += 1
            b = b >> 1

    new_bins = []
    for i, c in enumerate(reversed(counts)):
        if c >= K:
            new_bins = bin_d[50-i-1]
            ans = 1<<(50-i-1)
            new_bins = [b - ans for b in new_bins]
            break

    if len(new_bins) == 0:
        return 0
    else:
        return ans + search(new_bins, K)


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

    # make binaries
    binaries = []
    for i in range(N):
        for j in range(i, N):
            binaries.append(sum(a[i:j+1]))

    print(search(binaries, K))


main()

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import deque
import copy
import math
def get_read_func(fileobject):
    if fileobject == None :
        return raw_input
    else:
        return fileobject.readline


def main():
    if len(sys.argv) > 1:
        f = open(sys.argv[1])
    else:
        f = None
    read_func = get_read_func(f);
    input_raw = read_func().strip().split()
    [N, K] = [int(input_raw[0]), int(input_raw[1])]
    input_raw = read_func().strip().split()
    A = [int(input_raw[i]) for i in range(N)]
    B = [0L for i in range(N * (N + 1)/2)]
    S = [0L for i in range(N + 1)]
    for i in range(N):
        S[i + 1] = long(S[i]) + long(A[i])
    idx = 0
    for i in range(1, N + 1):
        for j in range(i, N + 1):
            B[idx] = S[j] - S[i - 1]
            idx += 1

    ans = 0L
    remains = [True for i in range(len(B))]
    for k in range(40, -1, -1):
        new_remains = copy.deepcopy(remains)
        mask = 0x01 << k
        ok_count = 0
        for i in range(len(B)):
            if (B[i] & mask) > 0 and remains[i] == True:
                ok_count += 1
            else:
                new_remains[i] = False
        if ok_count >= K:
            ans += mask
            remains = copy.deepcopy(new_remains)

    print ans





if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import deque
import copy
import math
def get_read_func(fileobject):
    if fileobject == None :
        return raw_input
    else:
        return fileobject.readline


def main():
    if len(sys.argv) > 1:
        f = open(sys.argv[1])
    else:
        f = None
    read_func = get_read_func(f);
    input_raw = read_func().strip().split()
    [N, K] = [int(input_raw[0]), int(input_raw[1])]
    input_raw = read_func().strip().split()
    A = [int(input_raw[i]) for i in range(N)]
    B = [0L for i in range(N * (N + 1)/2)]
    S = [0L for i in range(N + 1)]
    for i in range(N):
        S[i + 1] = long(S[i]) + long(A[i])
    idx = 0
    for i in range(1, N + 1):
        for j in range(i, N + 1):
            B[idx] = S[j] - S[i - 1]
            idx += 1

    remains = [True for i in range(len(B))]
    for k in range(40, -1, -1):
        new_remains = copy.deepcopy(remains)
        mask = 0x01 << k
        ok_count = 0
        for i in range(len(B)):
            if (B[i] & mask) > 0 and remains[i] == True:
                ok_count += 1
            else:
                new_remains[i] = False
        if ok_count >= K:
            remains = copy.deepcopy(new_remains)

    ans = 0
    for i in range(len(B)):
        if remains[i] == True:
            if ans == 0:
                ans = B[i]
            else:
                ans &= B[i]
    print ans





if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

import sys, re
from collections import deque, defaultdict, Counter
from math import sqrt, hypot, factorial, pi, sin, cos, radians
if sys.version_info.minor >= 5: from math import gcd
else: from fractions import gcd 
from heapq import heappop, heappush, heapify, heappushpop
from bisect import bisect_left, bisect_right
from itertools import permutations, combinations, product
from operator import itemgetter, mul
from copy import copy, deepcopy
from functools import reduce, partial
from fractions import Fraction
from string import ascii_lowercase, ascii_uppercase, digits

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def round(x): return int((x*2+1) // 2)
def fermat(x, y, MOD): return x * pow(y, MOD-2, MOD) % MOD
def lcm(x, y): return (x * y) // gcd(x, y)
def lcm_list(nums): return reduce(lcm, nums, 1)
def gcd_list(nums): return reduce(gcd, nums, nums[0])
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

N, K = MAP()
A = LIST()

L = [[] for i in range(42)]
for i in range(N):
    tmp = [0] * (N+1)
    for j in range(i, N):
        tmp[j+1] += A[j] + tmp[j]
        k = 0
        while tmp[j+1] >= 2 ** k:
            k += 1
        L[k-1].append(format(tmp[j+1], str(k) + 'b'))

for nums in reversed(L):
    if len(nums) >= K:
        L2 = copy(nums)
        break

def rec(cur):
    global L2
    if cur == len(L2[0]):
        return
    cnt = 0
    flag = False
    L3 = []
    for i in range(len(L2)):
        if L2[i][cur] == '1':
            cnt += 1
            L3.append(L2[i])
            if cnt == K:
                flag = True
    if flag:
        L2 = copy(L3)
        rec(cur+1)
    else:
        rec(cur+1)
    return

rec(1)

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from collections import Counter


def check(ans, s):
    ret = 0
    for k, v in Sumdic.items():
        if ans & k != ans:
            continue
        ret += v
        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: Time Limit Exceeded

Solution code:
def check(ans, s):
    ret = 0
    for k, v in sumdic.items():
        if ans & k != ans:
            continue
        ret += v
        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 = {}
for i in range(len(A)):
    for j in range(i + 1, len(A) + 1):
        tmp = sumA[j] - sumA[i]
        sumdic[tmp] = sumdic.get(tmp, 0) + 1
        # print(i, j)

# print(A)
# print(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 != 0):
    if check(ans + 2 ** tmp, k) is True:
        ans += 2 ** tmp
    tmp -= 1
print(ans)

Submission status: Time Limit Exceeded

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 = sorted(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: Time Limit Exceeded

Solution code:
# coding:utf-8

from itertools import combinations

n, k = map(int, input().split())
an = [a for a in map(int, input().split())]

bn = []
for x in range(n):
    for y in range(x, n):
        bn.append(sum(an[x:y+1]))

xmax = 0
m = 40
for i in range(m, 0, -1):
    counter = 0
    for b in bn:
        t = xmax + pow(2, i)
        if t&b == t:
            counter += 1
            if counter >= k:
                xmax += pow(2, i)
                break
    
print(xmax)

Submission status: Time Limit Exceeded

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(50)):
    num = sum([(B >> d) & 1 for B in Bs])
    if num >= K:
        ans += 1 << d
        Bs = [B if (B >> d) & 1 else 0 for B in Bs]

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from itertools import accumulate as ac
from functools import reduce

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

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

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
from itertools import accumulate as ac
from functools import reduce

n,k=map(int,input().split())
*ac,=ac([0]+list(map(int,input().split())))
count_bits=lambda n:sum(map(lambda x:x&n==n,l))

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

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
B = []
for i in range(N):
    beauty = 0
    for j in range(i, N):
        beauty += A[j]
        B.append(beauty)
#print(B)
#bB = [format(b, '08b') for b in B]
#print(bB)
#bB.sort()
#print(bB)
maxb = 0
C = [[] for i in range(64)]
for b in range(63, -1, -1):
    for i in range(len(B)):
        if (1<<b) & B[i]:
            C[b].append(i)
    if not maxb and len(C[b])>=K:
        maxb = b
#print(maxb)
ans = set(C[maxb])
#print(ans)
for b in range(maxb, -1, -1):
    if len(C[b]) >= K:
        if len(ans.intersection(set(C[b]))) >= K:
            ans = ans.intersection(set(C[b]))
#print(ans)
res = B[ans.pop()]
for item in ans:
    res = res & B[item]
print(res)

Submission status: Time Limit Exceeded

Solution code:
n,k=map(int,input().split())
l=list(map(int,input().split()))
a = [0]
for i in l:
    a.append(a[-1]+i)
 
s = []
for i in range(1,n+1):
    for j in range(0,i):
        s.append(a[i]-a[j])
#print(s)
m=40
x=0
for i in range(40,-1,-1):
    a=0
    for j in s:
        if (x+(1<<i))&j==(x+(1<<i)):
            a+=1
           # print(i,j)
        if a>=k:
            x+=2**i
            break
print(x)

Submission status: Time Limit Exceeded

Solution code:
n,k=map(int,input().split())
l=list(map(int,input().split()))
a = [0]
for i in l:
    a.append(a[-1]+i)
 
s = []
for i in range(1,n+1):
    for j in range(0,i):
        s.append(a[i]-a[j])
#print(s)
m=40
x=0
for i in range(40,-1,-1):
    a=0
    for j in s:
        if (x+2**i)&j==(x+2**i):
            a+=1
           # print(i,j)
        if a>=k:
            x+=2**i
            break
print(x)

Submission status: Time Limit Exceeded

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

setAsum = set()

for i in range(N):
  for j in range(i+1, N+1):
    setAsum.add(sum(A[i:j]))
    
x = 0
for i in range(40, 0, -1):
  if len([a for a in setAsum if (x+2**(i-1)) & a == (x+2**(i-1))]) >= K:
    x += 2**(i-1)
print(x)

Submission status: Time Limit Exceeded

Solution code:
N,K = map(int,input().split())
a = list(map(int,input().split()))
b = []
bi = 0
m = 0
for l in range(N):
  for r in range(l,N):
    b.append(sum(a[l:r+1]))

maxb = max(b)
while 2**bi < maxb:
  bi+=1

while(2**bi>=1):
  List = []
  for i in range(len(b)):
    if 2**bi&b[i]>0:
      List.append(b[i])
  if len(List)>=K:
    b = List
    m += 2**bi
  bi -= 1

print(int(m))

Submission status: Time Limit Exceeded

Solution code:
N,K = map(int,input().split())
a = list(map(int,input().split()))
b = []
bi = 40
m = 0
for l in range(N):
  for r in range(l,N):
    b.append(sum(a[l:r+1]))


while(2**bi>=1):
  List = []
  for i in range(len(b)):
    if 2**bi&b[i]>0:
      List.append(b[i])
  if len(List)>=K:
    b = List
    m += 2**bi
  bi -= 1

print(int(m))

Submission status: Time Limit Exceeded

Solution code:
N,K = map(int,input().split())
a = list(map(int,input().split()))
b = []
bi = 0
m = 0
for l in range(N):
  for r in range(l,N):
    b.append(sum(a[l:r+1]))

while(2**bi<max(b)):
  bi+=1

while(2**bi>=1):
  List = []
  for i in range(len(b)):
    if 2**bi&b[i]>0:
      List.append(b[i])
  if len(List)>=K:
    b = List
    m += 2**bi
  bi -= 1

print(int(m))

Submission status: Time Limit Exceeded

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: Time Limit Exceeded

Solution code:
n, k = [int(v) for v in input().split()]
a = [int(v) for v in input().split()]
 
b = []
for i in range(n):
    s = a[i]
    b.append(s)
    for j in range(i+1, n):
        s += a[j]
        b.append(s)
 
MAX = 40
ans = 0
for t in range(MAX, 0, -1):
    x = ans + (1 << (t-1))
    cnt = 0
    for v in b:
        if x & v == x:
            cnt += 1
    if cnt >= k:
        ans = x
 
print(ans)

Submission status: Time Limit Exceeded

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(n-i):
        r += a[i+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: Time Limit Exceeded

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


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

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

print(res)