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:
from sys import stdin
n,k=[int(x) for x in stdin.readline().rstrip().split()]
data=[int(x) for x in stdin.readline().rstrip().split()]

tmp=[]
cnt=0
for i in range(len(data)):
    for j in range(len(data)-i):
        tmp.append(((sum(data[i:1+i+j]))))
        cnt+=1
beat_v=sorted(tmp)
tmp2 = list(map(lambda x: len(bin(x)), beat_v))

#同じ桁のbit列がK個以上あるか探索
cnt2=0
maxbit=tmp2[-1]
res=0
for l in reversed(tmp2):
    if maxbit==l:
        cnt2+=1
    maxbit=l
    if cnt2==k:
        res=maxbit
        break
cnt3=0
for m in reversed(tmp2):
    if m != res:
        cnt3+=1
    else :
        break
        
res_=list(reversed(beat_v))[cnt3:cnt3+k]
result=res_[0]
for g in res_:
    result=result&g
print(result)

Submission status: Wrong Answer

Solution code:
import itertools
n,k = map(int,input().split())
a = list(map(int,input().split()))
b = [a[0]]
for i in range(n-1):
    b.append(b[i]+a[i+1])
c = list(itertools.combinations(range(n),2))
s = a
for i in c:
    s.append(b[i[1]]-b[i[0]]+a[i[0]])
s.sort(reverse=True)
t = s[0]
for i in range(k-1):
    t = t&s[i+1]
print(t)

Submission status: Wrong Answer

Solution code:
from sys import stdin
n,k=[int(x) for x in stdin.readline().rstrip().split()]
data=[int(x) for x in stdin.readline().rstrip().split()]

tmp=[0]*(n**2)
cnt=0
for i in range(len(data)):
    for j in range(len(data)-i):
        tmp[cnt]=((sum(data[i:1+i+j])))
        cnt+=1
beat_v=sorted(tmp)
tmp2 = list(map(lambda x: len(bin(x)), beat_v))

#同じ桁のbit列がK個以上あるか探索
cnt2=0
maxbit=tmp2[-1]
res=0
for l in reversed(tmp2):
    if maxbit==l:
        cnt2+=1
    maxbit=l
    if cnt2>k:
        res=maxbit
        break
cnt3=0
for m in reversed(tmp2):
    if m != res:
        cnt3+=1
    else :
        break
        
res_=list(reversed(beat_v))[cnt3:cnt3+k]
result=res_[0]
for g in res_:
    result=result&g
print(result)

Submission status: Wrong Answer

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

ds = []

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

o = []
ds.sort(reverse=True)

sum = 0
for i in range(32,-1,-1):
    count = 0
    o.clear()

    for dss in ds:
        if dss & (1 << i) > 0 :
            count += 1
            o.append(dss)

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

print(sum)

Submission status: Wrong Answer

Solution code:
line = str(input())
lines = line.split(' ')

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

d = []

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

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

ds = []

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

o = []
ds.sort(reverse=True)

sum = 0
for i in range(32,-1,-1):
    count = 0
    o.clear()

    for dss in ds:
        if dss & (1 << i) > 0 :
            count += 1
            o.append(dss)

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

print(sum)

Submission status: Wrong Answer

Solution code:
line = str(input())
lines = line.split(' ')

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

d = list()

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

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

ds = list()

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

o = list()
ds.sort(reverse=True)

sum = 0
for i in range(32,-1,-1):
    count = 0
    o.clear()

    for dss in ds:
        if dss & (1 << i) > 0 :
            count += 1
            o.append(dss)

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

    if count == k :
        break

print(sum)

Submission status: Wrong Answer

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

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

A = []

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

A.sort()
A.reverse()

for i in range(K-1):
        A[i+1] = A[i] & A[i+1]

print(A[K-1])

Submission status: Wrong Answer

Solution code:
line = str(input())
lines = line.split(' ')

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

d = list()

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

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

ds = list()

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

o = list()
ds.sort(reverse=True)

for i in range(32,-1,-1):
    count = 0
    o.clear()

    for dss in ds:
        if dss & (1 << i) > 0 :
            count += 1
            o.append(dss)

    if count >= k :
        ds = list(o)

    if count == k :
        break

sum = ds[0]
for i in range(1,k):
    sum = sum & ds[i]

print(sum)

Submission status: Wrong Answer

Solution code:
line = str(input())
lines = line.split(' ')

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

d = list()

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

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

ds = list()

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

o = list()

for i in range(32,-1,-1):
    count = 0
    o.clear()

    for dss in ds:
        if dss & (1 << i) > 0 :
            count += 1
            o.append(dss)

    if count >= k :
        ds = list(o)
        
    if count == k :
        break

