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:
def b_len(v):
    return len(bin(v)[2:])


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)

pw = b_len(ps[k - 1])
MOD = 2 ** pw

nps = {i:set() for i in range(pw)}
for i in range(len(ps)):
    t = ps[i] % MOD
    for j in range(pw):
        if 1 << j & t:
            nps[j].add(i)

ans = 0
buf = set(range(len(ps)))
for j in range(pw)[::-1]:
    if len(nps[j]) >= k:
        x = buf & nps[j]
        if len(x) >= k:
            buf = x
            ans += 2 ** j

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np


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

L = []

for l in range(N):
    for k in range(N+1):
        if l < k:
            L.append(sum(A[l:k]))

L.sort()
L = L[::-1]

L = [format(l, 'b') for l in L]
k = len(L[0])
L = [('0' * (k - len(l))) + l for l in L]
L = [list(map(int, list(l))) for l in L]

L.sort()
L = L[::-1]
L = np.array(L)

for i in range(k):
    if np.sum(L[:, i]) >= K:
        L = L[L[:, i] == 1]

R = '0b'

for i in range(k):
    if 0 in L[:K, i].tolist():
        R += '0'
    else:
        R += '1'

print(int(R, 2))

Submission status: Time Limit Exceeded

Solution code:
import itertools

n,k = map(int,input().split())
a = list(map(int,input().split()))
sumlist = []

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

sumlist.sort(reverse=True)

x = 1
while 1:
    if 2**(x-1)-1 < sumlist[k-1] <= 2**x-1:
        break
    else:
        x += 1

ans = sumlist[:k]
u = k
while 1:
    if 2**(x-1)-1 < sumlist[u] <= 2**x-1:
        ans.append(sumlist[u])
        u += 1
    else:
        break

maxand = 0
for v in itertools.combinations(ans,k):
    vv = list(v)
    tmp = 2**99-1
    for i in vv:
        tmp = tmp & i
    maxand = max(maxand,tmp)

print(maxand)

Submission status: Time Limit Exceeded

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

