Problem:


Score : 400 points

Problem Statement

One day, Niwango-kun, an employee of Dwango Co., Ltd., found an integer sequence (a_1, ..., a_N) of length N.
He is interested in properties of the sequence a.

For a nonempty contiguous subsequence a_l, ..., a_r (1 \leq l \leq r \leq N) of the sequence a, its beauty is defined as a_l + ... + a_r. Niwango-kun wants to know the maximum possible value of the bitwise AND of the beauties of K nonempty contiguous subsequences among all N(N+1)/2 nonempty contiguous subsequences. (Subsequences may share elements.)

Find the maximum possible value for him.

Constraints

2 \leq N \leq 1000

1 \leq a_i \leq 10^9

1 \leq K \leq N(N+1)/2

All numbers given in input are integers

Input

Input is given from Standard Input in the following format:

N K
a_1 a_2 ... a_N

Output

Print the answer.

Sample Input 1

4 2
2 5 2 5

Sample Output 1

12

There are 10 nonempty contiguous subsequences of a. Let us enumerate them:

contiguous subsequences starting from the first element: \{2\}, \{2, 5\}, \{2, 5, 2\}, \{2, 5, 2, 5\}

contiguous subsequences starting from the second element: \{5\}, \{5, 2\}, \{5, 2, 5\}

contiguous subsequences starting from the third element: \{2\}, \{2, 5\}

contiguous subsequences starting from the fourth element: \{5\}

(Note that even if the elements of subsequences are equal, subsequences that have different starting indices are considered to be different.)

The maximum possible bitwise AND of the beauties of two different contiguous subsequences is 12.
This can be achieved by choosing \{5, 2, 5\} (with beauty 12) and \{2, 5, 2, 5\} (with beauty 14).

Sample Input 2

8 4
9 1 8 2 7 5 6 4

Sample Output 2

32



Sample input:
4 2
2 5 2 5


Sample output:
12

Submission status: Wrong Answer

Solution code:
import math