sum = ds[0]
for i in range(1,k):
    sum = sum & ds[i]

print(sum)

Submission status: Wrong Answer

Solution code:
import itertools

N, K = map(int, input().split())
a = list(map(int, input().split()))
beautiful_num = []
bit_mask = []

for i in range(N):
    for j in range(N):
        if i <= j:
            temp = [0] * N
            for k in range(i, j+1):
                temp[k] = 1
            bit_mask.append(temp)

for bit in bit_mask:
    temp = 0
    for b, num in zip(bit, a):
        if b == 1:
            temp += num
    beautiful_num.append(temp)

ma_b_cnt = 0
target_set = None

for i in range(40, -1, -1):
    if target_set is None:
        for b in beautiful_num:
            if b & (1 << i) != 0:
                ma_b_cnt += 1
        if ma_b_cnt < K:
            ma_b_cnt = 0
        else:
            target_set = set([b for b in beautiful_num if b & (1 << i) != 0])
    else:
        count = 0
        for b in target_set:
            if b & (1 << i) == 0:
                count += 1
        if len(target_set) - count >= K:
            for b in list(target_set):
                if b & (1 << i) == 0:
                    target_set.remove(b)
        else:
            break

result = 0
for comb in itertools.combinations(target_set, K):
    temp = comb[0]
    for b in comb[1:]:
        temp = b & b
    if temp > result:
        result = temp

print(result)

Submission status: Wrong Answer

Solution code:
import itertools

N, K = 4,2#map(int, input().split())
a = [2,5,2,5]#list(map(int, input().split()))
beautiful_num = []
bit_mask = []

for i in range(N):
    for j in range(N):
        if i <= j:
            temp = [0] * N
            for k in range(i, j+1):
                temp[k] = 1
            bit_mask.append(temp)

for bit in bit_mask:
    temp = 0
    for b, num in zip(bit, a):
        if b == 1:
            temp += num
    beautiful_num.append(temp)

ma_b_cnt = 0
target_set = None

for i in range(40, -1, -1):
    if target_set is None:
        for b in beautiful_num:
            if b & (1 << i) != 0:
                ma_b_cnt += 1
        if ma_b_cnt < K:
            ma_b_cnt = 0
        else:
            target_set = set([b for b in beautiful_num if b & (1 << i) != 0])
    else:
        count = 0
        for b in target_set:
            if b & (1 << i) == 0:
                count += 1
        if len(target_set) - count >= K:
            for b in list(target_set):
                if b & (1 << i) == 0:
                    target_set.remove(b)
        else:
            break

result = 0
for comb in itertools.combinations(target_set, K):
    temp = comb[0]
    for b in comb[1:]:
        temp = b & b
    if temp > result:
        result = temp

print(result)

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]

def cao(aller, i):
	dicter = dict()
	for m in aller:
		for n in m:
			if n < i:
				try:
					dicter[n].append(m)
				except:
					dicter[n] = [m]
	for i in sorted(dicter)[::-1]:
		if len(dicter[i]) >= a[1]:
			return dicter[i], i
		else:
			return [], -1
	return [], -1
	
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(x)
			except:
				superlist[m] = [x]
						
for i in sorted(superlist)[::-1]:
	if len(superlist[i]) >= a[1]:
		li = superlist[i]
		sumer = 2 ** i
		gan1, gan2 = cao(li, i)
		while gan2 != -1:
			sumer += 2**gan2
			gan1,gan2 = cao(gan1,gan2)
		print(sumer)
		break

Submission status: Wrong Answer

Solution code:
import itertools

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

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

d = list()

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

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

ds = list()

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

digit = 0
o = list()

for i in range(31,-1,-1):
    count = 0
    o.clear()
    for dss in ds:
        if dss & (1 << i) > 0 :
            count += 1
            o.append(dss)
    if count >= k :
        ds = o.copy()

sum = ds[0]
for i in range(1,k):
    sum = sum & ds[i]

print(sum)

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)
l=0
ndim = n*(n+1)//2
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(reverse=True)
#print(beauty)
ans = 0
kgroup = beauty[:]
nextkgroup = []
bt = 40
while True:
    cnt = 0
    btn = 1 << bt
    for i in kgroup:
        if i & btn != 0:
            nextkgroup.append(i)
            cnt += 1
    if cnt >= k:
        ans = ans | btn
        kgroup = nextkgroup[:]
        nextkgroup = []
    bt -= 1
    if bt < 0:
        break
    
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = [int(_) for _ in input().split()]
A = [int(_) for _ in input().split()]
B = []
for i in range(N):
    B += [A[i]]
    for j in range(i+1,N):
        B += [B[-1] + A[j]]

