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: Runtime Error

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)
ans = 0
for i in range(40,-1,-1):
    count = 0
    score_a = []
    for j in s:
        if (j & (1<<i)):
            count += 1
            score_a.append(j)
    if count >= K:
        ans += (1<<i)
        s = [i for i in score_a]
 
print(ans)

Submission status: Runtime Error

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)
ans = 0
for i in range(40,-1,-1):
    count = 0
    score_a = []
    for j in score:
        if (j & (1<<i)):
            count += 1
            score_a.append(j)
    if count >= K:
        ans += (1<<i)
        score = [i for i in score_a]
 
print(ans)

Submission status: Runtime Error

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+1,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: Runtime Error

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

ds = []

for i in range(0,n):
    sum = 0
    for j in range(i,n):
        sum += d[j]
        ds.append(sum)
        //print(str(sum) + " ",end='')
    //print()

ds.sort(reverse=True)

sum = 0
for i in range(40,-1,-1):
    count = 0
    o = []
    for dss in ds:
        if dss & (1 << i) > 0 :
            count += 1
            o.append(dss)

    if count >= k :
        sum += 1 << i
        ds = o

print(sum)

Submission status: Runtime Error

Solution code:
a,b = map(int,input().split())

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

l = []
for i in range(a):
    l += [c[i]]
    for j in range(i+1,a):
        l += [l[-1] + c[j]]
print(l)
co = 0
la = 0
for i in range(40,-1,-1):
	for j in range(len(l)):
		if l[j] & (la + 2 ** i) == (la + 2 ** i):
			co += 1
	if co >= b:
		la += (2 ** i)
	co = 0

print(la)

Submission status: Runtime Error

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

sum_a = 0
for i in range(N):
    sum_a += a[i]

result = sum_a
f = []
l = []
for i in range(K-1):
    if sum_a - (a[0]+sum(f)) < sum_a - (a[-(i+1)]+sum(l)):
        l.append(a.pop(-(i+1)))
        temp = sum_a - sum(l)
    else:
        f.append(a.pop(0))
        temp = sum_a - sum(f)
    result = result & temp
print(result)

Submission status: Runtime Error

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

sum_a = 0
for i in range(N):
    sum_a += a[i]

result = sum_a
f = [0]
l = [0]
for i in range(K-1):
    if sum_a - (a[0]+sum(f)) <= sum_a - (a[-(i+1)]+sum(l)):
        l.append(a[-(i+1)])
        temp = sum_a - sum(l)
    else:
        f.append(a[0])
        temp = sum_a - sum(f)
    result = result & temp
print(result)

Submission status: Runtime Error

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(sum(temp))
    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 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: Runtime Error

Solution code:
import itertools
n,d = map(int,input().split())
a = list(map(int,input().split()))
 
b = []
c = 0
m = n
for i in range(n):
    for j in range(m):
        for k in range(j+1):
            c += a[k]
        b.append(c)
        c = 0
    m -= 1
    del a[0]
b = list(set(b))
 
h = list(itertools.permutations(range(len(b)), d))
p = []
q = []
for i in range(len(h)):
    for j in range(d):
        p.append(b[h[i][j]])
        
    for k in range(d-1):
        k += 1
        if k == 1:
            e = bin(p[k])
            f = bin(p[k-1])
            g = int(e,0) & int(f,0)
        g = int(bin(g),0) & int(bin(p[k]),0)
    q.append(g)
    p = []
 
print(max(q))

Submission status: Runtime Error

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

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

A = [sum(S)]

for i in range(K):
    A.append(sum(S[1:]))
    A.append(sum(S[:-1]))
    if S[0] >= S[-1]:
        del S[-1]
    else:
        del S[0]

A.sort()
A.reverse()

print(A[K-1])

Submission status: Runtime Error

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

def dacao(b):
	aller = [0]
	sumer = 0
	for i in b:
		sumer += i
		aller.append(sumer)
		
	superlist = dict()
	for i in range(len(b)):
		for j in range(i + 1, len(b) + 1):
			temp = bin(aller[j] - aller[i])[::-1]
			x = []
			for k in range(len(temp)):
				if temp[k] == '1':
					x.append(k)
			for m in x:
				try:
					superlist[m].append(aller[j] - aller[i])
				except:
					superlist[m] = [aller[j] - aller[i]]
				
	for i in sorted(superlist)[::-1]:
		if len(superlist[i]) >= a[1]:
			li = list(map(lambda x : x - 2**i, superlist[i]))
			return li, i
			break
		return [], -1
			
