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:
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys

input = raw_input().split(" ")
N = int(input[0])
K = int(input[1])

aVec = raw_input().split(" ")
a = []

for i in aVec:
    a.append(int(i))


VecNum = N * (N + 1) / 2


subarray = []

for i in range(0, N):
    a_bubun = a[i:]
    for k in range(0, len(a_bubun)):
        subarray.append(a_bubun[k:])


#print subarray

beauty = []
for i in subarray:
    beauty.append(sum(i))
beauty.sort(reverse=True)


#K個大きい物を選ぶ
aK = []
for i in range(0, K):
    aK.append(beauty[i])


#論理積の最大値なので、最小値を求める
print min(aK)

Submission status: Wrong Answer

Solution code:
import itertools

N,K = map(int, input().split())
nums = list(map(int, input().split()))
temp = []
for s, e in itertools.combinations(range(N+1), 2):
  num = sum(nums[s:e])
  temp.append(num)

temp = sorted(temp, reverse=True)
ans_list = []

for i in range(len(temp)-K):
  a=1073741823
  num = temp[i:K+i]
  for j in num:
    ans = a&j
    a = j
    if j==num[-1]:
      ans_list.append(ans)

print(max(ans_list))

Submission status: Wrong Answer

Solution code:
import math

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

s = sum(a)
_a = sorted(a)

l = []
e = int(math.log(s))
c = 0
for i in range(N):
	n = s - _a[i]
	if int(math.log(n)) == e:
		l.append(n)
		c += 1
		if c == K:
			break
	else:
		e = int(math.log(n))
		l.append(n)
		c = 1

ans = l.pop(0)
for i in l:
	ans &= i
print(ans)

Submission status: Wrong Answer

Solution code:
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
sub_a = []
for i in range(len(a)):
    for j in range(i, len(a)):
        sub_a.append(sum(a[i:j+1]))
index = 0
a_max = max(sub_a)
while a_max >= 2**index:
    index += 1
index -= 1
res = 0
for i in range(index+1):
    sub_a = sorted(sub_a, reverse = True)
    for j in range(len(sub_a)):
        if sub_a[j] - 2**(index-i) < 0:
            break
    num = len(sub_a[:j])
    for l in range(j):
        sub_a[l] -= 2**(index-i)
    if num < k:
        continue
    sub_a = sub_a[:j]
    res += 2**(index-i)
    #print(sub_a, res)

print(res)

Submission status: Wrong Answer

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

beauty.sort()
beauty.reverse()
print(beauty[k-1])

Submission status: Wrong Answer

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


for k in range(K):
    if k == 0:
        ans = tmp[k]
    else:
        ans = int(ans and tmp[k])

print(ans)



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

# print(ans)

Submission status: Wrong Answer

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

for i in range(K):
    if i == 0:
        R = L[i]
    else:
        R = R & L[i]

print(R)

Submission status: Wrong Answer

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

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

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

SUM.sort()
#print(sorted(SUM))
#print([bin(SUM[i]) for i in range(len(SUM))])
#SUM.sort(reverse=True)

for i in range(30,-1,-1):
    COUNT=0
    for s in SUM:
        if s & 1<<i:
            COUNT+=1

    #print(COUNT)#,SUM)

    if COUNT<K:
        continue
    else:
        NLIST=[]
        for s in SUM:
            if s & 1<<i:
                NLIST.append(s)

        SUM=NLIST

    #print(SUM)

ANS=SUM[0]
for s in SUM:
    ANS=ANS & s

print(ANS)

Submission status: Wrong Answer

Solution code:

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

for i in range(N-1):
    a[i+1] += a[i]

seq = []
for i in range(N):
    for sl in range(i+1):
        if sl == 0:
            seq.append(a[N-1-i+sl])
        else:
            seq.append(a[N-1-i+sl] - a[sl-1])

ans = 0
l = len(seq)
for i in range(100,-1,-1):
    cnt = 0
    for j in range(l):
        if seq[j] >= (1<<i):
            cnt += 1

    if cnt >= K:
        ans += (1 << i)
        seq = list(map(lambda x: x - (1 << i),seq))