def main():
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    if k == 1:
        print(sum(a))
        return
    cum = [0] * (len(a) + 1)
    for i in range(len(a)):
        cum[i+1] = cum[i] + a[i]
    b = [0] * (n * (n+1) // 2)
    index = 0
    for i in range(len(cum)):
        for j in range(i+1, len(cum)):
            b[index] = cum[j] - cum[i]
            index += 1

    ret = 0
    for i in range(45, 0, -1):
        b.sort(reverse=True, key=lambda x:x & 1 << i)
        tmp = search(b, i)
        if tmp + 1 < k:
            continue
        t = 1 << i
        ret += t
        b = b[:tmp+1]


    print(ret)


def search(array, i):
    le = 0
    ri = len(array)
    while ri - le > 1:
        mi = (ri + le) // 2
        if array[mi] & 1 << i:
            le = mi
        else:
            ri = mi
    return le


main()

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

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

maxlog = 0
maxe = max(sums)
while maxe >= 2**maxlog:
    maxlog += 1
maxlog -= 1

res = 0

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

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

print(res)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
a = list(map(int, input().split()))
selected = [0] * K
for i in range(N):
    a_sub = a[i:]
    if sum(a_sub[:len(a_sub)]) < min(selected):
        break
    for j in reversed(range(0, len(a_sub))):
        a_sub_sub = a_sub[:j + 1]
        a_sub_sub_sum = sum(a_sub_sub)
        if a_sub_sub_sum > min(selected):
            selected[selected.index(min(selected))] = a_sub_sub_sum
        else:
            break
res = selected[0]
for i in range(1, K):
    res = res & selected[i]
print(res)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import math


def main():
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    if k == 1:
        print(sum(a))
        return
    cum = [0] * (len(a) + 1)
    for i in range(len(a)):
        cum[i+1] = cum[i] + a[i]
    b = [0] * (n * (n+1) // 2)
    index = 0
    for i in range(len(cum)):
        for j in range(i+1, len(cum)):
            b[index] = cum[j] - cum[i]
            index += 1

    ret = 0
    for i in range(30, 0, -1):
        b.sort(reverse=True, key=lambda x:x & 1 << i)
        tmp = search(b, i)
        if tmp + 1 < k:
            continue
        t = 1 << i
        ret += t
        b = b[:tmp+1]


    print(ret)


def search(array, i):
    le = 0
    ri = len(array)
    while ri - le > 1:
        mi = (ri + le) // 2
        if array[mi] & 1 << i:
            le = mi
        else:
            ri = mi
    return le


main()

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(reverse=True)

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

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

        SUM=NLIST

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

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
for combi in iter.combinations(set(beauty_list), k):
    ans = combi[0]
    for element in combi:
        ans &= element
        if ans == 0:
            break
    if ans > max_and:
        max_and = ans

print(max_and)

Submission status: Wrong Answer

Solution code:
#      
import collections, atexit, math, sys, bisect 

sys.setrecursionlimit(1000000)
def getIntList():
    return list(map(int, input().split()))    

try :
    #raise ModuleNotFoundError
    import numpy
    def dprint(*args, **kwargs):
        #print(*args, **kwargs, file=sys.stderr)
        # in python 3.4 **kwargs is invalid???
        print(*args,  file=sys.stderr)
    dprint('debug mode')
except Exception:
    def dprint(*args, **kwargs):
        pass



inId = 0
outId = 0
if inId>0:
    dprint('use input', inId)
    sys.stdin = open('input'+ str(inId) + '.txt', 'r') #标准输出重定向至文件
if outId>0:
    dprint('use output', outId)
    sys.stdout = open('stdout'+ str(outId) + '.txt', 'w') #标准输出重定向至文件
    atexit.register(lambda :sys.stdout.close())     #idle 中不会执行 atexit


    
N, K = getIntList()
#print(N)
za  = getIntList()
zt = []

for i in range(N):
    tot = 0
    for j in range(N-i):
        tot += za[i+j]
        zt.append(tot)
zt.sort(reverse = True)

def find(z, bh):
    g = (1<<bh)
    zr = []
    for x in z:
        if g&x:
            zr.append(x)
    return zr

nowh = 30
result= 0
while nowh>=0:
    zr = find(zt,nowh)
    if len(zr)<K:
        nowh-=1
        continue
    result |= (1<<nowh)
    nowh-=1
    zt = zr

    
print(result)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
array = input()
array = array.replace(' ','')
substring = []
ans = 0

for l in range(n):
    for i in range(n-l):
        substring.append(array[i:i+l+1])

final_list = list(map(int,substring))
final_list.sort(reverse=True)

item = str(final_list[k-1])


for m in range(len(item)):
    ans += int(item[m:m+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]
a = [0]+a
s = []
for i in range(1,N+1):
    for j in range(i):
        s.append(a[i]-a[j])

ans = 0
for n in range(39,0,-1):
    n2 = 2**n
    s_ = [s[i]%n2 for i in range(len(s)) if s[i]&n2 == n2]
    if len(s_) >= K:
        ans += n2
        s = s_

print(ans)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
a = list(map(int,input().split()))
l = []
num = 0
"""
for i in range(len(a)):
    for j in range(len(a)):
        if i < j:
            l.append(sum(a[i:j+1])) 
else:
    l.sort()
    l.reverse()
    print(l[k-1])
"""
"""
co = 0
num = 0
for i in range(k-1):
    if a[0] > a[-1]:
        a.pop(-1)
    elif a[0] < a[-1]:
        num -= 1
        if num < 0:
            co += 1
            num = co
            a.pop(0)
    else:
        if sum(a[0:abs(k-1-i)]) > sum(a[::-1][0:abs(k-1-i)]):
            a.pop(-1)
        else:
            a.pop(0)
    print(a)
else:
    print(sum(a))
"""
co = 0
num = 0
ri = []
lef = []
for i in range(k-1):
    #for j in range(i):
    #    num -= 1
     #   if num < 0:
      #      co += 1
       #     num = co
        #    a.pop(0)
    num -= 1
    if num < 0:
        co += 1
        num = co
        lef = a[co:]
    ri = a[0:-1-i]
else:
    print(max(sum(lef),sum(ri)))

Submission status: Wrong Answer

Solution code:
# encoding: utf-8
N, K = map(int, input().split())
a = list(map(int, input().split()))

# create table
sumtab = [0] * (N + 1)
sumtmp = 0
for i, ai in enumerate(a):
    sumtmp += ai
    sumtab[i + 1] = sumtmp

# calc bit length
for blen in range(1, 33):
    if (sumtab[-1] >> blen) < 1: break
# print("#", blen)
    
# calc
ans = 0
for blen_tmp in list(range(blen))[::-1]:
    ans += (1 << blen_tmp)
    cnt = 0
    # print("#", blen_tmp, ans)
    for l in range(N):
        for r in list(range(N))[::-1]:
            blr = sumtab[r + 1] - sumtab[l] # beautifulness
            # print("##", l, r, blr)
            if blr < ans: break
            elif blr & ans < ans: continue
            else: cnt += 1
            # loop end
            if cnt >= K: break
        if cnt >= K: break
    if cnt < K: ans -= (1 << blen_tmp)
    else: continue

print(ans)

Submission status: Wrong Answer

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

l.sort()
l.reverse()


ans=[]
for i in range(len(l)-k+1):
    x=l[i]
    for j in range(k):
        x=x&l[i+j]
    ans.append(x)
 

print(max(ans))

Submission status: Wrong Answer

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

sum_list = []
b = []

for i in range(N):
    b = int_a[i:]
    for j in range(N):
        sum_list.append(sum(b[j:]))
        
sort_list = sorted(sum_list, reverse=True)
print(sort_list[K-1])

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("{:041b}".format(sum(As[i:j])))

    def find(i, alls):
        if i > 40:
            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:
# encoding: utf-8
N, K = map(int, input().split())
a = list(map(int, input().split()))

# create table
sumtab = [0] * (N + 1)
sumtmp = 0
for i, ai in enumerate(a):
    sumtmp += ai
    sumtab[i + 1] = sumtmp

# calc bit length
for blen in range(1, 33):
    if (sumtab[-1] >> blen) < 1: break
# print("#", blen)
    
# calc
th = 0
for blen_tmp in list(range(blen))[::-1]:
    th += (1 << blen_tmp)
    cnt = 0
    # print("#", blen_tmp, th)
    for l in range(N):
        for r in list(range(N))[::-1]:
            blr = sumtab[r + 1] - sumtab[l] # beautifulness
            # print("##", l, r, blr)
            if blr & th < th: break
            else: cnt += 1
            # loop end
            if cnt >= K: break
        if cnt >= K: break
    if cnt < K: th -= (1 << blen_tmp)
    else: continue

print(th)

Submission status: Wrong Answer

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

iter = 0
num = K
while num > 0:
    iter += 1
    num -= iter
sum_of_subarrays = []
for i in range(iter):
    for j in range(i+1):
        sum_of_subarrays.append(sum(a_s[j:N-i+j]))
sum_of_subarrays.sort()
sum_of_subarrays.reverse()
answer = sum_of_subarrays[0]
for i in range(K):
    answer &= sum_of_subarrays[i]
print(answer)

Submission status: Wrong Answer

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

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

ls=sorted(ls, reverse=True)
# print(ls)
lsans=[]
for i in range(len(ls)-K-1):
    tmp=ls[i]
    for j in range(i+1,i+K):
        tmp=tmp&ls[j]
    lsans.append(tmp)
# print(lsans)
print(max(lsans))

Submission status: Wrong Answer

Solution code:
# coding:utf-8

import sys
from collections import Counter


input = sys.stdin.readline
INF = float('inf')
MOD = 10 ** 9 + 7


def inpl(): return list(map(int, input().split()))


N, K = inpl()
A = inpl()

# li = []
li2 = []
for i in range(N):
    for j in range(i, N):
        tmp = sum(A[i:j + 1])
        # cnt = Counter(bin(tmp)[2:])['1']
        # print(cnt, A[i:j + 1])
        # li.append(bin(tmp)[2:])
        li2.append(tmp)

li2.sort()
li2.reverse()
# print(li2)
bit_max_length = len(bin(li2[0]))
ans = 0
for i in range(N * (N + 1) // 2 - K + 1):
    tmp = li2[i]
    if bit_max_length - len(bin(tmp)) > 2: break
    for j in range(i, i + K):
        tmp &= li2[j]
    ans = max(ans, tmp)

print(ans)
# print(li)
# c = Counter(li)
# print(c)

Submission status: Wrong Answer

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
    for j in range(len(blist)):
        #print(i,j)
        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])

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

Solution code:
import math


def main():
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    cum = [0] * (len(a) + 1)
    for i in range(len(a)):
        cum[i+1] = cum[i] + a[i]
    b = [0] * ((n+1) * (n+2) // 2)
    index = 0
    for i in range(len(cum)):
        for j in range(i, len(cum)):
            b[index] = cum[j] - cum[i]
            index += 1

    ret = 0
    for i in range(30, 1, -1):
        b.sort(reverse=True, key=lambda x:x & 1 << i)
        tmp = search(b, i)
        if tmp + 1 < k:
            continue
        t = 1 << i
        ret += t
        b = b[:tmp+1]
        for j in range(len(b)):
            b[j] = b[j] - (b[j] >> i << i)

    print(ret)


def search(array, i):
    le = 0
    ri = len(array)
    while ri - le > 1:
        mi = (ri + le) // 2
        if array[mi] & 1 << i:
            le = mi
        else:
            ri = mi
    return le


main()

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
array = input()
array = array.replace(' ','')
substring = []
ans = 0

for l in range(n):
    for i in range(n-l):
        substring.append(array[i:i+l+1])

final_list = list(map(int,substring))
final_list.sort(reverse=True)

item = str(final_list[k-1])


for m in range(len(item)):
    ans += int(item[m:m+1])


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):
    beauty = 0
    for j in range(i, N):
        beauty += A[j]
        B.append(beauty)
#print(B)
#bB = [format(b, '08b') for b in B]
#print(bB)
#bB.sort()
#print(bB)
maxb = 0
C = [[] for i in range(32)]
for b in range(31, -1, -1):
    for i in range(len(B)):
        if (1<<b) & B[i]:
            C[b].append(i)
    if not maxb and len(C[b])>=K:
        maxb = b
#print(maxb)
ans = set(C[maxb])
#print(ans)
for b in range(maxb, -1, -1):
    if len(C[b]) >= K:
        if len(ans.intersection(set(C[b]))) >= K:
            ans = ans.intersection(set(C[b]))
#print(ans)
res = B[ans.pop()]
for item in ans:
    res = res & B[item]
print(res)

Submission status: 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(n):
        if i+j>n-1:break
        l.append(sum(a[j:j+i+1]))
print(l[-k])

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)

ans = 0xFFFFFFFF
for x in b[0:K]:
  ans &= x
print(ans)

Submission status: Wrong Answer

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

array_int = [int(ele) for ele in input().split()]

new_array = []
for n in range(N):
	for i in range(n, len(array_int)):
		new_array.append( array_int[n:i+1] )


#print(new_array)

tmp_max = 0
for n in range(len(new_array) ):
	x = sum(new_array[n])
	if x < sum(new_array[n]):
		continue
	for i in range(n+2, len(new_array)):
		y = sum(new_array[i])
		tmp = x & y

		if tmp_max < tmp:
			tmp_max = tmp

print(tmp_max)

Submission status: Wrong Answer

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

p = K
num = 0
itr = 1

while p > 0:
    p = p - itr
    itr = itr +1
    num = num +1

sumlist = []
for i in range(num):
    sum = 0
    for j in range(len(a)-num+1):
        sum = sum + a[i+j]
    sumlist.append(sum)

print(max(sumlist))

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
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:
    break
  
ans=""
f=0
for i in range(length):
  for b in Beauty:
    if b=="0"*length:
      pass
    else:
      if b[i]=="0":
        ans+="0"
        f=1
        break
  if f==0:
    ans+="1"
  f=0
print(int(ans,2))

Submission status: Wrong Answer

Solution code:
import numpy as np
def main():
    n,k = map(int, input().split())
    a = list(map(int, input().split()))

    x = [sum(a[i:j+1]) for i in range(n) for j in range(i,n)]
    x.sort()
    x = x[::-1]
    ans = x[0]
    for i in range(k-1):
        ans = ans & x[i+1]
    print(ans)

if __name__ == "__main__":
    main()

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
for combi in iter.combinations(set(beauty_list), k):
    ans = combi[0]
    for element in combi:
        ans &= element
    if ans > max_and:
        max_and = ans

print(max_and)

Submission status: Wrong Answer

Solution code:
import bisect
import sys

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()
x=SUM[-K]

y=bisect.bisect_left(SUM,x)

COUNT=0
for s in SUM[y:]:
    if s & x==x:
        COUNT+=1

if COUNT>=K:
    print(x)
    sys.exit()


ANS=SUM[-1]

for i in range(-1,-K-1,-1):
    ANS=ANS& SUM[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)
# print(beauty)
target = [beauty[0]]
for i in range(1, len(beauty)):
    if beauty[i] != beauty[i - 1]:
        target.append(beauty[i])

digit = 0
targetDigit = target[-1]
while targetDigit > 0:
    digit += 1
    targetDigit = int(targetDigit / 2)
out = 0
while digit > 0:
    target = list(reversed(sorted(target)))
    # print(target)
    out = target[0]
    if target[k - 1] >> (digit - 1) == 1:
        for i in range(1, k):
            out &= target[i]
        print(out)
        exit()
    else:
        # print("aaaaaa")
        # print("digit:{}".format(digit))
        for i in range(k):
            # print("target[{}]:{}vs{}".format(i, target[i], (1 << (digit - 1)) - 1))
            target[i] &= (1 << (digit - 1)) - 1
            # print("->target[{}]:{}".format(i, target[i]))
    digit -= 1
print(0)

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("{:021b}".format(sum(As[i:j])))

    def find(i, alls):
        if i > 20:
            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)
    a = 0xFFFFFFFF
    for p in ans:
        a &= int(p, 2)
    return a

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())
array = input()
array = array.replace(' ','')
substring = []
ans = 0