def cao(b):
	aller = [0]
	sumer = 0
	superlist = dict()
	for i in b:
		temp = bin(i)[::-1]
		x = []
		for k in range(len(temp)):
			if temp[k] == '1':
				x.append(k)
		for m in x:
			try:
				superlist[m].append(i)
			except:
				superlist[m] = [i]
				
	for i in sorted(superlist)[::-1]:
		if i == 0:
			return [], -1
		if len(superlist[i]) >= a[1]:
			li = list(map(lambda x : x - 2**i, superlist[i]))
			return li, i
		return [],-1

sumx = 0
gan1, gan2 = dacao(c)
while gan2 != -1:
	sumx += 2**gan2
	gan1, gan2 = cao(gan1)
	
print(sumx)

Submission status: Runtime Error

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

def dacao(b):
	aller = [0]
	sumer = 0
	for i in b:
		sumer += i
		aller.append(sumer)
		
	superlist = dict()
	for i in range(len(b)):
		for j in range(i + 1, len(b) + 1):
			temp = bin(aller[j] - aller[i])[::-1]
			x = []
			for k in range(len(temp)):
				if temp[k] == '1':
					x.append(k)
			for m in x:
				try:
					superlist[m].append(aller[j] - aller[i])
				except:
					superlist[m] = [aller[j] - aller[i]]
				
	for i in sorted(superlist)[::-1]:
		if len(superlist[i]) >= a[1]:
			li = list(map(lambda x : x - 2**i, superlist[i]))
			return li, i
			break
		return [], -1
			
def cao(b):
	aller = [0]
	sumer = 0
	superlist = dict()
	for i in b:
		temp = bin(i)[::-1]
		x = []
		for k in range(len(temp)):
			if temp[k] == '1':
				x.append(k)
		for m in x:
			try:
				superlist[m].append(i)
			except:
				superlist[m] = [i]
				
	for i in sorted(superlist)[::-1]:
		if i == 0:
			return [], -1
		if len(superlist[i]) >= a[1]:
			li = list(map(lambda x : x - 2**i, superlist[i]))
			return li, i
			break

sumx = 0
gan1, gan2 = dacao(c)
while gan2 != -1:
	sumx += 2**gan2
	gan1, gan2 = cao(gan1)
	
print(sumx)

Submission status: Runtime Error

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 = [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]

L = L[:K]

R = '0b'

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

print(int(R, 2))

Submission status: Runtime Error

Solution code:
import numpy as np


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

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


s_bin = []