print(ans)

Submission status: Wrong Answer

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)

Submission status: Wrong Answer

Solution code:

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

for i in range(N-1):
    a[i+1] += a[i]

seq = []
for i in range(N):
    for sl in range(i+1):
        if sl == 0:
            seq.append(a[N-1-i+sl])
        else:
            seq.append(a[N-1-i+sl] - a[sl-1])

ans = 0
l = len(seq)
for i in range(1000,-1,-1):
    cnt = 0
    for j in range(l):
        if seq[j] >= (1<<i):
            cnt += 1

    if cnt >= K:
        ans += (1 << i)
        seq = list(map(lambda x: x - (1 << i),seq))

print(ans)

Submission status: Wrong Answer

Solution code:
N = list(map(int,input().split()))
S = list(map(int,input().split()))
K = [1,2,4,8,16,32,64,128,256,512]
tmp = 0
for i,n in enumerate(K):
    if n >= N[1]:
        tmp = i
        break
print(str(sum(S[tmp:])))

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
a = list(map(int, input().split()))
sums = []
power = 0
ans = 0
powerCount = []

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

maxNum = max(sums)
while (maxNum > 1):
    maxNum = maxNum / 2
    power += 1
sums.sort()

binary = [format(i, "b").zfill(power) for i in sums]
# 各ビットについて、いくつの数字において1があったかをカウントし、K以上あったものの和が答え

for i in range(power):
    if len([num for num in binary if num[i] == "1"]) >= K:
        ans += 2 ** (power - i - 1)
        binary = [num for num in binary if num[i] == "1"]
    else:
        binary = [num for num in binary if num[i] == "0"]


print(ans)

Submission status: Wrong Answer

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

import numpy as np

cumsum = np.cumsum(a) 
lst = np.array([0])
for i in range(N):
  lst = np.append(lst,np.cumsum(a[i:]))

lst = list(lst)
lst.pop(0)
sort_lst = sorted(lst,reverse = True)

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

print(ans)

Submission status: Wrong Answer

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()
pp = [0]
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(k)

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

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
a = list(map(int, input().split()))
s = [0]
for i in range(N):
  s.append(s[i] + a[i])
# print(s)

b = []
for i1 in range(N):
  for i2 in range(i1+1, N+1):
    b.append(s[i2] - s[i1])
# print(b)
b.sort(reverse = True)
# print(b)

for sft in range(32):
  mask = 1 << (31 - sft)
  tmp_b = []
  for i in range(len(b)):
    if (b[i] & mask) != 0:
      tmp_b.append(b[i])
  # print(tmp_b)
  if len(tmp_b) >= K:
    b = tmp_b


ans = b[0]
for i in range(1, K):
  ans &= b[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+1):
    for j in range(i+1,N+1):
        b.append(sum(a[i:j]))
sorted_b = sorted(b)

max_a = sum(a)
border = 1
while max_a > border:
    border = border*2

border /= 2
i = 1
count = 0
while count!=K:
    if border <= sorted_b[-i]:
        count+=1
        i += 1
    else:
        for j in range(1,i):
            sorted_b[-j] -= border
        sorted_b = sorted(sorted_b)
        count = 0
        i = 1
        border /= 2
print(int(sorted_b[-i+1]))

Submission status: Wrong Answer

Solution code:

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

for i in range(N-1):
    a[i+1] += a[i]

seq = []
for i in range(N):
    for sl in range(i+1):
        if sl == 0:
            seq.append(a[N-1-i+sl])
        else:
            seq.append(a[N-1-i+sl] - a[sl-1])

ans = 0
for i in range(1000,-1,-1):
    cnt = 0
    for j in range(N):
        if seq[j] >= (1<<i):
            cnt += 1

    if cnt >= K:
        ans += (1 << i)
        seq = list(map(lambda x: x - (1 << i),seq))

print(ans)

Submission status: Wrong Answer

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

b.sort(reverse=True)
s=b[:K]
if K==1:
    tmp=s[0]