for k in range(n):
    for i in range(n-k):
        substring.append(array[i:i+k+1])

final_list = list(map(int,substring))
final_list.sort(reverse=True)

item = str(final_list[k-1])


for i in range(len(item)):
    ans += int(item[i:i+1])


print(ans)

Submission status: Wrong Answer

Solution code:
import math
from functools import reduce
import collections
import itertools
s = list(map(int,input().split()))
n = s[0]
k = s[1]
a = list(map(int,input().split()))
len_ = n-k+1
ans = 0
for i in range(k):
    if i == 0:
        if ans < sum(a[i:i+len_]):
            ans = sum(a[i:i+len_])
    elif i == k-1:
        if ans < sum(a[i:i+len_]):
            ans = sum(a[i:i+len_])
            
            
    else:
        if k<=3:
            if ans < sum(a[i:i+len_+1]):
                ans = sum(a[i:i+len_+1])
        else:
            if ans < sum(a[i:i+(n-math.ceil(k/2))]):
                ans = sum(a[i:i+(n-math.ceil(k/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()
b=b[::-1]
s=b[:K]
if K==1:
    tmp=s[0]
else:
    tmp=s[0]&s[1]
    for i in range(2,K):
        tmp=tmp&s[i]

print(tmp)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
A = list(map(int, input().split()))
list = []
for i in range(N):
    for j in range(i+1, N+1):
        list.append(sum(A[i:j]))

list = sorted(list, reverse=True)
b = list[0]
for i in range(1, K):
    b = b & list[i]
print(b)

Submission status: Wrong Answer

Solution code:
from sys import stdin
def main():
    N, K = map(int, stdin.readline().rstrip().split())
    A = list(map(int, stdin.readline().rstrip().split()))

    edge = 0
    if K % 2 == 0:
        edge = K // 2
    else:
        edge = K // 2 + 1

    while( edge < K):
        leftA = A[:len(A)-K+edge]
        rightA = A[K-edge:]

        if sum(leftA) == sum(rightA):
            break
        edge += 1

    leftA = A[:len(A)-K+edge]
    rightA = A[K-edge:]

    print(max(sum(leftA), sum(rightA)))

input = lambda: stdin.readline()
main()

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
a = list(map(int, input().split()))
s = []
for i in range(len(a)-1):
    sum_v = a[i]
    for j in range(i+1, len(a)):
        sum_v += a[j]
        s.append(sum_v)
for k in range(K):
    if k == 0:
        ans = s.pop(s.index(max(s)))
    else:
        ans = ans & s.pop(s.index(max(s)))
print(ans)

Submission status: Wrong Answer

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

iter = 0
while K > 0:
    iter += 1
    K -= iter
sum_of_subarrays = []
for i in range(iter):
    for j in range(i+1):
        sum_of_subarrays.append(sum(a_s[j:N-i+j]))
sum_of_subarrays.sort()
sum_of_subarrays.reverse()


print(sum_of_subarrays[K])

Submission status: Wrong Answer

Solution code:
import sys

N, K = map(int, sys.stdin.readline().strip().split(" "))
A = map(int, sys.stdin.readline().strip().split(" "))

l = []

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

l.sort(reverse=True)

print(l[K-1])

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]))

Submission status: Wrong Answer

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

l.sort()
l.reverse()


ans=[]
for i in range(len(l)-k):
    x=l[i]
    for j in range(k):
        x=x&l[i+j]
    ans.append(x)
 

print(max(ans))

Submission status: Wrong Answer

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

c,d,e = 0,0,0

for i in range(n):
    l = 0
    for j in range(n-i):
            l += a[i+j]
    if l > c:
        c,d,e = l,c,d
    elif c >= l > d:
        c,d,e = c,l,d
    elif d >= l >e:
        c,d,e = c,d,l
print(max(c&d,d&e,c&e))

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
a = list(map(int, input().split()))
s = []
for i in range(len(a)-1):
    sum_v = a[i]
    s.append(sum_v)
    for j in range(i+1, len(a)):
        sum_v += a[j]
        s.append(sum_v)
for k in range(K):
    if k == 0:
        ans = s.pop(s.index(max(s)))
    else:
        ans = ans & s.pop(s.index(max(s)))
print(ans)

Submission status: Wrong Answer

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

l_A = len(A)
nums = []
for i in range(l_A):
    sum = 0
    for j in range(i, l_A):
        sum += A[j]
        nums.append(sum)
nums.sort(reverse=True)
max_num = 0
for num in nums:
    if num > max_num:
        max_num = num

b = bin(max_num)
b_max_nums = (list(b))[2:]
l_b_max_nums = len(b_max_nums)

b_nums = []
for num in nums:
    n = []
    for i in range(l_b_max_nums):
        n.append('0')
    _b = bin(num)
    _b_nums = (list(_b))[2:]
    l_b_nums = len(_b_nums)
    for i in range(l_b_nums):
        n[len(n)-1-i] = _b_nums[l_b_nums - 1 - i]
    b_nums.append(n)

target_group = b_nums
for i in range(l_b_max_nums):
    l_target = len(target_group)
    _target_group = []
    for j in range(l_target):
        if b_nums[j][i] == '1':
            _target_group.append(b_nums[j])
    if len(_target_group) >= K:
        target_group = _target_group

ans = int("0b" + ''.join(target_group[0]), 2)
l_target_group = len(target_group)
for i in range(1, l_target_group):
    ans = ans & int("0b" + ''.join(target_group[i]), 2)
print(ans)

Submission status: Wrong Answer

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

p = K
num = 0
while p != 0:
    p = p//2
    num = num +1

sumlist = []
for i in range(num):
    sum = 0
    for j in range(len(a)-num+1):
        sum = sum + a[i+j]
    sumlist.append(sum)

print(max(sumlist))

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(reverse=True)

ANS=SUM[0]

for i in range(K):
    ANS=ANS& SUM[i]

print(ANS)

Submission status: Wrong Answer

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

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


max = 0

for right in range(K-1):
    left = int(K // (right+1)) - 1
    if right == 0:
        max = sum(a[left:])
    if max < sum(a[left:-right]):
        max = sum(a[left:-right])

print(max)

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))
btyarr.sort(reverse=True)

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

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

print(ans)

Submission status: Wrong Answer

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

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

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

l.sort(reverse=True)
for i in range(n*(n+1)//2):
  if l[i]&l[i+1]:break
print(reduce(lambda x,y:x&y,l[i:i+k]))