for i in range(n):
    for j in range(i+1, n+1):
        cache  = s[j] - s[i]
        mozi = []
        for i in range(30):
            r = cache % 2
            mozi.append(r)
            cache = cache // 2
        mozi.append(cache)

        s_bin.append(reversed(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: Runtime Error

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

Submission status: Runtime Error

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

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

c = []
for i in range(len(a)):
    b = [a[i] & a[j] for j in range(i + 1, len(a))]
    c.extend(b)
    print(c)

for k in range(2, K):
    d = []
    for i in range(1 + k, N):
        b = [a[i] & c[j] for j in range(len(c))]
        d.extend(b)
    
    c = d
print(max(c))

Submission status: Runtime Error

Solution code:
import itertools
n,d = map(int,input().split())
a = list(map(int,input().split()))

b = []
c = 0
m = n
for i in range(n):
    for j in range(m):
        for k in range(j+1):
            c += a[k]
        b.append(c)
        c = 0
    m -= 1
    del a[0]
b = list(set(b))

h = list(itertools.permutations(range(len(b)), d))
p = []
for i in range(len(h)):
    for j in range(d):
        p.append(b[h[i][j]])
        
    for k in range(d-1):
        k += 1
        if k == 1:
            e = bin(p[k])
            f = bin(p[k-1])
            g = int(e,0) & int(f,0)
        g = int(bin(g),0) & int(bin(p[k]),0)
    p = []

print(g)

Submission status: Runtime Error

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+1,N+1):
        b.append(sum(a[i:j]))

b.sort(reverse=True)
if K==1:
    tmp=b[0]
else:
    gmax=None
    for t in range(len(b)-K):
        b_=b[t:]
        for _ in range(K-1):
            max=None
            for k in range(1,len(b_)):
                tmp=b_[0]&b_[k]
                if max is None or tmp>max:
                    max=tmp
                    mind=k
            if gmax is not None:
                if gmax>max:
                    break
            b_[0]=max
            b_.pop(mind)
        if gmax is None or max>gmax:
            gmax=max
        else:
            break

print(gmax)

Submission status: Runtime Error

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

def dacao(b):
	aller = [0]
	sumer = 0
	for i in b:
		sumer += i
		aller.append(sumer)
		
	superlist = dict()
	for i in range(len(b)):
		for j in range(i + 1, len(b) + 1):
			temp = bin(aller[j] - aller[i])[::-1]
			x = []
			for k in range(len(temp)):
				if temp[k] == '1':
					x.append(k)
			for m in x:
				try:
					superlist[m].append(aller[j] - aller[i])
				except:
					superlist[m] = [aller[j] - aller[i]]
				
	for i in sorted(superlist)[::-1]:
		if len(superlist[i]) >= a[1]:
			li = list(map(lambda x : x - 2**i, superlist[i]))
			return li, i
			break
		return [], -1
			
def cao(b):
	aller = [0]
	sumer = 0
	superlist = dict()
	for i in b:
		temp = bin(i)[::-1]
		x = []
		for k in range(len(temp)):
			if temp[k] == '1':
				x.append(k)
		for m in x:
			try:
				superlist[m].append(i)
			except:
				superlist[m] = [i]
				
	for i in sorted(superlist)[::-1]:
		if len(superlist[i]) >= a[1]:
			li = list(map(lambda x : x - 2**i, superlist[i]))
			return li, i
			break
		return [], -1

sumx = 0
gan1, gan2 = dacao(c)
while gan2 != -1:
	sumx += 2**gan2
	gan1, gan2 = cao(gan1)
	
print(sumx)

Submission status: Runtime Error

Solution code:
import numpy as np
import collections as cp
N, K = map(int,input().split())
#print("N,K",N,K)
a_arr = input().split()
tmp = []
for ele in a_arr:
    tmp.append(int(ele))
arr = tmp
sum_arr = []
bin_arr = []
len_arr = []
for i in range(N):
    for j in range(i,N):
        tmpnum = sum(arr[i:j+1])
        sum_arr.append(tmpnum)
        bin_arr.append(bin(tmpnum))
        len_arr.append(len(bin(tmpnum)))

#print(bin_arr)
max_len = max(len_arr) - 2
ans = 0
tmp_arr = sum_arr
loop = 0
end_flag = 0
get_ev = 0
while end_flag==0:
    ans_arr = []
    non_arr = []
    number = 0
    two_pow = pow(2, max_len-1-loop)
    loop += 1
    for sum_tmp in tmp_arr:
        if sum_tmp >= two_pow:
            number +=1
            ans_arr.append(sum_tmp - two_pow)
        else:
            non_arr.append(sum_tmp)
    if number >=K:
        tmp_arr = ans_arr
        ans += two_pow
        get_ev = 1
    elif get_ev == 1:
        end_flag = 1
    else:
        tmp_arr = ans_arr.append(non_arr)
        if loop >= max_len-1:
            end_flag = 1

print(ans)

Submission status: Runtime Error

Solution code:
N,K = map(int,input().split())
A = list(map(int,input().split()))
L = []
for i in range(N):
    for j in range(1,N+1):
        if i > j - 1:
            continue
        else:
            l = A[i:j]
            L.append(sum(l))
L.sort()
border = 2 ** (len(str(bin(L[-K]))[2:]) - 1)
L = [e for e in L if e >= border]
def combinationListRecursive(data, r):
  if r == 0 or r > len(data):
    return []

  result = []
  _combinationListRecursive(data, r, 0, [], result)
  return result


def _combinationListRecursive(data, r, start, progress, result):
  if r == 0:
    result.append(progress)
    return

  for i in range(start, len(data)):
    _combinationListRecursive(data, r - 1, i + 1, progress + [data[i]], result)

result = combinationListRecursive(L,K)
ans_list = []
for list_c in result:
    for e in list_c:
        if e == list_c[0]:
            copy = e
            continue
        AND = copy & e
        if e == list_c[-1]:
            ans = AND
            ans_list.append(ans)
            break
        copy = AND
print(max(ans_list))

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
a = list(map(int, input().split()))
b = []
start = 0
end = N-1
sum1 = 0
sum2 = 0
K -= 1
while K > 0:
    if sum1 + sum2 <= min(sum1 + a[start], sum2 + a[end]):
        b.append(sum1 + sum2)
        if a[start] < a[end]:
            sum1 += a[start]
            start += 1
            K -= (N - end - 1)
        else:
            sum2 += a[end]
            end -= 1
            K -= start
    elif sum1 + a[start] > sum2 + a[end]:
        sum2 += a[end]
        end -= 1
        b.append(sum2)
        K -= start
    else:
        sum1 += a[start]
        start += 1
        b.append(sum1)
        K -= N - end - 1
print(sum(a)-b[-1])

Submission status: Runtime Error

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+1,N+1):
        b.append(sum(a[i:j]))

