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: Wrong Answer

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

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

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


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

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

    return r


print(main())

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
A = [int(x) for x in input().split()]
B = [0] * N
B[0] = A[0]
for i in range(1, N):
    B[i] = B[i-1] + A[i]

maxv = 0

s = set()

for i in range(N):
    for j in range(i, N):
        if i == 0:
            s.add(B[j])
        else:
            s.add(B[j] - B[i-1])
l = sorted(list(s))

if K == 1:
    print(l[-1])
else:
    maxv = l[-1]
    for i in range(1,K):
        maxv = maxv & l[-1-i]

print(maxv)

Submission status: Wrong Answer

Solution code:
from array import array
from sys import stderr, setrecursionlimit
from functools import reduce
from operator import add, and_
setrecursionlimit(2147483647)
def f(): return [int(i) for i in input().split()]
def yes(n): print("Yes" if n else "No")
def debug(*x, sep=" ", end="\n"):
    for item in x:
        stderr.write(str(item))
        stderr.write(sep)
    stderr.write(end)

n, k = f()
a = array('l', f())
val = array('l')

for i in range(n):
    for j in range(i, n):
        val.append(sum(a[i:j+1]))

print(reduce(and_, sorted(val, reverse=True)[:k]))

Submission status: Wrong Answer

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

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

score = []
for r in range(1,N+1):
    for l in range(0,r):
        score.append(A_sum[r]-A_sum[l])

score.sort(reverse=True)
ans = score[0]
for i in range(1,K):
    ans = ans and score[i]

print(ans)

Submission status: Wrong Answer

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

a = [int(i) for i in input().split()]

max = sum(a)

K -= 1
while K > 0:
    if sum(a[1:]) > sum(a[:-1]):
        a = a[1:]
    else:
        a = a[:-1]
    max = sum(a)
    K -= 2

print(sum(a))

Submission status: Wrong Answer

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

for i in range(len(a)):
    for j in range(len(a)):
        if i < j:
            l.append(sum(a[i:j+1])) 
else:
    l.sort()
    l.reverse()
    print(l[k-1])

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys
from collections import deque
import copy

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] = [long(input_raw[0]), long(input_raw[1])]
    A = []
    input_raw = read_func().strip().split()
    S =[0 for i in range(N)]
    for i in range(N):
        A.append(int(input_raw[i]))
        if i > 0:
            S[i] = S[i - 1] + A[i]
        else:
            S[i] = A[i]

    parts = []
    for l in range(N):
        for r in range(l, N):
            if l - 1 >= 0:
                parts.append(S[r] - S[l - 1])
            else:
                parts.append(S[r])
    parts.sort(reverse=True)
    S = 0xFFFFFFFF
    for i in range(K):
        S &= parts[i]
    print S
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# coding: utf-8
# Your code here!
n,k = map(int,input().split())
l = list(map(int,input().split()))
rui = [0]
s = 0
for i in range(n):
    s = s + l[i]
    rui.append(s)
se = set()
for i in range(n):
    for j in range(i+1,n+1):
        se.add(rui[j] - rui[i])
# print(rui)
sums = sorted(list(se),reverse=True)
bit_and = [sums[0]]
te = sums[0]
for a in range(1,len(sums)):
    te = te & sums[a]
    bit_and.append(te)
print(bit_and[k-1])

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
A = list(map(int,input().split()))
cums = [0]
for a in A:
    cums.append(cums[-1] + a)

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

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

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

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
l=[]
for i in range(n):
  x=0
  for j in range(i,n):
    x+=a[j]
    l.append(x)
l.sort()
l.reverse()
print(l[k-1])

Submission status: Wrong Answer

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

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


sums.sort(reverse = True)
print(sums[K-1])

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
from pprint import pprint
N, K = map(int, input().split())
an = list(map(int, input().split()))

rui = [0]
tmp = 0
for a in an:
    rui.append(tmp+a)
    tmp += a


sum_list = [] 

for l in range(1,N+1):
    for r in range(l,N+1):
        tmp = rui[r] - rui[l-1]
        sum_list.append(tmp)

sum_list.sort(reverse=True)
ans = sum_list[0]
for s in sum_list[1:K]:
    ans &= s