else:
    max=None
    for j in range(N+1-K):
        tmp=b[j]&b[j+1]
        for i in range(j+2,j+K):
            tmp=tmp&b[i]
        if max is None:
            max=tmp
        elif tmp>max:
            max=tmp

print(max)

Submission status: Wrong Answer

Solution code:

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

for i in range(N-1):
    a[i+1] += a[i]

seq = []
for i in range(N):
    for sl in range(i+1):
        if sl == 0:
            seq.append(a[N-1-i+sl])
        else:
            seq.append(a[N-1-i+sl] - a[sl-1])

ans = 0
for i in range(50,-1,-1):
    cnt = 0
    for j in range(N):
        if seq[j] >= (1<<i):
            cnt += 1

    if cnt >= K:
        ans += (1 << i)
        seq = list(map(lambda x: x - (1 << i),seq))

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):
    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:
        t=sorted(t)[::-1]
        m=t[0]
        for i in t[1:k]:
            m&=i
        print(m)
        exit()

Submission status: Wrong Answer

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

p = 1
while k-1 > 0:
    k -= p
    p += 1


for i in range(p-1):
    s = s - min([a[0], a[-1]])
    if a[0] < a[-1]:
        del a[0]
    else:
        del a[-1]

print(s)

Submission status: Wrong Answer

Solution code:
import sys
fin = sys.stdin.readline
from functools import reduce

def range_sum(cuml_list, l, r):
    if l == 0:
        return cuml_list[r]
    else:
        return cuml_list[r] - cuml_list[l - 1]


N, K = [int(elem) for elem in fin().split()]
a_list = [int(elem) for elem in fin().split()]
cuml_list = []
current_sum = 0
for a in a_list:
    current_sum += a
    cuml_list.append(current_sum)

all_beautifulness = []
for l in range(N):
    for r in range(l, N):
        cuml_sum = range_sum(cuml_list, l, r)
        all_beautifulness.append(cuml_sum)

#all_beautifulness.sort()
max_beautifulness = 0
max_bit = len(bin(max(all_beautifulness))[2:])
for bit in range(max_bit, 0, -1):
    base = 2**(bit + 1)
    for i, b in enumerate(all_beautifulness):
        if b >= base:
            all_beautifulness[i] = b - base
    all_beautifulness.sort()
    and_b = all_beautifulness[-K]
    for b in all_beautifulness[-K+1:]:
        and_b &= b
    max_beautifulness = max(max_beautifulness, and_b)
print(max_beautifulness)

Submission status: Wrong Answer

Solution code:
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
b = []
c = []
i = 0
while i < k*10:
    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)
for i in range(k):
    c.append(d[i])

ans = d[0]
for i in range(k):
    if i+1 == k:
        break
    ans = ans & c[i+1]
print(ans)

Submission status: Wrong Answer

Solution code:

def solve(N, K, As):
    alls = []
    for i in range(N):
        for j in range(i + 1, N + 1):
            alls.append("{:033b}".format(sum(As[i:j])))

    def find(i, alls):
        if i > 32:
            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 = 0xFFFFFFFF
        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: Wrong Answer

Solution code:
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)
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
for i in range(start, -1, -1):
    onehot = 2 ** i
    tmp = sum(map(f, sumlist))
    if tmp >= K:
        and_result = list(map(f, sumlist))
        break
# print(sumlist)
# print(and_result)
ans = -1
count = 0
for i in range(len(sumlist)):
    if ans == -1 and and_result[i]:
        ans = sumlist[i]
        count += 1
    elif ans == -1:
        pass
    elif and_result[i]:
        ans = ans & sumlist[i]
        count += 1
    if count == K:
        break
if ans == -1:
    ans = 0
print(ans)

Submission status: Wrong Answer

Solution code:
import math

n, kk = tuple(map(int, input().split()))
sumnum = n * (n + 1) // 2
arr = tuple(map(int, input().split()))
sums = [0 for i in range(sumnum)]
hojo = [0 for i in range(n)]
point = 0
maxe = 0

for i, e in enumerate(arr):
    for k in range(i):
        hojo[k] += e
        sums[point] = hojo[k]
        point += 1
    hojo[i] = e
    sums[point] = hojo[i]
    point += 1

    if maxe < e:
        maxe = e