b.sort(reverse=True)
if K==1:
    tmp=b[0]
else:
    gmax=None
    for t in range(len(b)-K):
        b_=b[t:]
        for _ in range(K-1):
            max=None
            for k in range(1,len(b_)):
                tmp=b_[0]&b_[k]
                if max is None or tmp>max:
                    max=tmp
                    mind=k
            b_[0]=max
            b_.pop(mind)
        if gmax is None or max>gmax:
            gmax=max
        else:
            break

print(gmax)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
from pprint import pprint
import itertools

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)

bin_list = [[0 for _ in range(32)] for _ in range(len(sum_list))]

for i,s in enumerate(sum_list):
    tmps = bin(s)[2:]
    for j, b in enumerate(reversed(tmps)):
        bin_list[i][j] = int(b)

Submission status: Runtime Error

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

T = []
for i in range(n):
    t = 0
    for j in range(i, n):
        t += A[j]
        T.append(t)

T = sorted(T)[::-1]

alen = len(str(T[0]))
blen = len(str(T[1]))

idx = 2
if alen == blen:
    while idx < n:
        if len(str(T[idx])) == alen:
            pass
        else:
            break
        
        idx += 1
else:
    while idx < n:
        if len(str(T[idx])) == blen:
            pass
        else:
            break
        
        idx += 1

W = []
for i in range(idx):
    W.append(T[i])

W = sorted(W, key=lambda x : bin(x).count('1'))

ans = W[0]
for i in range(1, k):
    ans = ans & W[i]

print(ans)

Submission status: Runtime Error

Solution code:
m = input()
n = int(m.split()[0])
k = int(m.split()[1])
l = input()
a = l.split()
b = []
for i in range(int(n*(n+1)/2)+1):
    s = 0
    if i <= n:
        if i == 0:
            continue
        for j in range(i):
            s += int(a[j])
        b.append(s)
    if i > n:
        p = i
        while True:
            p -= n
            n -= 1
            a.remove(a[0])
            if (p <= n):
                break
        if p == 0:
            continue
        for j in range(p):
            s += int(a[j])
        b.append(s)
print(b)

Submission status: Runtime Error

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

import numpy as np
import itertools

and_memo = {}


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, lenght, idx=0):
    if idx == lenght - 1:
        return arr[-1]
    if arr[idx] == 0:
        return 0

    key = tuple(arr[idx:])
    if key in and_memo:
        return and_memo[key]

    result = arr[idx] & logical_and(arr, lenght, idx + 1)
    and_memo[key] = result
    return result


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, K), combinations)

    print(max(ands))

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
from pprint import pprint
import itertools

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 = [] 

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

bin_list = [[0 for _ in range(32)] for _ in range(len(sum_list))]

for i,s in enumerate(sum_list):
    tmps = str(bin(s))[2:]
    for j, b in enumerate(reversed(tmps)):
        bin_list[i][j] = int(b)

Submission status: Runtime Error

Solution code:
import math
import copy

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

# n未満の数字から2個ずつのリストを作って美しさリストを返す
def mapper(li):
    beau=[]
    for i in range(len(li) ):
        bb=li[i]
        for j in range(i,len(li)):
            if j > i:
                bb+=li[j]
            beau.append(bin(bb)[2:].zfill(30) )
    return(beau)