print(ans)

Submission status: Wrong Answer

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

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

b.sort(reverse=True)

ans = b[0]
for i in b[:k]:
    ans = ans & i

print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import accumulate

n, k = map(int, input().split())
aaa = list(map(int, input().split()))
acc = [0] + list(accumulate(aaa))
subs = []

for l in range(n):
    acl = acc[l]
    for r in range(l + 1, n + 1):
        beu = acc[r] - acl
        subs.append(beu)

subs.sort(reverse=True)

ans = (1 << 50) - 1
for beu in subs[:k]:
    # print(beu, bin(beu), ans)
    ans &= beu

print(ans)

Submission status: Wrong Answer

Solution code:
# B
N, K = map(int, input().split())
a_list = list(map(int, input().split()))
NN = N*(N+1) // 2

arr = [0] * NN
res = 0

# bruce
ind = 0
for i in range(N):
    r = 0
    for j in range(i, N):
        r += a_list[j]
        arr[ind] = r
        ind += 1
        
# get Greedy
for m in range(40, 0, -1):
    cnt = 0
    for i in range(NN):
        if (arr[i] // (2**m)) % 2 == 1:
            cnt += 1
    if cnt >= K:
        res += 2**m
        for i in range(NN):
            if (arr[i] // (2**m)) % 2 == 0:
                arr[i] = 0

print(res)

Submission status: Wrong Answer

Solution code:
import math
from functools import reduce
import collections
import itertools
s = list(map(int,input().split()))
n = s[0]
k = s[1]
a = list(map(int,input().split()))
len_ = n-k+1
ans = 0
for i in range(k):
    if i == 0:
        if ans < sum(a[i:i+len_]):
            ans = sum(a[i:i+len_])
    elif i == k-1:
        if ans < sum(a[i:i+len_]):
            ans = sum(a[i:i+len_])
    else:
        if ans < sum(a[i:i+len_+1]):
            ans = sum(a[i:i+len_+1])
print(ans)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
from itertools import accumulate
from functools import reduce

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

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

l.sort(reverse=True)
print(l[k-1])

Submission status: Wrong Answer

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


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


beauty = sorted(beauty, reverse=True)
k = beauty[:K]
ans = k[0]
for i in range(1, K):
    ans = ans & k[i]


print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
l=list(map(int,input().split()))
s=[]
for i in range(n):
    for j in range(i+1,n):
        a=0
        a+=sum(l[i:j+1])
        s.append(a)
s.sort(reverse=True)
print(s[k-1])

Submission status: Wrong Answer

Solution code:
from itertools import combinations


def solve(N, K, As):
    alls = []
    for i in range(N):
        for j in range(i + 1, N + 1):
            alls.append(sum(As[i:j]))
    m = 0
    for c in combinations(alls, K):
        a = 0xFFFFFFFF
        for p in c:
            a &= p
        m = max(m, a)
    return m


# print(solve(4, 2, [2, 5, 2, 5]))
# print(solve(8, 4, [9, 1, 8, 2, 7, 5, 6, 4]))
if __name__ == "__main__":
    l = input()
    N = int(l.split(" ")[0])
    K = int(l.split(" ")[1])
    As = [int(c) for c in input().split()]
    print(solve(N, K, As))

Submission status: Wrong Answer

Solution code:
from itertools import accumulate
from functools import reduce

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

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

l.sort(reverse=True)
print(reduce(lambda x,y:x&y,l[:k]))

Submission status: Wrong Answer

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

L = []
for i in range(N):
    v = A[i]
    L.append(v)
    for j in range(i+1, N):
        v += A[j]
        L.append(v)
L.sort(reverse=1)
R = []
ans = 2**32-1
for i in range(K):
    ans &= L[i]
print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
x=[0,a[0]]
for i in range(1,n):
    x.append(x[-1]+a[i])
y=[]
for i in range(n+1):
    for j in range(i+1,n+1):
        y.append(x[j]-x[i])
y.sort()
y=y[::-1][:k]
z=y[0]
for i in range(1,k):
    z&=y[i]
print(z)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

cs = [0]
for aa in a:
    cs.append(cs[-1] + aa)

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

ps = sorted(ps, reverse=True)[:k]
ans = ps[0]
for p in ps:
    ans = ans & p
print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
*a,=map(int,input().split())
l=[]
for i in range(n):
    for j in range(i+1,n+1):
        l.append(sum(a[i:j]))
print(sorted(l)[-k])

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
A = list(map(int,input().split()))
cums = [0]
for a in A:
    cums.append(cums[-1] + a)

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

for i in range(32,-1,-1):
    nums = []
    for v in vs:
        if v&(1<<i):
            nums.append(v)
            if len(nums) == K:
                ans = nums[0]
                for n in nums[1:]:
                    ans &= n
                print(ans)
                exit()

print(0)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
lis = list(map(int,input().split()))
li = []
for i in range(n):
  for j in range(i,n):
    li.append(sum(lis[i:j+1]))
li.sort(reverse = True)
print(li[k-1])

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
a=[int(i) for i in input().split()]
S=[0 for i in range(N+1)]
for i in range(1,N+1):
    S[i]=S[i-1]+a[i-1]
L=[]
for i in range(N+1):
    for j in range(i):
        L.append(S[i]-S[j])
L.sort(reverse=True)
ans=L[0]
for i in range(1,K):
    ans=ans&L[i]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
stand_bit = [set() for i in range(42)]
acc = [0 for i in range(n+1)]
acc[0] = 0
for i in range(1, n+1):
    acc[i] += a[i-1] + acc[i-1]
set_ans = set()
for i in range(n):
    for j in range(i, n):
        set_ans.add(acc[j+1] - acc[i])
        s = acc[j+1] - acc[i]
        index = 41
        while s:
            if s & 1:
                stand_bit[index].add(acc[j+1] - acc[i])
            s = s >> 1
            index -= 1
for i in range(42):
    if len(set_ans & stand_bit[i]) >= k:
        set_ans &= stand_bit[i]
ans_list = list(set_ans)
ans = ans_list[0]
for i in ans_list:
    ans &= i
print(ans)

Submission status: Time Limit Exceeded

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

def main():
    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]
    
    b = n*(n+1)//2
    beauty = [0]*(b)
    ind = 0
    for i in range(n):
        for j in range(i, n):
            beauty[ind] = s[j+1] - s[i]
            ind += 1
    
    l = len(bin(max(beauty))) - 2
    
    ans = 0
    for i in range(l-1, -1, -1):
        key = 0
        count = 0
        for j in range(b):
            if (beauty[j] >> i) & 1:
                count += 1
                key = key | (1 << j)

        if count >= k:
            ans = (1 << i)
            for p in range(i-1, -1, -1):
                key2 = 0
                count = 0
                for q in range(b):
                    if not (key >> q) & 1:
                        continue
                    if (beauty[q] >> p) & 1:
                        count += 1
                        key2 = key2 | (1 << q)
                if count >= k:
                    key = key2
                    ans += (1 << p)
            break

    print(ans)

    
if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

def main():
    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]
    
    b = n*(n+1)//2
    beauty = [0]*(b)
    ind = 0
    for i in range(n):
        for j in range(i, n):
            beauty[ind] = s[j+1] - s[i]
            ind += 1
    
    beauty.sort()
    l = len(bin(beauty[-1])) - 2
    
    bit = [[0]*l for _ in range(b)]

    for i in range(l):
        for j, x in enumerate(beauty):
            if (x >> i) & 1:
                bit[j][i] = 1
    
    ans = 0
    for i in range(l-1, -1, -1):
        key = 0
        count = 0
        for j in range(b):
            if bit[j][i] == 1:
                count += 1
                key = key | (1 << j)
        if count >= k:
            ans = (1 << i)
            for p in range(i-1, -1, -1):
                key2 = 0
                count = 0
                for q in range(b):
                    if not (key >> q) & 1:
                        continue
                    if bit[q][p] == 1:
                        count += 1
                        key2 = key2 | (1 << q)
                if count >= k:
                    key = key2
                    ans += (1 << p)
            break

    print(ans)

    