maxlog = 0
while maxe >= 1 << maxlog:
    maxlog += 1

res = 0

for i in range(maxlog, -1, -1):
    ya = [0 for i in range(len(sums))]
    dab = 0
    gaga = 1 << i
    for e in sums:
        if e & gaga:
            ya[dab] = e
            dab += 1

    if dab >= kk:
        res += gaga
        sums = ya[:dab]

print(res)

Submission status: Wrong Answer

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()
pp = []
for i in range(len(l)-b):
	k = l[i]
	for j in range(1,b):
		k = l[i+b] & k
	pp.append(k)

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

Submission status: Wrong Answer

Solution code:
import numpy as np
n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
sub_a = []
for i in range(len(a)):
    for j in range(i, len(a)):
        sub_a.append(sum(a[i:j+1]))
sub_a = np.array(sub_a)
index = 0
a_max = sub_a.max()
while a_max >= 2**index:
    index += 1
index -= 1
res = 0
for i in range(index+1):
    sub_a = np.sort(sub_a)[::-1]
    for j in range(sub_a.shape[0]):
        if sub_a[j] - 2**(index-i) < 0:
            break
    num = sub_a[:j].shape[0]
    if num < k:
        for l in range(j):
            sub_a[l] -= 2**(index-i)
        #print(sub_a)
        continue
    if num >= k:
        for l in range(j):
            sub_a[l] -= 2**(index-i)
        sub_a = sub_a[:j]
        res += 2**(index-i)
        #print(sub_a, res)

print(res)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
a=[int(i) for i in input().split()]
S=[0]*(N+1)
for i in range(N+1):
  if i==0:
    S[0]=0
  else:
    S[i]=S[i-1]+a[i-1]
length=len(bin(S[N]))-2    
Beauty=[]
for a in range(N+1):
  for b in range(a):
    c=list("0"*(length-len(bin(S[a]-S[b]))+2)+bin(S[a]-S[b]))
    del c[length-len(bin(S[a]-S[b]))+2:length-len(bin(S[a]-S[b]))+4]
    Beauty.append("".join(c))