blist=mapper(a)
blist.sort()
blist.reverse()
clist=copy.deepcopy(blist)
#print(blist)

bblist=[]

ans=0
flg=0
inf=9999999999
valid=[]
maxi=inf
for i in range(30):
    tmp=0
    blist.sort(key=lambda x:x[i])
    blist.reverse()

    for j in range(len(blist) ):
        tmp+=int(blist[j][i])
        if tmp>=K:
            valid.append(i)
            if blist[j][i]=='0':
                if blist[j] in clist:
                    clist.remove(blist[j])
    if tmp>=K:
        blist=clist

#print(set(valid)) 
#print(clist)

for i in set(valid):
    ans+= math.pow(2,30-i-1)

print(int(ans))

Submission status: Runtime Error

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

sum_a = 0
for i in range(N):
    sum_a += a[i]

temp = 0
for i in range(K-1):
    if sum_a - a[0] < sum_a - a[-1]:
        temp = sum_a - a.pop(-1)
    else:
        temp = sum_a - a.pop(0)
    result = sum_a & temp
print(result)

Submission status: Runtime Error

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

import numpy as np
import itertools
import sys

sys.getrecursionlimit(3000) 

and_memo = {}


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, lenght, idx=0):
    if idx == lenght - 1:
        return arr[-1]
    if arr[idx] == 0:
        return 0

    key = tuple(arr[idx:])
    if key in and_memo:
        return and_memo[key]

    result = arr[idx] & logical_and(arr, lenght, idx + 1)
    and_memo[key] = result
    return result


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, K), combinations)

    print(max(ands))

Submission status: Runtime Error

Solution code:
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
b = []
c = []
i = 0
while True:
    m = n - i
    for j in range(i+1):
        c = a[j:j + m]
        if sum(c) != 0:
            b.append(sum(c))
    i += 1
    if i > n*(n-1)//2:
        break
d = sorted(b, reverse=True)
ans = d[0]
for i in range(k):
    if i+1 == k:
        break
    ans = ans & d[i+1]
print(ans)n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
b = []
c = []
i = 0
while True:
    m = n - i
    for j in range(i+1):
        c = a[j:j + m]
        if sum(c) != 0:
            b.append(sum(c))
    i += 1
    if i > n*(n-1)//2:
        break
d = sorted(b, reverse=True)
ans = d[0]
for i in range(k):
    if i+1 == k:
        break
    ans = ans & d[i+1]
print(ans)

Submission status: Runtime Error

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, lenght, idx=0):
    if idx == lenght - 1:
        return arr[-1]
    if arr[idx] == 0:
        return 0

    return arr[idx] & logical_and(arr, lenght, idx + 1)


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, K), combinations)

    print(max(ands))

Submission status: Runtime Error

Solution code:
a,b = map(int,input().split())

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

aa = []
aa.append(0)
aa.append(c[0])

l = []
for i in range(1,a):
	aa.append(aa[i]+c[i])
d = 0
p = len(aa)
for i in range(p):
	for j in range(i+1,p):
		l.append(aa[j]-aa[i])

l.sort()
l.reverse()
l = list(map(bin,l))
pp = []
for i in range(len(l)-b):
	k = l[i]
	for j in range(1,b):
		k = l[i+b] & k
		if k <= min(pp):
			break
	pp.append(int(k))

pp.sort()
pp.reverse()
print(pp[0])

Submission status: Runtime Error

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

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