if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin python3
# -*- coding: utf-8 -*-

import itertools

n, k = map(int, input().split())
a = list(itertools.accumulate(list(map(int, input().split()))))
a = [0] + a
s = []
for i in range(n+1):
    for j in range(i,n+1):
        s.append(list(format(a[j]-a[i], '050b')))

ret = 0
sel = set(range(n*(n+1)//2))
for i in range(50):
    nxt = set([])
    for j in sel:
        if s[j][i] == '1':
            nxt.add(j)
    if len(nxt)>=k:
        sel = nxt
        ret += 2**(49-i)
print(ret)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin python3
# -*- coding: utf-8 -*-

import itertools

n, k = map(int, input().split())
a = list(itertools.accumulate(list(map(int, input().split()))))
a = [0] + a
s = []
for i in range(n+1):
    for j in range(i,n+1):
        s.append(list(format(a[j]-a[i], '050b')))

ret = 0
sel = set(range(n*(n+1)//2))
for i in range(50):
    nxt = set([])
    for j in sel:
        if s[j][i] == '1':
            nxt.add(j)
    if len(nxt)>=k:
        sel = nxt
        ret += 2**(49-i)
print(ret)

Submission status: Time Limit Exceeded

Solution code:
from itertools import accumulate, combinations

def main():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))
    sum_A = sum(A)
    B = [0] + list(accumulate(A))
    cand = []
    for i in range(N+1):
        for j in range(i+1,N+1):
            cand.append(B[j] - B[i])
    cand.sort(reverse=True)
    length = len(bin(cand[K-1])[2:])
    num = []
    for c in cand:
        if len(bin(c)[2:]) < length:
            break
        num.append(c)

    ans = 0
    for item in combinations(num,K):
        n = 0
        for i in range(len(item)):
            if i == 0:
                n = item[i]
            n &= item[i]
        ans = max(ans, n)

    print(ans)




if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
import math
import collections
import bisect
import copy

# import numpy as np

sys.setrecursionlimit(10 ** 7)
INF = 10 ** 16
MOD = 10 ** 9 + 7
# MOD = 998244353

ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))
na1 = lambda: list(map(lambda x: int(x) - 1, sys.stdin.readline().split()))