ans = 0
for i in range(40, -1, -1):
    B_new = []
    for b in B:
        if b & 1 << i:
            B_new += [b]
    if len(B_new) >= K:
        ans += 1 << i
        B = [_ % 1 << i for _ in B_new]
    else:
        B = [_ % 1 << i for _ in B]
print(ans)

Submission status: Wrong Answer

Solution code:
a,b = [int(_) for _ in input().split()]

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

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

print(la)

Submission status: 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)

max_val = max(sum_list)
max_2pow = len(bin(max_val)[2:])
ans = 2**(max_2pow-1)
# print(ans,max_2pow)
while max_2pow > 1:
    max_2pow -= 1
    tmp = ans + 2**(max_2pow-1)
    if sum([1 for v in sum_list if v & tmp == tmp]) >= K:
        ans += 2**(max_2pow-1)
    # print(ans, max_2pow)

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**6)

def main():
	n, k = map(int, input().split())
	a = [0] + [int(i) for i in input().split()]
	b = []
	for i in range(1,n+1):
		a[i] = a[i-1] + a[i]
	for i in range(0,n):
		for j in range(i+1, n+1):
			b += [a[j] - a[i]] 
	ans = 0
	for i in range(35, -1, -1):
		count = 0
		for temp in b:
			ref = ans + 2**i
			ref2 = temp & ref
			if ref2 == ref:
				count += 1
		if count >= k:
			ans = ans + 2**i
	print(ans)

main()

Submission status: Wrong Answer

Solution code:
import math

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[0].append(added_a[j]-added_a[i])

beauty[0].sort()
beauty[0].reverse()
tmp=0
flag=1
max_exponent=math.floor(math.log(beauty[0][0])/math.log(2))
for i in range(max_exponent+1):
    if flag==1:
        beauty.append([])
        # print(beauty[i])
        cnt=0
        for j in range(len(beauty[i])):
            if beauty[i][j]>=2**(max_exponent-i):
                cnt+=1
                beauty[i+1].append(beauty[i][j]-2**(max_exponent-i))
        if cnt>=k:
            tmp+=2**(max_exponent-i)
        else:
            beauty[i+1]=beauty[i]

print(tmp)

Submission status: Wrong Answer

Solution code:
import math

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[0].append(added_a[j]-added_a[i])

beauty[0].sort()
beauty[0].reverse()
tmp=0
flag=1
max_exponent=math.floor(math.log(beauty[0][0])/math.log(2))
for i in range(max_exponent+1):
    if flag==1:
        beauty.append([])
        # print(beauty[i])
        cnt=0
        for j in range(len(beauty[i])):
            if beauty[i][j]>=2**(max_exponent-i):
                cnt+=1
                beauty[i+1].append(beauty[i][j]-2**(max_exponent-i))
        if cnt>=k:
            tmp+=2**(max_exponent-i)
        else:
            beauty[i+1]=beauty[i]
        beauty.sort()
        beauty.reverse()

print(tmp)

Submission status: Wrong Answer

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

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

A = []

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

A.sort()
A.reverse()

print(A[K-1])

Submission status: Wrong Answer

Solution code:
import math

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[0].append(added_a[j]-added_a[i])

beauty[0].sort()
beauty[0].reverse()
tmp=0
flag=1
max_exponent=math.floor(math.log(beauty[0][0])/math.log(2))
for i in range(max_exponent+1):
    if flag==1:
        beauty.append([])
        # print(beauty[i])
        cnt=0
        for j in range(len(beauty[i])):
            if beauty[i][j]>=2**(max_exponent-i):
                cnt+=1
                beauty[i+1].append(beauty[i][j]-2**(max_exponent-i))
        if cnt>=k:
            tmp+=2**(max_exponent-i)
        beauty.sort()
        beauty.reverse()
        
print(tmp)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**6)

def main():
	n, k = map(int, input().split())
	a = [int(i) for i in input().split()]
	b = []
	c = [0 for _ in range(40)]
	for i in range(0,n):
		s = 0
		for j in range(i, n):
			s += a[j]
			b += [s]
	ans = 0
	for i in range(35, -1, -1):
		count = 0
		for temp in b:
			if temp & ans + 2**i == ans + 2**i:
				count += 1
		if count >= k:
			ans = ans + 2**i
	print(ans)

main()

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)