dp = [[-1 for k in range(N*(N+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: Runtime Error

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

b = []
c = 0
m = n
for i in range(n):
    for j in range(m):
        for k in range(j+1):
            c += a[k]
        b.append(c)
        c = 0
    m -= 1
    del a[0]
b.sort(reverse=True)

for i in range(d-1):
    i += 1
    if i == 1:
        e = bin(b[i])
        f = bin(b[i-1])
        g = int(e,0) & int(f,0)
    g = int(bin(g),0) & int(bin(b[i]),0)
print(g)

Submission status: Runtime Error

Solution code:
import math
import copy

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

# n未満の数字から2個ずつのリストを作って美しさリストを返す
def mapper(li):
    beau=[]
    for i in range(len(li) ):
        bb=li[i]
        for j in range(i,len(li)):
            if j > i:
                bb+=li[j]
            beau.append(bin(bb)[2:].zfill(30) )
    return(beau)

blist=mapper(a)
blist.sort()
blist.reverse()
clist=copy.deepcopy(blist)
#print(blist)

bblist=[]

ans=0
flg=0
inf=9999999999
valid=[]
maxi=inf
for i in range(30):
    tmp=0
    blist.sort(key=lambda x:x[i])
    blist.reverse()

    for j in range(len(blist) ):
        tmp+=int(blist[j][i])
        if tmp>=K:
            valid.append(i)
            if blist[j][i]=='0':
                if blist[j] in clist:
                    clist.remove(blist[j])
    if tmp>=K:
        blist=clist

#print(valid)
#print(clist)

for i in range(30):
    tmp=0
    for j in range(len(clist)):
        tmp+=int(clist[j][i])
    if tmp>=K:
        ans+= math.pow(2,30-i-1)

print(int(ans))

Submission status: Runtime Error

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

numList=[]
gokei=0
for i in range(n):
  gokei+=aList[i]
  numList.append(gokei)

allList=[]
allList.extend(numList)

for j in range(1,n-1):
  for i in range(j,n):
    tmp=numList[i]-numList[j-1]
    allList.append(tmp)

allList.sort()
allList.reverse()
#print(allList[k-1])

anskoho=[]
for j in range(100):
  tmp=0
  for i in range(0+j,k+j):
    if(i==(0+j)):
      tmp=allList[0+j]
    else:
      tmp=tmp&allList[i]
  anskoho.append(tmp)

anskoho.sort()
anskoho.reverse()

print(anskoho[0])

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
from pprint import pprint
import itertools

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 = [] 

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

bin_list = [[0 for _ in range(32)] for _ in range(len(sum_list))]

for i,s in enumerate( sum_list):
    tmps = str(bin(s))[2:]
    for j,b in enumerate( reversed(tmps)):
        bin_list[i][j] = int(b)

# pprint(bin_list)

sum_bin = []
for j in range(32):
    tmp_bin = 0
    for i in range(len(sum_list)):
        tmp_bin += bin_list[i][j]
    sum_bin.append(tmp_bin)

# pprint(sum_bin)

max_keta = -1
for i,s in enumerate( sum_bin):
    if s >= K:
        max_keta = i

if max_keta == -1:
    print(0)
    exit()

sub_sum_list = [a for a in sum_list if 2**(max_keta) <= a < 2**(max_keta+1)]

# pprint(sub_sum_list)

ans_list = []
for c in itertools.combinations(sub_sum_list, K):
    ans = c[0]
    for s in c[1:K]:
        ans &= s

    ans_list.append(ans)

if len(ans_list) == 0:
    print(0)
else:
    print(max(ans_list))

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
b=[]
tmp=0
for i in range(len(a)):
    tmp=a[i]+tmp
    b.append(tmp)
c=[]
l=len(b)
if (k!=1):
    for i in range(k-1):    
        c.append(b[l-1-i])
    for i in range(k-1):
        for j in range(k-1):
            c.append(b[l-j-1]-b[i])
            if (b[l-j-1]-b[i])<b[-k+1]:
                break 

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

else:
    print(b[l-1])

Submission status: Runtime Error

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

bin_list = [[0 for _ in range(32)] for _ in range(len(sum_list))]

for i,s in enumerate( sum_list):
    tmps = str(bin(s))[2:]
    for j,b in enumerate( reversed(tmps)):
        bin_list[i][j] = int(b)

# pprint(bin_list)

sum_bin = []
for j in range(32):
    tmp_bin = 0
    for i in range(len(sum_list)):
        tmp_bin += bin_list[i][j]
    sum_bin.append(tmp_bin)

# pprint(sum_bin)

max_keta = -1
for i,s in enumerate( sum_bin):
    if s >= K:
        max_keta = i

if max_keta == -1:
    print(0)
    exit()

sub_sum_list = [a for a in sum_list if 2**(max_keta) <= a < 2**(max_keta+1)]

# pprint(sub_sum_list)

ans_list = []
for c in itertools.combinations(sub_sum_list, K):
    ans = c[0]
    for s in c[1:K]:
        ans &= s

    ans_list.append(ans)

if len(ans_list) == 0:
    print(0)
else:
    print(max(ans_list))