# ===CODE===


def main():
    n, k = ns()
    a = na()

    d = []
    for i in range(n):
        tmp = a[i]
        d.append(tmp)
        for j in range(i + 1, n):
            tmp += a[j]
            d.append(tmp)

    def maxbit(a, idx, k):
        first1 = [[] for _ in range(41)]
        for ai in a:
            for i in range(40, -1, -1):
                if ai >> i & 1:
                    first1[i].append(ai)

        for i in range(idx - 1, -1, -1):
            if len(first1[i]) >= k:
                return first1[i], i

        return first1[idx], -1

    idx = 40
    while True:
        tmp, idx = maxbit(d, idx, k)
        if idx == -1:
            break
        d = tmp

    bit1 = [0 for _ in range(41)]
    for di in d:
        for i in range(41):
            if di >> i & 1:
                bit1[i] += 1

    ans = 0
    for i, bi in enumerate(bit1):
        if bi >= k:
            ans += pow(2, i)

    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()))
stand_bit = [set() for i in range(42)]
acc = [0 for i in range(n+1)]
acc[0] = 0
for i in range(1, n+1):
    acc[i] += a[i-1] + acc[i-1]
set_ans = set()
for i in range(n):
    for j in range(i, n):
        set_ans.add(acc[j+1] - acc[i])
        s = acc[j+1] - acc[i]
        index = 41
        while s :
            if s & 1:
                stand_bit[index].add(acc[j+1] - acc[i])
            s = s >> 1
            index -= 1
for i in range(42):
    if len(set_ans & stand_bit[i]) >= k:
        set_ans &= stand_bit[i]
ans_list = list(set_ans)
ans = ans_list[0]
for i in ans_list:
    ans &= i
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
stand_bit = [set() for i in range(64)]
acc = [0 for i in range(n+1)]
acc[0] = 0
for i in range(1, n+1):
    acc[i] += a[i-1] + acc[i-1]