sub_sum_list = list(sum_list)
ans_bit = [0 for _ in range(64)]
for _ in range(64):
    bin_list = [[0 for _ in range(64)] for _ in range(len(sub_sum_list))]

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

    # pprint(bin_list)

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

    # pprint(sum_bin)

    max_keta = -1
    for i,s in enumerate( sum_bin):
        if s >= K and ans_bit[i] == 0:
            max_keta = i

    if max_keta == -1:
        break

    ans_bit[max_keta] = 1
    tmp = 0
    for i, b in enumerate(ans_bit):
        tmp += b*(2**i)

    sub_sum_list = [a for a in sub_sum_list if tmp <= a < tmp + 2**max_keta]

    # pprint(sub_sum_list)

ans = 0
for i,b in enumerate(ans_bit):
    ans += b*(2**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])
co = 0
la = 0

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

print(la)

Submission status: Wrong Answer

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

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)

sub_sum_list = list(sum_list)
ans_bit = [0 for _ in range(64)]
while True:
    bin_list = [[0 for _ in range(64)] for _ in range(len(sub_sum_list))]

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

    # pprint(bin_list)

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

    # pprint(sum_bin)

    max_keta = -1
    for i,s in enumerate( sum_bin):
        if s >= K and ans_bit[i] == 0:
            max_keta = i

    if max_keta == -1:
        break

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

    # pprint(sub_sum_list)

ans = 0
for i,b in enumerate(ans_bit):
    ans += b*(2**i)
print(ans)

Submission status: Wrong Answer

Solution code:
from itertools import accumulate


def calc(use):
    mod = 1 << 10
    ans = (1 << 50) - 1
    for lr in use:
        l, r = divmod(lr, mod)
        ans &= acc[r] - acc[l]
    return ans


n, k = map(int, input().split())
aaa = list(map(int, input().split()))
acc = [0] + list(accumulate(aaa))
bits = [set() for _ in range(50)]

for l in range(n):
    acl = acc[l]
    li = l << 10
    for r in range(l + 1, n + 1):
        beu = acc[r] - acl
        ri = li + r
        i = 0
        while beu:
            beu, b = divmod(beu, 2)
            if b == 1:
                bits[i].add(ri)
            i += 1

bits.reverse()
# print(bits)
ans = None
use = None
remain = None
secondary = None

for i, b in enumerate(bits):
    if use is None:
        if len(b) < k:
            continue
        use = b.copy()
        continue
    if remain is None:
        new_use = use.intersection(b)
        if len(new_use) > k:
            use = new_use
        elif len(new_use) == k:
            ans = calc(new_use)
            break
        else:
            remain = new_use
            secondary = use.difference(new_use)
        continue
    new_use = secondary.intersection(b)
    if len(remain) + len(new_use) == k:
        remain.update(new_use)
        ans = calc(remain)
        break
    if len(remain) + len(new_use) < k:
        secondary.difference_update(new_use)
        remain.update(new_use)
    else:
        use = remain.update(new_use)
        remain = None
if ans is None:
    ans = calc(use)

print(ans)

Submission status: Wrong Answer

Solution code:
import copy
N, K = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
ans = []
temp = 0
p = 0

for i in range(int(N*(N+1)/2)):
    temp += i+1
    if temp >= K:
        p = i
        break

#print(p)
list = []

for i in range(p+1):
    b = copy.deepcopy(a)
    #print(b)
    for j in range(i):
        del b[0]
    for j in range(p-i):
        del b[len(b)-1]
    #print(b)
    list.append(b)

for i in range(len(list)):
    ans.append(sum(list[i]))

#print(ans)
print(max(ans))

Submission status: Wrong Answer

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


def main():
    from bisect import bisect
    from itertools import accumulate

    n, k = map(int, input().split())
    a = list(accumulate([0] + list(map(int, input().split()))))
    summed_a = list()

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

    b = [2 ** i for i in range(42)]
    subarray = [[] for _ in range(42)]

    for summed_ai in summed_a:
        subarray[bisect(b, summed_ai)].append(summed_ai)

    for sub in list(reversed(subarray)):
        if len(sub) >= k:
            c = sorted(sub, reverse=True)[:k]
            ans = c[0]

            for y in range(1, len(c)):
                ans = int(bin(ans & c[y]), 2)

            print(ans)
            exit()

    print(0)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import numpy as np
from functools import reduce
def solve():
    N, K = map(int, input().split())
    A = np.array(list(map(int, input().split())))
    S = np.zeros((N,N), dtype=int)
    for i in range(N):
        S[np.arange(i+1), np.arange(i,-1,-1)] = S[np.arange(i+1), np.arange(i,-1,-1)-1]+A[i]
    S = S.ravel()
    S.sort()
    S = S[::-1]
    b = reduce(int.__and__, map(int,S[:K]))
    return b
    
