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:
#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*2:
    #if i==n:
        break
    i+=1

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

Submission status: Wrong Answer

Solution code:
from math import log2
n, k = map(int, input().split())
a = list(map(int, input().split()))
m = sum(a)
order = int(log2(m))
box = [[] for i in range(order+1)]
b = [[a[i]] for i in range(n)]
c = set(a)
for i in range(n-1):
    for j in range(i+1, n):
        b[i].append(a[j]+b[i][-1])
        if b[i][-1] not in c:
            c.add(b[i][-1])
            d = int(log2(b[i][-1]))
            box[d].append(b[i][-1])

l = []
for i in range(order, -1, -1):
    if len(box[i]) >= k:
        l.append(box[i])

q = 0
for j in range(len(l)):
    ans = sorted(l[j])[-1::-1]
    p = ans[0]
    for i in range(k):
        p &= ans[i]
    q = max(p, q)

print(q)

Submission status: Wrong Answer

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

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)

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

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat Nov 24 20:12:29 2018

@author: maezawa
"""
import numpy as np
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)
l=0
ndim = n*(n+1)//2
beauty = np.array([0]*ndim,dtype='int64')   
for i in range(1,n+1):
    for j in range(i,n+1):
        b = accum[j] - accum[i-1]        
        beauty[l] = ~b
        l += 1

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:
n,k=map(int,input().split())
*a,=map(int,input().split())
l=[]
for i in range(n):
    t=0
    v=[]
    for j in range(i,n):
        t+=a[j]
        v.append(t)
    l.append(v)
i=0
v=[]
while 1:
    if l[i]:v.append(l[i].pop())
    if len(v)==k:break
    if not l[i]or l[(i+1)%len(l)]and l[i][-1]<l[(i+1)%len(l)][-1]:i=(i+1)%len(l)
m=v[0]
for t in v[1:]:
    m&=t
print(m)

Submission status: Wrong Answer

Solution code:
def calc_and(X, k):
    res = 999999999999
    if len(X) == 0:
        return 0
    if k == 1:
        return X[-1]
    for i in range(len(X)-1, len(X)-1-k, -1):
        skip = 0
        if not (X[i] & (X[i]-1)):
            skip = calc_and(X[0:i+1], k-1)
        noskip = res & X[i]
        res = max(skip, noskip)
    return res


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

s = []

for i in range(N):
    for j in range(i, N):
        if i == 0:
            s.append(B[j])
        else:
            s.append(B[j] - B[i-1])
l = sorted(s)
if K == 1:
    print(l[-1])
else:
    maxv = calc_and(l, K)
    print(maxv)

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 = 0
for i in range(1,len(sort_lst)-K):
  tmp = sort_lst[i-1]
  for j in range(i,i+K-1):
    tmp = tmp&sort_lst[j]
  if ans < tmp:
    ans = tmp
  #print(tmp)
#print(sort_lst)
print(ans)
#print(len(sort_lst))

Submission status: Wrong Answer

Solution code:
def getmaxand(bs, K):
    for k in range(29, -1, -1):
        bit = 1 << k
        avails = [v for v in bs if v & bit]
        if len(avails) < K:
            continue
        filt = ~(-1 << k)
        elims = [v & filt for v in avails]
        return (1 << k) + getmaxand(elims, K)
    return 0


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

bs = []
for l in range(N):
    for r in range(l, N):
        bs.append(sum(A[l:r+1]))

print(getmaxand(bs, K))

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
 
import sys
 
input = raw_input().split(" ")
N = int(input[0])
K = long(input[1])
 
aVec = raw_input().split(" ")
a = []
 
for i in aVec:
    a.append(long(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:
# -*- 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))]

Submission status: Wrong Answer

Solution code:
import sys
import collections
import itertools
import math
import re
from collections import Counter
from collections import deque
from collections import defaultdict

MOD = 10 ** 9 + 7

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


B = []
C = []

s = 0
for i in range(0,N-1):
    for j in range(i, N):
        if j==i:
            C.append(A[i])
        else:
            C.append(C[-1] + A[j])

Q = len(C)
use = [False] *Q
ret = 0

C = sorted(C,reverse=True)

def filterList(list, i):
    retList = []
    cnt = 0
    for l in list:
        if l & (1 << i):
            cnt += 1
            retList.append(l)

    if cnt >= K:
        return retList
    else:
        return list


# 31..0
D = C
for i in range(31,-1,-1):
    D = filterList(D,i)

EE = list(itertools.combinations(D, K))

ret = 0
for E in EE:
    r = E[0]
    for e in E:
        r = r & e
    ret = max(ret, r)


print(ret)

sys.exit(0)

Submission status: Wrong Answer

Solution code:
# coding:utf-8

from itertools import combinations

def max_b(b_list):
    t = 0
    for i in range(len(b_list)):
        if i == 0:
            t = b_list[i]
        else:
            t = t & b_list[i]
    return t

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

bn = []
for i in range(n):
    for y in range(n):
        bn.append(sum(an[i:y+1]))


max_b_num = 0
for p in combinations(bn, 3):
    tmp = max_b(p)
    if tmp >= max_b_num:
        max_b_num = tmp
    
print(max_b_num)

Submission status: Wrong Answer

Solution code:
import math

def gcd(a,b):
    """Compute the greatest common divisor of a and b"""
    while b > 0:
        a, b = b, a % b
    return a
 
def lcm(a, b):
    """Compute the lowest common multiple of a and b"""
    return a * b / gcd(a, b)
 

def f(b,n):
	if n < b:
		return n
	return f(b, n//b) + n%b
 

N, K = map(int,input().split())
total_sum = 0
v = list(map(int,input().split()))
bubunwa = [0]

for i in range(len(v)):
	total_sum += v[i]
	bubunwa.append(total_sum)

sums = []
for i in range(len(bubunwa)):
	for j in range(i, len(bubunwa)):
		sums.append(bubunwa[j]-bubunwa[i])

sums.sort(reverse=True)

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

print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
a = list(map(int, input().split()))
beauty = []
max = -1
for i in range(N):
    sum = 0
    for j in range(i, N):
        sum += a[j]
    beauty.append(sum)
print(beauty[0] & beauty[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
l = len(seq)
s = set()
for i in range(50,-1,-1):
    cnt = 0
    for j in range(l):
        if seq[j] >= 0 and (seq[j] & (1<<i)) != 0:
            cnt += 1
            s.add(j)

    if cnt >= K:
        ans += (1 << i)
        for j in range(l):
            if j in s:
                seq[j] = seq[j] - (1 << i)
            else:
                seq[j] = -1


print(ans)

Submission status: Wrong Answer

Solution code:
import math
import itertools

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

acc = [0] + list(itertools.accumulate(a))

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

lst.sort(reverse=True)

ma = lst[0]
d = int(math.log2(ma))

count = 0
result = lst[0]
for i in range(N):
    if lst[count] >> d == 1:
        result = result & lst[count]
        count += 1
        if count == K:
            print(result)
            exit(0)
    else:
        lst = lst[count:]
        count = 0
        d -= 1
        result = lst[0]

print(result)

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)
print(sorted(l)[-k])

Submission status: Wrong Answer

Solution code:
n, k =  map(int, input().split())
a = list(map(int, input().split()))
import math
m = math.floor(n - (-3 + (1 + 8 * k) ** 0.5 ) / 2) 
x = 0
for i in range(m):
  y = sum(a[i:(i+m)])
  x = max(x,y)
print(x)

Submission status: Wrong Answer

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

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

B = []
for i in range(N):
    SUM = 0
    for j in range(i,N):
        SUM += A[j]
        B.append(SUM)
        
B = list(sorted(B,reverse=True))

MAX = B[0]
two = 1
POW = 1
while(MAX>=two*2):
    two*=2
    POW+=1
#print(two,POW,B)

#MAXBITソート
for p in range(POW)[::-1]:
    C = list(filter(lambda x:x>=two,B))
    if len(C)>=K:
        break
    else:
        two//=2
        
POW = p+1

for p in range(POW):
    D = list(filter(lambda x:(x//two)%2==1,C))
    #print(D)
    LEN = len(D)
    if LEN>=K:
        C = D[:]
        if LEN==K:break
        two//=2

ans = C[0]
for c in C[1:K]:
    ans &= c
    
print(ans)

Submission status: Wrong Answer

Solution code:
from math import log2
n, k = map(int, input().split())
a = list(map(int, input().split()))
m = sum(a)
order = int(log2(m))
box = [[] for i in range(order+1)]
b = [[a[i]] for i in range(n)]
c = list(a)
for i in range(n-1):
    for j in range(i+1, n):
        b[i].append(a[j]+b[i][-1])
        c.append(b[i][-1])

ans = sorted(list(c))[-1::-1][0:k]

p = ans[0]
for i in range(k):
    p &= ans[i]
print(p)

Submission status: Wrong Answer

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

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

B = []
for i in range(N):
    SUM = 0
    for j in range(i,N):
        SUM += A[j]
        B.append(SUM)
        
B = list(sorted(B,reverse=True))

MAX = B[0]
two = 1
POW = 1
while(MAX>=two*2):
    two*=2
    POW+=1
#print(two,POW,B)

#MAXBITソート
for p in range(POW)[::-1]:
    C = list(filter(lambda x:x>=two,B))
    if len(C)>=K:
        break
    else:
        two//=2
        
POW = p+1

for p in range(POW):
    D = list(filter(lambda x:(x//two)%2==1,C))
    #print(D)
    LEN = len(D)
    if LEN>=K:
        C = D[:]
        if LEN==K:break
        two//=2

ans = C[0]
for c in C[1:]:
    ans &= c
    
print(ans)

Submission status: Wrong Answer

Solution code:
import math
import itertools

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

acc = [0] + list(itertools.accumulate(a))

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

lst.sort(reverse=True)

result = lst[0]
for i in range(K):
    result = result & lst[i]

print(result)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
from functools import reduce


def calc(a,b):
    return a&b

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
ans = reduce(calc,list(map(int,sorted_b[-i+1:])))
print(ans)

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
    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=copy.deepcopy(clist)

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

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

print(int(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("{:061b}".format(sum(As[i:j])))
    print("alls", len(alls))
    def find(i, alls):
        if i > 60:
            return []
        ones = [a for a in alls if a[i] == "1"]
        if len(ones) < K:
            return find(i + 1, alls)
        elif len(ones) == K:
            return ones
        elif len(ones) > K:
            ret = find(i + 1, ones)
            if len(ret) == 0:
                return ones[0:K]
            else:
                return ret

    ans = find(0, alls)
    if len(ans):
        a = 0xFFFFFFFFFFF
        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(" ")))

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(50,-1,-1):
    cnt = 0
    for j in range(l):
        if seq[j] >= 0 and seq[j] & (1<<i) != 0:
            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:
import itertools

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

count = 1

sum_list = []

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

sum_list.sort()
sum_list.reverse()

max_val = 0
for i in range(len(sum_list)-1-K):
    res = sum_list[i]
    for i in sum_list[i+1:i+K]:
        res = res & i
    if res > max_val:
        max_val = res

print(max_val)

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]

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

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

for i in range(K):
    if i == 0:
        R = int('0b' + ''.join(L[i]), 2)
    else:
        R = R & int('0b' + ''.join(L[i]), 2)

print(R)

Submission status: Wrong Answer

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

S=[0]

for x in A:
	if len(S)==0:
		S.append(x)
	else:
		S.append(S[-1]+x)


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

L.sort(reverse=True)

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

Submission status: Wrong Answer

Solution code:
# coding:utf-8

from itertools import chain,combinations, filterfalse

def powerset(iterable):
    s = list(iterable)
    return combinations(s, 3)

def max_b(b_list):
    t = 0
    for i in range(len(b_list)):
        if i == 0:
            t = b_list[i]
        else:
            t = t & b_list[i]
    return t

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

bn = []
for i in range(n):
    for y in range(n):
        bn.append(sum(an[i:y+1]))

hoge = powerset(bn)

max_b_num = 0
for p in hoge:
    tmp = max_b(p)
    if tmp >= max_b_num:
        max_b_num = tmp
    
print(max_b_num)

Submission status: Wrong Answer

Solution code:
def calc_and(X, k):
    res = 999999999999
    if len(X) == 0:
        return 0
    if k == 1:
        return X[-1]
    for i in range(len(X)-1, len(X)-1-k, -1):
        skip = 0
        if not (X[i] & (X[i]-1)):
            skip = calc_and(X[0:i], k-1)
        noskip = res & X[i]
        res = max(skip, noskip)
    return res


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

s = []

for i in range(N):
    for j in range(i, N):
        if i == 0:
            s.append(B[j])
        else:
            s.append(B[j] - B[i-1])
l = sorted(s)
if K == 1:
    print(l[-1])
else:
    maxv = calc_and(l, K)
    print(maxv)

Submission status: Wrong Answer

Solution code:
from functools import reduce


def calc(a,b):
    return a&b

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
ans = reduce(calc,sorted_b[-i+1:])
print(int(ans))

Submission status: Wrong Answer

Solution code:
import sys
import collections
import itertools
import math
import re
from collections import Counter
from collections import deque
from collections import defaultdict

MOD = 10 ** 9 + 7

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


B = []
C = []

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

Q = len(C)
use = [False] *Q
ret = 0

C = sorted(C,reverse=True)

def filterList(list, i):
    retList = []
    cnt = 0
    for l in list:
        if l & (1 << i):
            cnt += 1
            retList.append(l)

    if cnt >= K:
        return retList
    else:
        return list


# 31..0
D = C
for i in range(31,-1,-1):
    D = filterList(D,i)

EE = list(itertools.combinations(D, K))

ret = 0
for E in EE:
    r = E[0]
    for e in E:
        r = r & e
    ret = max(ret, r)


print(ret)

sys.exit(0)

Submission status: Wrong Answer

Solution code:
from math import log2
n, k = map(int, input().split())
a = list(map(int, input().split()))
m = sum(a)
order = int(log2(m))
box = [[] for i in range(order+1)]
b = [[a[i]] for i in range(n)]
c = set(a)
for i in range(n-1):
    for j in range(i+1, n):
        b[i].append(a[j]+b[i][-1])
        if b[i][-1] not in c:
            c.add(b[i][-1])
            d = max(int(log2(b[i][-1])), 0)
            box[d].append(b[i][-1])

for i in range(order, -1, -1):
    if len(box[i]) >= k:
        idx = i
        break

ans = sorted(box[idx])[-1::-1]
p = ans[0]
for i in range(k):
    p &= ans[i]
print(p)

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()
L.reverse()
L_ch = L[:K]
for e in L_ch:
    if e == L_ch[0]:
        copy = e
        continue
    AND = copy & e 
    if e == L_ch[-1]:
        print(AND)
        exit()
    copy = AND

Submission status: Wrong Answer

Solution code:
import itertools

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

count = 1

sum_list = []

max_sum = 0
for i in range(len(A)):
    for j in range(1,len(A[i:])+1):
        #print(bin(sum(A[i:i+j])),sum(A[i:i+j]))
        sum_list.append(sum(A[i:i+j]))

sum_list.sort()
sum_list.reverse()

res = sum_list[0]
for i in sum_list[1:K]:
    res = res & i

print(res)

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):
    if len(str(bin(ls[i])))!=len(str(bin(ls[i+K]))):
        continue
    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:
from functools import reduce


def calc(a,b):
    return a&b

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
ans = reduce(calc,sorted_b[-i+1:])
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)
    if num == k:
        break

print(res)

Submission status: Wrong Answer

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

n = 1
k = 0
for i in range(1, N):
    n += i
    if n >= K:
        k = i
        break

ans = 0
for i in range(k+1):
    ans = max(ans, sum(a[i:i+(N-k)]))
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import collections
import itertools
import math
import re
from collections import Counter
from collections import deque
from collections import defaultdict

MOD = 10 ** 9 + 7

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


B = []
C = []

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

Q = len(C)
use = [False] *Q
ret = 0

C = sorted(C,reverse=True)

# 31..0
first = True
for j in range(31,-1,-1):
    if first == False and use.count(True) <= K-1:
        break

    # k個以上たってるか？
    target = 1 << j
    cnt = 0
    for i in range(Q):
        if (C[i] & target):
            if first == True:
                cnt += 1
            else:
                if use[i]:
                    cnt += 1

    if cnt >= K:
        tmpRet = (1 << 32) - 1
        if first == True:
            for i in range(Q):
                use[i] = (C[i] & target != 0)
            first = False
        else:
            for i in range(Q):
                use[i] = use[i] and (C[i] & target != 0)

        for i in range(Q):
            if use[i]:
                tmpRet = tmpRet & C[i]
        ret = tmpRet


D = []
for i in range(Q):
    if use[i]:
        D.append(C[i])

EE = list(itertools.combinations(D, K))

ret = 0
for E in EE:
    r = E[0]
    for e in E:
        r = r & e
    ret = max(ret, r)


print(ret)

sys.exit(0)

Submission status: Wrong Answer

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

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)

assert len(all_beautifulness) == N*(N+1) // 2
max_beautifulness = 0
max_bit = len(bin(max(all_beautifulness))) - 2
for bit in range(max_bit, 0, -1):
    base = 2**bit
    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=[]
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:
    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)

print(max)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**6)
def main():
	s = [0 for _ in range(40)]
	n, k = map(int, input().split())
	a = [0] + [int(i) for i in input().split()]
	for i in range(1,n+1):
		a[i] = a[i-1] + a[i]
	lst = []
	for i in range(0,n):
		for j in range(i+1, n+1):
			lst += [a[j]-a[i]]
	lst.sort()
	ans = lst[-1]
	for i in range(k-1):
		ans = ans & lst[-2-i]
	print(ans)
main()

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
lis = list(map(int,input().split()))
li = []
num = 0
for i in range(n):
  for j in range(i,n):
    num += lis[j]
    li.append(num)
  num = 0
li.sort(reverse = True)
ans = li[0]
for i in range(k):
  ans = ans&li[i]
print(ans)
  # print(li)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
A = [int(i) for i in input().split()]
tmp = []
ch = 0
for i in range(1, 1001):
    if int(0.5*i*(i+1)) >= K:
        ch = i
        break

for i in range(ch):
    tmp.append(sum(A[i:N-ch+i+1]))

tmp.sort(reverse = True)

num = K - int(0.5*(ch-1)*ch) -1
print(sum(A) & tmp[num])

Submission status: Wrong Answer

Solution code:
a = input().split()
a = [int(i) for i in a]
b = input().split()
b = [int(i) for i in 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 = sorted(superlist[i])[::-1]
		temp = li[0]
		for j in li[:a[1]]:
			temp &= j
		print(temp)
		break