set_ans = set()
for i in range(n):
    for j in range(i, n):
        set_ans.add(acc[j+1] - acc[i])
        s = acc[j+1] - acc[i]
        index = 63
        while s :
            if s & 1:
                stand_bit[index].add(acc[j+1] - acc[i])
            s = s >> 1
            index -= 1
for i in range(64):
    if len(set_ans & stand_bit[i]) >= k:
        set_ans &= stand_bit[i]
ans_list = list(set_ans)
ans = ans_list[0]
for i in ans_list:
    ans &= i
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
stand_bit = [set() for i in range(64)]
acc = [0 for i in range(n+1)]
acc[0] = 0
for i in range(1, n+1):
    acc[i] += a[i-1] + acc[i-1]
set_ans = set()
for i in range(n):
    for j in range(i, n):
        set_ans.add(acc[j+1] - acc[i])
        s = "{:b}".format(acc[j+1] - acc[i])
        s = s.zfill(64)
        for num, bit in enumerate(s):
            if bit == "1":
                stand_bit[num].add(acc[j+1] - acc[i])
for i in range(64):
    if len(set_ans & stand_bit[i]) >= k:
        set_ans &= stand_bit[i]
ans_list = list(set_ans)
ans = ans_list[0]
for i in ans_list:
    ans &= 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):
    for j in range(i,n):
        b.append(sum(a[i:j+1]))
ans = 0
for i in reversed(range(41)):
    cou = 0
    now = ans + 2**i
    for j in b:
        if j & now >= now:
            cou += 1
    if cou >= k:
        ans += 2**i
print(ans)

Submission status: Time Limit Exceeded

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

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

# 計算
now = 0
for d in range(40,-1,-1):
    cnt = 0
    for s in sums:
        # 上から見て、bitが立っている数がk個以上かどうか
        # 最上位がkこ未満だったら、そのbitはもう使わない
        # 1000 -> 1100 -> 1100 -> 1101 みたいな絞り込み
        if s & (now|(1<<d)) == now|(1<<d):
            cnt += 1
    # print(d,cnt)
    if cnt >= k:
        now |= 1<<d
print(now)

Submission status: Time Limit Exceeded

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

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

# 計算
ans = 0
now = 0
for d in range(40,-1,-1):
    cnt = 0
    for s in sums:
        # 上から見て、bitが立っている数がk個以上かどうか
        # 最上位がkこ未満だったら、そのbitはもう使わない
        # 1000 -> 1100 -> 1100 -> 1101 みたいな絞り込み
        if s & (now|(1<<d)) == now|(1<<d):
            cnt += 1
    # print(d,cnt)
    if cnt >= k:
        now |= 1<<d
print(now)

Submission status: Time Limit Exceeded

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

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

# 計算
ans = 0
now = 0
for d in range(40,-1,-1):
    cnt = 0
    for s in sums:
        # 上から見て、bitが立っている数がkこ以上かどうか
        # 1000 -> 1100 -> 1100 -> 1101 みたいな絞り込み
        # 何故か、
        if s & (now+(1<<d)) == now+(1<<d):
            cnt += 1
    # print(d,cnt)
    if cnt >= k:
        now += 1<<d
print(now)

Submission status: Time Limit Exceeded

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

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

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

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

Submission status: Time Limit Exceeded

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

from itertools import accumulate
acc = [0]+list(accumulate(As))
dp = [0] * 50
mx = 0
sums = []
for i in range(n+1):
    for j in range(i+1,n+1):
        s = acc[j]-acc[i]
        # print(i,j,s)
        sums.append(s)
        for d in range(50):
            if s & (1<<d):        
                dp[d] += 1
mx = 0
for i in range(50):
    if dp[i] >= k:
        mx = max(mx,i)

ex = [a for a in sums if a&(1<<mx)]
ans = 0
from itertools import combinations
for l in combinations(ex, k):
    ret = l[0]
    for e in l[1:]:
        ret &= e
    ans = max(ret, ans)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from itertools import accumulate, combinations

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

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

ans = 0
for l in combinations(btfl, k):
    cnt = l[0]
    for i in l:
        cnt &= i
    ans = max(ans, cnt)
print(ans)

Submission status: Time Limit Exceeded

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

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

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

Submission status: Time Limit Exceeded

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