Beauty.sort()
Beauty=Beauty[::-1]
kiroku=N*(N+1)//2
flag=0
for i in range(length):
  for j in range(N*(N+1)//2):
    if Beauty[j][i]=="0":
      Beauty[j]="0"*length
      kiroku-=1
    if kiroku<=K:
      flag=1
      break
  if flag==1:
    break
ans=2**100-1
for i in range(N*(N+1)//2):
  if Beauty[i]!="0"*length:
    ans=ans&int(Beauty[i],2)
print(ans)

Submission status: Wrong Answer

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(k):
  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: Wrong Answer

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

times=0
while True:
    tmpsum=sum(a[0:N-times])
    ls.append(tmpsum)
    for i in range(0,times):
        tmpsum+=-a[i]+a[i+N-times]
        ls.append(tmpsum)
    times+=1
    if times==N:
        break

ls=sorted(ls, reverse=True)
# print(ls)
maxans=0
for i in range(len(ls)-K-1):
    tmp=ls[i]
    for j in range(i+1,i+K):
        tmp=tmp&ls[j]
    if maxans<tmp:
        maxans=tmp
# print(lsans)
print(maxans)

Submission status: Wrong Answer

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

ans = d[0]
for i in range(k):
    if i+1 == k:
        break
    ans = ans & c[i+1]
print(ans)

Submission status: Wrong Answer

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)

max_and = 0
combination_list = list(iter.combinations(set(beauty_list), k))
for combi in combination_list:
    ans = combi[0]
    for element in combi:
        ans &= element
        if ans == 0:
            break
    #print(ans)
    if ans > max_and:
        max_and = ans

print(max_and)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
a = list(map(int, input().split()))
s = [0]
for i in range(N):
  s.append(s[i] + a[i])
# print(s)

b = []
for i1 in range(N):
  for i2 in range(i1+1, N+1):
    b.append(s[i2] - s[i1])
# print(b)
b.sort(reverse = True)
# print(b)

for sft in range(32):
  mask = 1 << (31 - sft)
  tmp_b = []
  for i in range(len(b)):
    if (b[i] & mask) != 0:
      tmp_b.append(b[i])
  # print(tmp_b)
  if len(tmp_b) >= K:
    b = tmp_b


ans = 0xFFFFFFFF
for i in range(K):
  ans &= b[i]

print(ans)

Submission status: Wrong Answer

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

btyarr = [0 for i in range(N*(N+1)//2)]
cnt = 0
for i in range(N):
    bty = 0
    for j in range(i, N):
        bty += a[j]
        btyarr[cnt] = bty
        cnt += 1

#print(cnt)
#print(len(btyarr))

l = len(btyarr)
s = 0
e = l
for i in range(30, -1, -1):
    b = 1 << i
    cnt = 0
    for j in range(s, e):
        if btyarr[j] & b:
            tmp = btyarr[s+cnt]
            btyarr[s+cnt] = btyarr[j]
            btyarr[j] = tmp
            cnt += 1
    if cnt >= K:
        e = s + cnt

    #print(btyarr)

ans = btyarr[0]
for i in range(K):
    ans &= btyarr[i]

print(ans)

Submission status: Wrong Answer

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


def show(target):
    for i in range(len(target)):
        print(bin(target[i]))


targetDigit = target[-1]
while targetDigit > 0:
    digit += 1
    targetDigit = int(targetDigit / 2)
out = 0
while digit > 0:
    # target = list(reversed(sorted(target)))
    # 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)
        for i in range(len(target)):
            if target[i] >> (digit - 1) == 1:
                target[i] &= (1 << (digit - 1)) - 1
            else:
                target[i] = 0
    digit -= 1
print(out)
"""
8 3
1 1 1 1 1 1 1 1 
"""

Submission status: Wrong Answer

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

R.sort(reverse=True)
A = map(lambda x:reduce(f_and, x), list(cmb(R[:K*2], K)))
A = list(A)
print(max(A))

Submission status: Wrong Answer

Solution code:
import itertools

N,K = map(int, input().split())
nums = list(map(int, input().split()))
temp = []
for s, e in itertools.combinations(range(N+1), 2):
  num = sum(nums[s:e])
  temp.append(num)

temp = sorted(temp, reverse=True)
num = temp[:K]

a=1073741823
for i in num:
  ans = a&i
  a = i

print(ans)

Submission status: Wrong Answer

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

btyarr = [0 for i in range(N*(N+1)//2)]
cnt = 0
for i in range(N):
    bty = 0
    for j in range(i, N):
        bty += a[j]
        btyarr[cnt] = bty
        cnt += 1

#print(cnt)
#print(len(btyarr))

l = len(btyarr)
s = 0
e = l
for i in range(28, -1, -1):
    b = 1 << i
    cnt = 0
    for j in range(s, e):
        if btyarr[j] & b:
            tmp = btyarr[s+cnt]
            btyarr[s+cnt] = btyarr[j]
            btyarr[j] = tmp
            cnt += 1
    if cnt >= K:
        e = s + cnt

    #print(btyarr)

ans = btyarr[0]
for i in range(K):
    ans &= btyarr[i]

print(ans)

Submission status: Wrong Answer

Solution code:
N,K=map(int,input().split())
a=[int(i) for i in input().split()]
S=[0]*(N+1)
for i in range(N+1):
  if i==0:
    S[0]=0
  else:
    S[i]=S[i-1]+a[i-1]
length=len(bin(S[N]))-2    
Beauty=[]
for a in range(N+1):
  for b in range(a):
    c=list("0"*(length-len(bin(S[a]-S[b]))+2)+bin(S[a]-S[b]))
    del c[length-len(bin(S[a]-S[b]))+2:length-len(bin(S[a]-S[b]))+4]
    Beauty.append("".join(c))
Beauty.sort()
Beauty=Beauty[::-1]
kiroku=N*(N+1)//2
flag=0
for i in range(length):
  for j in range(N*(N+1)//2):
    if Beauty[j][i]=="0":
      Beauty[j]="0"*length
      kiroku-=1
    if kiroku<=K:
      flag=1
      break
  if flag==1:
    break
print(Beauty)
ans=2**100-1
for i in range(N*(N+1)//2):
  if Beauty[i]!="0"*length:
    ans=ans&int(Beauty[i],2)
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
fin = sys.stdin.readline
from functools import reduce

def range_sum(cuml_list, l, r):
    if l == 0:
        return cuml_list[r]
    else:
        return cuml_list[r] - cuml_list[l - 1]


N, K = [int(elem) for elem in fin().split()]
a_list = [int(elem) for elem in fin().split()]
cuml_list = []
current_sum = 0
for a in a_list:
    current_sum += a
    cuml_list.append(current_sum)

all_beautifulness = []
for l in range(N):
    for r in range(l, N):
        cuml_sum = range_sum(cuml_list, l, r)
        all_beautifulness.append(cuml_sum)

#all_beautifulness.sort()
max_beautifulness = 0
max_bit = len(bin(max(all_beautifulness))[:-2])
for bit in range(max_bit, 1, -1):
    base = 2**(bit + 1)
    for i, b in enumerate(all_beautifulness):
        if b >= base:
            all_beautifulness[i] = b - base
    all_beautifulness.sort()
    and_b = all_beautifulness[-K]
    for b in all_beautifulness[-K+1:]:
        and_b &= b
    max_beautifulness = max(max_beautifulness, and_b)
print(max_beautifulness)

Submission status: Wrong Answer

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()
ans = L[-K]
print(ans)

Submission status: Wrong Answer

Solution code:
from functools import reduce
N, K = list(map(lambda x : int(x) , input().split()))
a = list(map(lambda x : int(x) ,input().split()))

#N, K = list(map(lambda x : int(x) , "8 4".split()))
#a = list(map(lambda x : int(x) , "9 1 8 2 7 5 6 4".split()))

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

def conbination(beauty, r, list, cnt, answers):
	if cnt == r:
		print(list)
		answers.append( reduce(lambda x, y: x & y, list))
		return
	
	for i in range(0, len(beauty)):
		cnt += 1
		list.append(beauty[i])
		
		conbination(beauty[i+1:], r, list, cnt, answers)

		cnt -= 1
		list.pop()

answers = []
beauty = list(map(lambda x: sum(x), substring))

conbination(beauty, K, [], 0, answers)

print(max(answers))

print(N, K)

Submission status: Wrong Answer

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

accum = [0]*(n+1)
sacc = 0
for i in range(n):
    sacc += a[i]
    accum[i+1] = sacc
#print(accum)
beauty = []    
for i in range(1,n+1):
    for j in range(i,n+1):
        b = accum[j] - accum[i-1]
        
        beauty.append(~b)

beauty.sort()
#print(beauty)
ans = 0
for i in range(k):
    ans = ans | beauty[i]
    #print(ans)
ans = ~ ans
print(ans)

Submission status: Wrong Answer

Solution code:
import itertools

N,K = map(int, input().split())
nums = list(map(int, input().split()))
temp = []
for s, e in itertools.combinations(range(N+1), 2):
  num = sum(nums[s:e])
  temp.append(num)

temp = sorted(temp, reverse=True)
num = temp[:K]

a=10**9
for i in num:
  ans = a&i
  a = i

print(ans)

Submission status: Wrong Answer

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

ans = d[0]
for i in range(k):
    if i+1 == len(c):
        break
    ans = ans & c[i+1]
print(ans)

Submission status: Wrong Answer

Solution code:
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)
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
for i in range(start, -1, -1):
    onehot = 2 ** i
    tmp = sum(map(f, sumlist))
    if tmp >= K:
        and_result = list(map(f, sumlist))
        break
ans = -1
count = 0
for i in range(len(sumlist)):
    if ans == -1 and and_result[i]:
        ans = sumlist[i]
        count += 1
        continue
    elif ans == -1:
        continue
    ans = ans & sumlist[i]
    count += 1
    if count == K:
        break
print(ans)