tmp = [0 for _ in range(N*(N+1)//2)]
ind = 0
for l in range(N):
    sum_tmp = A[l]
    tmp[ind] = sum_tmp
    ind += 1
    for r in range(l+1,N):
        sum_tmp += A[r]
        tmp[ind] = sum_tmp
        ind += 1


tmp = sorted(tmp,reverse=True)

tmp2 = set(list(itertools.combinations(tmp,K)))
tmp2 = sorted(tmp2, reverse=True)

ans = 0
for T in tmp2:
    ans_tmp = T[0]
    for t in T:
        ans_tmp = int(ans_tmp and t)

    ans = max(ans,ans_tmp)
    break
    # if ans < ans_tmp:
    #     ans = ans_tmp
    #     print(T)


print(ans)







# B1 = A
# B2 = A[1:]
# ans = sum(B2)

# print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N, K = map(int, input().split())
A = list(map(int, input().split()))
sumlist = []
for i in range(N):
    for j in range(i+1):
        sumlist.append(sum(A[j:j+N-i]))
sumlist.sort(reverse=True)
sumlist = np.array(sumlist)
ans = sumlist[0]
for i in range(40, -1, -1):
    tmp = (ans & (2 ** i)) // (2 ** i)
    if tmp:
        start = i
        break
f = lambda x: (x & onehot) // onehot
ans_list = [0]
for i in range(start, -1, -1):
    onehot = 2 ** i
    tmp = sum(map(f, sumlist))
    if tmp >= K:
        and_result = list(map(f, sumlist))
        and_result = np.array(and_result).astype(np.bool)
        sumlist = sumlist[and_result]
        ans = sumlist[0]
        for j in range(1, K):
            ans = ans & sumlist[j]
        ans_list.append(ans)
print(max(ans_list))

Submission status: Time Limit Exceeded

Solution code:
import itertools
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()
y=[]
for z in itertools.combinations(l,k):
  x=z[0]
  for i in range(1,k):
    x=x&z[i]
  y.append(x)
print(max(y))

Submission status: Time Limit Exceeded

Solution code:
def solve(N, K, As):
    alls = []
    for i in range(N):
        for j in range(i + 1, N + 1):
            alls.append("{:061b}".format(sum(As[i:j])))
    def find(i, alls):
        if i > 60:
            return []
        ones = [a for a in alls if a[i] == "1"]
        if len(ones) < K:
            return find(i + 1, alls)
        elif len(ones) == K:
            return ones
        elif len(ones) > K:
            ret = find(i + 1, ones)
            if len(ret) == 0:
                return ones[0:K]
            else:
                return ret

    ans = find(0, alls)
    if len(ans):
        a = 0xFFFFFFFFFF
        for p in ans:
            a &= int(p, 2)
        return a
    else:
        return 0

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

Solution code:
import itertools

line = str(input())
lines = line.split(' ')

n = int(lines[0])
k = int(lines[1])

d = list()

line = str(input())
lines = line.split(' ')

for l in lines:
    d.append(int(l))

#for i in range(0,n):
#    print(d[i])

ds = list()

for i in range(0,n):
    for len in range(1,n-i+1):
        sum = 0
        for j in range(i,i+len):
            sum += d[j]
        ds.append(sum)

#for t in ds:
#    print(str(t) + " ", end='')

max = 0
for m in list(itertools.combinations(ds,k)):
    sum = m[0]
    for i in range(1,k):
        sum = sum & m[i]            
    
    if max < sum :
        max = sum

print (max)

Submission status: Time Limit Exceeded

Solution code:
n,k = map(int,input().split())
lis = list(map(int,input().split()))
li = []
num = 0
for i in range(n):
  for j in range(i,n):
    num += lis[j]
    li.append(num)
  num = 0
li.sort(reverse = True)
ans = li[0]
nu = []
h = 0
cou = 2 ** 35
while len(nu) < k:
  cou /= 2
  while li[h] >= cou:
    nu.append(li[h])
    h += 1
  # print(nu)
ans = {(2**35-1,0)}
for i in range(len(nu)):
  gone = set()
  for num,co in ans:
    if co < k:gone.add((num&nu[i],co+1))
    gone.add((num,co))
  ans = gone
an = 0
for v,b in ans:
  if b == k:an = max(an,v)
print(an)

Submission status: Time Limit Exceeded

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

tmp = [0 for _ in range(N*(N+1)//2)]
ind = 0
for l in range(N):
    sum_tmp = A[l]
    tmp[ind] = sum_tmp
    ind += 1
    for r in range(l+1,N):
        sum_tmp += A[r]
        tmp[ind] = sum_tmp
        ind += 1


tmp = sorted(tmp,reverse=True)

tmp2 = set(list(itertools.combinations(tmp,K)))

ans = 0
for T in tmp2:
    ans_tmp = T[0]
    for t in T:
        ans_tmp = int(ans_tmp and t)

    ans = max(ans,ans_tmp)
    # if ans < ans_tmp:
    #     ans = ans_tmp
    #     print(T)


print(ans)







# B1 = A
# B2 = A[1:]
# ans = sum(B2)

# print(ans)

Submission status: Time Limit Exceeded

Solution code:
#coding:utf-8
n,k  = map(int,input().split())
ls_a  = list(map(int,input().split()))
target = int(k*(k+1)/2)
ls_target = []
i = 1
while(1):
    for j in range(i):
        temp = ls_a[j:(len(ls_a)-i+j+1)]
        ls_target.append(temp)
    #if len(ls_target)>=k:
    if i==n:
        break
    i+=1

def check(num):
    for i in range(100):
        if (num < 2**i):
            return i-1, 2**(i-1)
mx = sum(ls_target[0])
digit, mx_bin = check(mx)
ans = 0
for d in range(digit+1):
#    print (mx_bin)
#    print (ls_target)
    new_idx = []
    cnt = 0
    for idx,ls in enumerate(ls_target):
        if sum(ls) & mx_bin > 0:
            cnt += 1
            new_idx.append(idx)
    if cnt >= k:
        ls_target = [ls_target[i] for i in new_idx]
        ans += mx_bin
    mx_bin = int(mx_bin/2)
print (ans)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
import itertools

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

# beauty を網羅
def beauty(n, lst):
    lst_beauty = list([sum(lst[i:j]) for i in range(n) for j in range(i+1, n+1)])
    return lst_beauty
lst_beauty = beauty(N, lst_a)


# AND 演算すべき beauty の組み合わせをピックアップ
def pickup_beauty(k, lst):
    lst_c = list(itertools.combinations(lst, k))
    return lst_c
lst_pickup = pickup_beauty(K, lst_beauty)


# ピックアップした組合せのうち、最大の値を求める
def and_max(lst):
    val=0
    for p in lst:
        tmp = p[0]
        for i in p:
            tmp &= i
        if tmp > val:
            val = tmp
    return val
print(and_max(lst_pickup))

Submission status: Time Limit Exceeded

Solution code:
def b_len(v):
    return len(bin(v)[2:])


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)

pw = b_len(ps[k - 1])
MOD = 2 ** pw

nps = [set() for _ in range(pw)]
len_nps = [0 for _ in range(len(nps))]
for i, p in enumerate(ps, 0):
    for j in range(pw):
        if 1 << j & p:
            nps[j].add(i)
            len_nps[j] += 1
ans = 0
buf = set(range(len(ps)))
for j in range(pw)[::-1]:
    if len_nps[j] >= k:
        x = buf & nps[j]
        if len(x) >= k:
            buf = x
            ans += 2 ** j

print(ans)

Submission status: Time Limit Exceeded

Solution code:
def show(target):
    for i in range(len(target)):
        print(bin(target[i])[2:].zfill(8))
    print("len:{}".format(len(target)))


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

# ありうる部分数列を列挙
beauty = []
for i in range(n):
    for j in range(i + 1, n + 1):
        beauty.append(sum(a[i:j]))
N = [i for i in range(n)]  # N = bit探索する桁数
"""
# Nの要素の全組み合わせが出力
for i in range(1, 1 << len(N)):
    beauty.append(0)
    # print(beauty)
    output = []
    for j in range(len(N)):
        if ((i >> j) & 1) == 1:
            beauty[-1] += a[j]
            output.append(a[j])  # このjが今回探索するindexの1つ
            # print("{}:{}".format(output, beauty[-1]))
"""
beauty = sorted(beauty)
target = [beauty[0]]
for i in range(1, len(beauty)):
    if beauty[i] != beauty[i - 1]:
        target.append(beauty[i])
digit = 0
# print(target)
targetDigit = target[-1]
while targetDigit > 0:
    digit += 1
    targetDigit = int(targetDigit / 2)
out = 0
while digit > 0:
    # target = list(reversed(sorted(target)))
    # print(digit)
    # show(target)
    count = 0
    for i in range(len(target)):
        if target[i] >> (digit - 1) == 1:
            count += 1
    if count >= k:
        out += 2 ** (digit - 1)
        i = 0
        while i < len(target):
            if target[i] >> (digit - 1) != 1:
                del (target[i])
            else:
                i += 1
    i = 0
    while i < len(target):
        target[i] &= (1 << (digit - 1)) - 1
        if target[i] == 0:
            del (target[i])
        else:
            i += 1
    digit -= 1
    # print("out:{}".format(out))
print(out)
"""
8 4
1 1 1 1 1 1 1 1 
"""

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):
	s = 0
	for j in range(i, N):
		s = s + a[j]
		b.append(s)
max_b = len(bin(max(b))) - 2
#print(b, max_b)
ans = 0
for i in range(max_b):
	p = 0
	c = 2 ** (max_b - 1 - i)
	#print("i=",i, "c=",c)
	for j in b:
		if (ans + c) & j == ans + c:
			#print("j=",j)
			p = p + 1
	if p >= K:
		ans = ans + 2 ** (max_b - 1 - i)
		#print("ans=",ans)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

N,K=map(int,input().split())
A=list(map(int,input().split()))
A.insert(0,0)
A=np.array(A).cumsum()

x=A[-1]
while True:
    ans=0
    for i in range(N):
        for j in range(i+1,N+1):
            y=A[j]-A[i]
            if x&y == x: ans+=1
    if ans>=K:
        print(x)
        break
    x-=1

Submission status: Time Limit Exceeded

Solution code:
import numpy as np


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

s = [0]
x = 0
for i in range(n):
    x += a[i]
    s.append(x)
    

cache = 0
s_bin = []

for i in range(n+1):
    for j in range(i+1, n+1):
        cache  = s[j] - s[i]
        mozi = list(map(int, list(bin(cache)[2:].zfill(30))))
        s_bin.append(mozi)


s_bin = np.array(s_bin)
kakui = np.sum(s_bin, axis=0)  

ans = 0

for i in range(0, 30):
    if kakui[i] >= k:
        ans += 2**(29-i)
        s_bin = s_bin[s_bin[:, i] == 1]
        kakui = np.sum(s_bin, axis=0)
        
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import itertools as tool

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

beautifuls = []
for i in range(0, len(a)):
    for j in range(i+1, len(a)+1):
        beautiful = sum(a[i:j])
        beautifuls.append(beautiful)

beau_K = list(tool.combinations(beautifuls, K))
ands = []
for combi in beau_K:
    for i in range(K):
        if i == 0:
            tmp = combi[0]
        else:
            tmp = eval(str(bin(tmp & combi[i])))
    ands.append(eval(str(tmp)))
print(max(ands))

Submission status: Time Limit Exceeded

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

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

dp = [[-1 for k in range(1000*(1000+1)//2+1)] for n in range(1000+1)]

def generate_substring(a):
    for i in range(N):
        for j in range(i, N):
            yield a[i:j+1]

def find_max_and(i, k):

    if dp[i][k] >= 0:
        return dp[i][k];
    
    if i == N_SUBSTR:
        res = 0xFFFFFFFF if k == 0 else 0
    elif k == 0:
        res = find_max_and(i + 1, k)
    else:
        a = find_max_and(i + 1, k)
        b = find_max_and(i + 1, k - 1) & scores[i]
        
        res = b if a == 0xFFFF else max(a, b)

    dp[i][k] = res
    return res

scores = list(map(sum, generate_substring(a)))
N_SUBSTR = len(scores)
print(find_max_and(0, K))

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
import itertools

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

# beauty を網羅
def beauty(n, lst):
    lst_beauty=[]
    for i in range(n):
        for j in range(i+1, n+1):
            # print(lst[i:j])
            lst_beauty.append(sum(lst[i:j]))
    return lst_beauty
lst_beauty = beauty(N, lst_a)


# AND 演算すべき beauty の組み合わせをピックアップ
def pickup_beauty(k, lst):
    lst_c = list(itertools.combinations(lst, k))
    return lst_c
lst_pickup = pickup_beauty(K, lst_beauty)


# ピックアップした組合せのうち、最大の値を求める
def and_max(lst):
    val=0
    for p in lst:
        tmp = p[0]
        for i in p:
            tmp &= i
        if tmp > val:
            val = tmp
    return val
print(and_max(lst_pickup))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np


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

s = [0]
x = 0
for i in range(n):
    x += a[i]
    s.append(x)
    

cache = 0
s_bin = []

for i in range(n+1):
    for j in range(i+1, n+1):
        cache  = s[j] - s[i]
        mozi = list(map(int, list(str(bin(cache))[2:].zfill(30))))
        s_bin.append(mozi)


s_bin = np.array(s_bin)
kakui = np.sum(s_bin, axis=0)  

ans = 0

for i in range(0, 30):
    if kakui[i] >= k:
        ans += 2**(29-i)
        s_bin = s_bin[s_bin[:, i] == 1]
        kakui = np.sum(s_bin, axis=0)
        
print(ans)

Submission status: Time Limit Exceeded

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

A2=[0]
sum=0
for i in A:
    A2.append(i+sum)
    sum+=i

#print(A2)
A3=[]
for i in range(len(A2)):
    for k in range(len(A2)-i-1):
        A3.append(A2[k+1+i]-A2[i])
A3.sort()
#print(A3)
#print(len(A3))

tmparr=[]
for i in range(50):
    if 2**i-1>A3[-1]:
        break
    tmparr.append(2**i-1)
#print(tmparr)



for i in tmparr[::-1]:
    rem=len(A3)-bisect.bisect_right(A3,i)
    if rem>K:
        A4=A3[-rem:]
        break
#print(A4)

#A5=[]
#for i in A4:
#    A5.append(bin(i))

result=[]
c = list(itertools.combinations(A4,K));
for i in c:
    init=i[0]
    for k in i:
        init = init & k
    result.append(init)
#print(result)
print(max(result))

Submission status: Time Limit Exceeded

Solution code:
from itertools import accumulate
from itertools import combinations

N, K = [int(x) for x in input().strip().split(" ")]
As = [0] + [int(x) for x in input().strip().split(" ")]

aAs = list(accumulate(As))

keta = 41
d = [set() for _ in range(41)]

for i, j in combinations(range(N), 2):
    rr = aAs[j + 1] - aAs[i]
    rr = "{0:041d}".format(int(bin(rr)[2:]))
    for k in range(keta):
        if rr[k] == "1":
            d[k].add((i, j))

for i in range(N):
    j = i
    rr = As[i+1]
    rr = "{0:041d}".format(int(bin(rr)[2:]))
    for k in range(keta):
        if rr[k] == "1":
            d[k].add((i, j))

candidates = None
rr = 0
for i, ss in enumerate(d):
    if len(ss) < K:
        continue
    if candidates == None:
        candidates = ss
        rr += pow(2, 40-i)
    else:
        xx = ss & candidates
        if len(xx) < K:
            continue
        candidates = xx
        rr += pow(2, 40-i)
print(rr)

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations
from functools import reduce

N, K = tuple(map(int, input().split()))
nums = [int(x) for x in input().split()]

scores = [sum(nums[i:j+1]) for i in range(N) for j in range(i + 1, N)]
print(max([reduce(lambda x, y: x & y, comb) for comb in combinations(set(scores), K)]))

Submission status: Time Limit Exceeded

Solution code:
N, K = map(int, input().split(" "))
Ax = list(map(int, input().split(" ")))
"""
N, K = 8, 4
Ax = [9,1,8,2,7,5,6,4]
"""

R = []
for i in range(len(Ax)):
    for j in range(i+1, len(Ax)+1):
        #print("Ax[%d] + Ax[%d] = %d" % (i, j, sum(Ax[i:j])))
        R.append(sum(Ax[i:j]))

R.sort(reverse=True)

from itertools import combinations as cmb
from functools import reduce

def f_and(x, y):
    return x and y

A = map(lambda x:reduce(f_and, x), list(cmb(R, K)))
A = list(A)
print(max(A))

Submission status: Time Limit Exceeded

Solution code:
n,k=map(int,input().split())
*a,=map(int,input().split())
l=[]
for i in range(n):
    t=0
    for j in range(i,n):
        t+=a[j]
        l.append(t)
list=[[]for _ in range(50)]
for t in l:
    u=bin(t)[2:][::-1]
    for i,v in zip(range(50),u):
        if int(v):list[i].append(t)
for t in list[::-1]:
    if len(t)>=k:
        import itertools
        m=0
        for v in itertools.combinations(t,k):
            u=v[0]
            for i in v[1:]:
                u&=i
            m=max(m,u)
        print(m)
        exit()

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
n,k=map(int, input().split())
a=list(map(int, input().split()))
a2=[0]
for i in a:
    a2.append(a2[-1]+i)
c=dict()
for i in range(n+1):
    for j in range(i,n+1):
        if j>i:
            c[a2[j]-a2[i]]=c.get(a2[j]-a2[i],0)+1
keys=list(c.keys())
keys.sort()
keys2=[bin(i).split('b')[1] for i in keys]
maxb=len(keys2[-1])
keys2=[i.zfill(maxb) for i in keys2]
bin_list=[]
for i in keys2:
    bin_list.append(list(map(int,list(i))))
bin_list=np.array(bin_list)
for i,j in enumerate(keys):
    bin_list[i]=bin_list[i]*c[j]
bin_list
ans=0
l=0
while l<maxb:
    if bin_list[:,l].sum()>=k:
        ans+=2**(maxb-l-1)
        bin_list=bin_list[bin_list[:,l]>0]
    l+=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations
from functools import reduce

N, K = tuple(map(int, input().split()))
nums = [int(x) for x in input().split()]

scores = [sum(nums[i:j+1]) for i in range(N) for j in range(i + 1, N)]
print(max([reduce(lambda x, y: x & y, comb) for comb in combinations(scores, K)]))

Submission status: Time Limit Exceeded

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

import numpy as np
import itertools


def list_subarray_sums(arr, length):
    cumsum = np.insert(np.cumsum(arr), 0, 0)
    for l in range(1, length + 1):
        for i in range(length - l + 1):
            yield cumsum[l + i] - cumsum[i]


def logical_and(arr, idx=0, current=0):
    if idx >= len(arr):
        return current

    if idx == 0:
        current = arr[0]
    else:
        current = current & arr[idx]

    if current == 0:
        return 0
    return logical_and(arr, idx + 1, current)


if __name__ == '__main__':
    N, K = map(int, input().split(' '))
    A = np.array(list(map(int, input().split(' '))))

    sums = set(list_subarray_sums(A, N))
    combinations = itertools.combinations(sums, K)
    ands = map(lambda arr: logical_and(arr), combinations)
    print(max(ands))

Submission status: Time Limit Exceeded

Solution code:
import copy
N,K = map(int,input().split())
A = map(int,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])

max_num = max(score)
i = 0
while(True):
    if(2**(i+1)>max_num):
        break
    else:
        i+=1

ans = 0
for j in range(i,-1,-1):
    if len(score)<K:
        break
    count = 0
    score_a = []
    score_b = []
    for k in range(len(score)):
        if score[k] >= 2**j:
            count += 1
            score[k] -= 2**j
            score_a.append(score[k])
        score_b.append(score[k])
    if count >= K:
        ans += 2**j
        score = copy.deepcopy(score_a)
    else:
        score = copy.deepcopy(score_b)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import itertools

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

count = 1

sum_list = []

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

max_val = 0

pair = list(set(itertools.permutations(sum_list,K)))

for i in pair:
    tmp_val = i[0]
    for j in i[1:]:
        tmp_val = tmp_val&j
    if max_val < tmp_val:
        max_val = tmp_val

print(max_val)

Submission status: Time Limit Exceeded

Solution code:
import itertools


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

L = []

for l in range(N):
    for k in range(N+1):
        if l < k:
            L.append(sum(A[l:k]))

Q = list(itertools.combinations(L, K))

R = 0

for q in Q:
    for i in range(len(q)):
        if i == 0:
            r = q[i]
        else:
            r = r & q[i]
    R = max(R, r)

print(R)

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations
from functools import reduce

N, K = tuple(map(int, input().split()))
nums = [int(x) for x in input().split()]

scores = []

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

print(max([reduce(lambda x, y: x & y, comb) for comb in combinations(scores, K)]))

Submission status: Time Limit Exceeded

Solution code:
import itertools

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

count = 1

sum_list = []

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

max_val = 0

for i in itertools.permutations(sum_list,K):
    tmp_val = i[0]
    for j in i[1:]:
        tmp_val = tmp_val&j
    if max_val < tmp_val:
        max_val = tmp_val

print(max_val)

Submission status: Time Limit Exceeded

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

max_num = max(score)
i = 0
while(True):
    if(2**(i+1)>max_num):
        break
    else:
        i+=1

ans = 0
for j in range(i,-1,-1):
    count = 0
    score_a = []
    score_b = []
    for k in range(len(score)):
        if score[k] >= 2**j:
            count += 1
            score[k] -= 2**j
            score_a.append(score[k])
        score_b.append(score[k])
    if count >= K:
        ans += 2**j
        score = copy.deepcopy(score_a)
    else:
        score = copy.deepcopy(score_b)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N, K = map(int, input().split())
a = list(map(int, input().split()))
array = []
result = 0

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

x = len(array)
case = [0 for _ in range(x)]

for i in range(K):
    case[i] = i+1

flag = True
while flag:
    num = array[case.index(1)]
    for i in range(2, K+1):
        num = num & array[case.index(i)]
    result = max(result, num)
    for i, j in zip(range(K, 0, -1), range(1, K+1)):
        if case.index(i) != x-j:
            case[case.index(i)+1] = i
            case[case.index(i)] = 0
            for k in range(1, K-i+1):
                case[case.index(i+k)] = 0
                case[case.index(i)+k] = i+k
            break        
        if i == 1:
            flag = False
print(result)

Submission status: Time Limit Exceeded

Solution code:
import itertools as iter

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

beauty_list = []
for i in range(len(a) + 1):
    for element in iter.combinations(a, i):
        beauty = sum(element)
        beauty_list.append(beauty)

ans_list = []
for combi in iter.combinations(beauty_list, k):
    ans = combi[0]
    for element in combi:
        ans &= element
    ans_list.append(ans)

print(max(ans_list))

Submission status: Time Limit Exceeded

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

import itertools


def list_subarray_sums(arr):
    for start in range(len(arr)):
        s = 0
        for end in range(start, len(arr)):
            s = s + arr[end]
            yield s


def logical_and(arr, idx=0, current=0):
    if idx >= len(arr):
        return current

    if idx == 0:
        current = arr[0]
    else:
        current = current & arr[idx]

    if current == 0:
        return 0
    return logical_and(arr, idx + 1, current)


if __name__ == '__main__':
    N, K = map(int, input().split(' '))
    A = list(map(int, input().split(' ')))

    sums = set(list_subarray_sums(A))
    combinations = itertools.combinations(sums, K)
    ands = map(lambda arr: logical_and(arr), combinations)
    print(max(ands))

Submission status: Time Limit Exceeded

Solution code:
from statistics import mean, median, variance, stdev
import itertools
N, K = map(int, input().split())
a = list(map(int, input().split()))

ans = 0
a_sum = [a[0]]

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

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


a_list = list(itertools.combinations(a_bubun, K))

max_point = 0

for i in range(len(a_list)-1):
    i_point = []
    for j in a_list[i]:
        i_point.append(sum(j))
    i_seki = i_point[0]
    for j in i_point:
        i_seki = i_seki & j
    max_point = max(max_point, i_seki)

print(max_point)

Submission status: Time Limit Exceeded

Solution code:
import itertools

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

sumList = []
for i in range(N+1):
    for j in range(i+1, N+1):
        jsum = sum(aList[i:j])
        sumList.append(jsum)

pq = list(itertools.combinations(sumList, K))
ac = 0


def get_all_and(glist):
    k = glist[0]
    for u in glist[1:]:
        k = k & u
    return k


for p in pq:
    pa = get_all_and(p)
    if pa > ac:
        ac = pa

print(ac)

Submission status: Time Limit Exceeded

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


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


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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 =[0L for i in range(N)]
    for i in range(N):
        A.append(long(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 = 0

    is_use = [True for i in range(len(parts))]
    for i in range(45):
        mask = (1 << 44 - i)
        count = 0
        is_use_save = copy.deepcopy(is_use)
        for j in range(len(parts)):
            if parts[j] & mask != 0 and is_use[j] == True:
                is_use[j] = True
                count += 1
            else:
                is_use[j] = False
        if count >= K:
            S += mask
        else:
            is_use = copy.deepcopy(is_use_save)


    print S

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import itertools


def p(label, val):
    # print("[%s] %s" % (label, val))
    pass


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

sumList = []
for i in range(N+1):
    for j in range(i+1, N+1):
        p("origin list", aList[i:j])
        jsum = sum(aList[i:j])
        sumList.append(jsum)

p("sumList", sumList)


pq = list(itertools.combinations(sumList, K))
p("pq", pq)

ac = 0


def get_all_and(glist):
    k = glist[0]
    for u in glist[1:]:
        k = k & u
    return k


for p in pq:
    pa = get_all_and(p)
    if pa > ac:
        ac = pa

print(ac)