if __name__ == '__main__':
    print(solve())

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
count = 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
    count = 0
  else:
    count += 1
    if count == K:
      break
  #print(tmp)
#print(sort_lst)
print(ans)
#print(len(sort_lst))

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 = mask = 0
top = N
for blen_tmp in list(range(blen))[::-1]:
    mask += (1 << blen_tmp)
    cnt = 0
    # print("#", blen_tmp, mask)
    for size in list(range(1, top + 1))[::-1]:
        cnt_tmp = 0
        for start in range(N + 1 - size):
            blr = sumtab[start + size] - sumtab[start] # beautifulness
            # print("##", start + 1, start + size, blr)
            if blr < mask: continue
            elif blr & mask == mask: cnt_tmp += 1
            # loop end
            if cnt + cnt_tmp >= K: break
        # loop end
        cnt += cnt_tmp
        if cnt >= K: break
        elif cnt_tmp == 0: break
    if cnt >= K:
        ans = mask
    else:
        mask -= (1 << blen_tmp)
        top = size

print(ans)

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]
nu = []
h = 0
cou = 2 ** 35
while len(nu) < k:
  cou /= 2
  while li[h] >= cou:
    nu.append(li[h])
    h += 1
  # print(nu)
ans = {(2**35-1,0)}
for i in range(len(nu)):
  gone = set()
  for num,co in ans:
    if co < k:gone.add((num&nu[i],co+1))
    if co + (n-i) >= k:gone.add((num,co))
  ans = gone
an = 0
for v,b in ans:
  if b == k:an = max(an,v)
print(an)

Submission status: Wrong Answer

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

ls = []
for i in range(n):
    for j in range(i, n):
        ls += [arr[i:j+1]]

ls2 = [0] * len(ls)        
for l in range(len(ls)):
    ls2[l] = sum(ls[l])

ls2.sort(reverse=True)

print(ls2[k-1])

Submission status: Wrong Answer

Solution code:
import bisect
N, K = map(int, input().split())
A = list(map(int, input().split()))
S = [sum(A[:i]) for i in range(N + 1)]
sum_A = [S[r] - S[l] for l in range(N + 1) for r in range(l + 1, N + 1) if r != l]

# sum_A.sort()
# a = sum_A[-1]
# m = len(bin(a)[2:])
# M = pow(2, m)
# r = bisect.bisect_left(sum_A, M)
# num = T - r
# while num < K:
#     M //= 2
#     r = bisect.bisect_left(sum_A, M)
#     num = T - r
#     print(r, num, K)
#
# print(sum_A[r:])

def g(A):
    res = A[0]
    for a in A[1:]:
        res &= a
    return res

def f(A):
    T = len(A)
    if T == K:
        return g(A)

    A.sort()
    a = A[-1]
    m = len(bin(a)[2:])
    M = pow(2, m)
    r = bisect.bisect_left(A, M)
    num = T - r

    while num < K:
        M //= 2
        r = bisect.bisect_left(A, M)
        num = T - r
    if M != 0:
        return M + f([a % M for a in A[r:]])
    else:
        return 0

print(f(sum_A))

Submission status: Wrong Answer

Solution code:
import math

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()
tmp=math.floor(math.log(beauty[0])/math.log(2))
flag=1
for i in range(tmp,0,-1):
    if flag==1:
        # print(beauty)
        cnt=0
        for j in range(n):
            if beauty[j]>=(2**i):
                cnt+=1
                if cnt==k:
                    flag=0
                    break
                else:
                    beauty[j]-=2**i
            else:
                beauty.sort()
                beauty.reverse()
                break


print(beauty[k-1])

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)):
		if(bubunwa[j]-bubunwa[i] != 0):
			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:
def getmaxand(bs, K):
    for k in range(35, -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:
# -*- 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(64)] for _ in range(len(sum_list))]

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

Submission status: Wrong Answer

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

beauty = list()
for i in range(N):
    for j in range(i, N):
        # print(sum(a[i:j+1]), end=', ')
        beauty.append(sum(a[i:j + 1]))
    # print()

inf = 1099511627775
dp = [[0] * K for _ in range((N * (N + 1)) // 2)]
for i in range((N * (N + 1)) // 2 - 1):
    for k in range(K):
        if k > 0:
            dp[i + 1][k] = max(dp[i][k - 1] & beauty[i], dp[i][k])
        else:
            dp[i + 1][k] = max(beauty[i], dp[i][k])
#     print(beauty[i])
# print(dp)

print(max([d[-1] for d in dp]))