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 itertools import accumulate
from functools import reduce

N,K = map(int,input().split())
A = list(map(int,input().split()))
acmA = [0] + list(accumulate(A))
sumA = []
for i in range(1,N+1):
  for j in range(i,N+1):
    sumA.append(acmA[j]-acmA[i-1])
sumA.sort(reverse=True)
print(reduce(lambda x, y:x&y, sumA[:K]))

Submission status: Wrong Answer

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

A.sort(reverse = True)
# print (A)

ans = 0
count = (N * (N + 1)) // 2
for i in range(count - K + 1):
    tmp = (1 << 18) - 1
    # print (tmp)
    for k in range(K):
        tmp &= A[i + k]
    ans = max(ans, tmp)

print (ans)

Submission status: Wrong Answer

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

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

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

B=[]    #美しさの全列挙
l=len(S)

for i in range(l):
    for j in range(i+1,l):
        B.append(S[j]-S[i])

B.sort(reverse=True)

kouho=[ 1 for _ in range(len(B))]    #部分和の合計値で、候補となる値を1のbitで表現する

for keta in range(15,-1,-1):
    new_kouho=[]
    goukei=0 #1の出現回数
    for idx,b in enumerate(B):
        b=b>>keta&1
        if kouho[idx]==1:   #元々の候補に入っていれば、値を次の候補に残す
            new_kouho.append(b)
            goukei+=b
        else:
            new_kouho.append(0)       
    if K<=goukei:
        kouho=new_kouho #goukeiがK以上だったら、絞った候補を元の候補列に代入

ans=None
for k,b in zip(kouho,B):
    if k==1:
        if ans is None:
            ans=b
        else:
            ans&=b

print ans

Submission status: Wrong Answer

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

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

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

B=[]    #美しさの全列挙
l=len(S)

for i in range(l):
    for j in range(i+1,l):
        B.append(S[j]-S[i])

B.sort(reverse=True)

dp=[ 0 for _ in range(K+1) ]

for idx,b in enumerate(B):
    if idx==0:
        dp[1]=b
    else:
        for i in range(K,0,-1):
            if dp[i-1]!=0:
                dp[i]=max(dp[i],dp[i-1]&b)

print dp[K]

Submission status: Wrong Answer

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

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

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

B=[]    #美しさの全列挙
l=len(S)

for i in range(l):
    for j in range(i+1,l):
        B.append(S[j]-S[i])

B.sort(reverse=True)

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

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
#l[i][j]でi~jの美しさ
l1=list(map(int,input().split()))
l2=[]
for i in range(n):
  sum1=0
  for j in range(i,n):
    sum1+=l1[j]
    l2.append(sum1)
l2=sorted(l2)
d=[0 for i in range(16)]
bitlist=[]
for i in l2:
  bit=[]
  num=i
  for j in range(16):
    d[j]+=num%2
    bit.append(num%2)
    num//=2
  bitlist.append(bit)
index=-1
for i in range(16):
  if d[i]>=k:
    index=i
    
if index==-1:
  print(0)
  exit()
b=True
ans1=[index]
while b:
  bitsub=[]
  for i in bitlist:
    if i[index]==0:
      pass
    else:
      bitsub.append(i)
  d=[0 for i in range(16)]
  for i in bitsub:
    for j in range(16):
      d[j]+=i[j]
  b=False
  for i in range(1,index+1):
    if d[index-i]>=k and not b:
      index=index-i
      b=True
      ans1.append(index)
  bitlist=bitsub
ans=0
for j in ans1:
  ans+=bitlist[0][j]*2**j
print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
#l[i][j]でi~jの美しさ
l1=list(map(int,input().split()))
l2=[]
for i in range(n):
  sum1=0
  for j in range(i,n):
    sum1+=l1[j]
    l2.append(sum1)
l2=sorted(l2)
d=[0 for i in range(16)]
bitlist=[]
for i in l2:
  bit=[]
  num=i
  for j in range(16):
    d[j]+=num%2
    bit.append(num%2)
    num//=2
  bitlist.append(bit)
index=-1
for i in range(16):
  if d[i]>=k:
    index=i
    
if index==-1:
  print(0)
  exit()
b=True
ans1=[index]
while b:
  bitsub=[]
  for i in bitlist:
    if i[index]==0:
      pass
    else:
      bitsub.append(i)
  d=[0 for i in range(16)]
  for i in bitsub:
    for j in range(16):
      d[j]+=i[j]
  b=False
  for i in range(1,index+1):
    if d[index-i]>=k and not b:
      index=index-i
      b=True
      ans1.append(index)
  bitlist=bitsub
ans=0
for j in ans1:
  ans+=bitlist[0][j]*2**j
print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
l=[[0 for i in range(n)] for j in range(n)]
#l[i][j]でi~jの美しさ
l1=list(map(int,input().split()))
for i in range(n):
  sum1=0
  for j in range(i,n):
    sum1+=l1[j]
    l[i][j]=sum1 
l2=[]
for i in range(n):
  for j in range(i,n):
    l2.append(l[i][j])
l2=sorted(l2)
d={}
for i in range(16):
  d[2**i]=0
bitlist=[]
for i in l2:
  bit=[]
  num=i
  for j in range(16):
    d[2**j]+=num%2
    bit.append(num%2)
    num//=2
  bitlist.append(bit)
index=-1
for i in range(16):
  if d[2**i]>=k:
    index=i
if index==-1:
  print(0)
  exit()
b=True
ans1=[index]
while b:
  bitsub=[]
  for i in bitlist:
    if i[index]==0:
      pass
    else:
      bitsub.append(i)
  d=[0 for i in range(16)]
  for i in bitsub:
    for j in range(16):
      d[j]+=i[j]
  b=False
  for i in range(1,index+1):
    if d[index-i]>=k and not b:
      index=index-i
      b=True
      ans1.append(index)
  bitlist=bitsub
ans=0
for j in ans1:
  ans+=bitlist[0][j]*2**j
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
from itertools import accumulate, combinations
from collections import Counter
n, k = [int(i) for i in sys.stdin.readline().split()]
a_ls = [int(i) for i in sys.stdin.readline().split()]
s_ls = [i for i in accumulate([0] + a_ls)]
s_ls = [s_ls[j] - s_ls[i] for i, j in combinations(range(n+1), 2)]
str_len = len(bin(max(s_ls))) - 2
s_ls.sort(reverse=True)
ct = Counter()
for ind in range(str_len - 1, -1, -1):
    res = 0
    flg = False
    for s in s_ls:
        if (s >> ind) & 1:
            res += 1
        if res >= k:
            flg = True
            break
    if flg:
        break
if not flg:
    ls = [0] * k
else:
    ls = [l & ((1 << (ind + 1)) - 1) for l in s_ls]
ls.sort(reverse=True)
res = (1 << (ind + 1)) - 1
for l in ls[:k]:
    res &= l
print(res)

Submission status: Wrong Answer

Solution code:
import sys
from itertools import accumulate, combinations
from collections import Counter
n, k = [int(i) for i in sys.stdin.readline().split()]
a_ls = [int(i) for i in sys.stdin.readline().split()]
s_ls = [i for i in accumulate([0] + a_ls)]
s_ls = [s_ls[j] - s_ls[i] for i, j in combinations(range(n+1), 2)]
str_len = len(bin(max(s_ls))) - 2
s_ls.sort(reverse=True)
ct = Counter()
for ind in range(str_len - 1, -1, -1):
    res = 0
    flg = False
    for s in s_ls:
        if (s >> ind) & 1:
            res += 1
        if res >= k:
            flg = True
            break
    if flg:
        break
ls = [l & ((1 << (ind + 1)) - 1) for l in s_ls]
ls.sort(reverse=True)
res = (1 << (ind + 1)) - 1
for l in ls[:k]:
    res &= l
print(res)

Submission status: Wrong Answer

Solution code:
import sys
from itertools import accumulate, combinations
from collections import Counter
n, k = [int(i) for i in sys.stdin.readline().split()]
a_ls = [int(i) for i in sys.stdin.readline().split()]
s_ls = [i for i in accumulate([0] + a_ls)]
s_ls = [s_ls[j] - s_ls[i] for i, j in combinations(range(n+1), 2)]
str_len = len(bin(max(s_ls))) - 2
s_ls.sort(reverse=True)
ct = Counter()
for s in s_ls:
    for ind in range(str_len):
        if (s >> ind) & 1:
            ct.update([ind])
s = ""
flg = False
for key, v in sorted(ct.items(), key=lambda x:x[0], reverse=True):
    key = int(key)
    if v >= k:
        break
ls = [l & ((1 << (key + 1)) - 1) for l in s_ls]
ls.sort(reverse=True)
print(ls[k-1])

Submission status: Wrong Answer

Solution code:
import sys
from itertools import accumulate, combinations
from collections import Counter
n, k = [int(i) for i in sys.stdin.readline().split()]
a_ls = [int(i) for i in sys.stdin.readline().split()]
s_ls = [i for i in accumulate([0] + a_ls)]
s_ls = [s_ls[j] - s_ls[i] for i, j in combinations(range(n+1), 2)]
str_len = len(bin(max(s_ls))) - 2
ls = [bin(i)[2:].zfill(str_len) for i in s_ls]
x_ls = ["".join([str(str_len - ind) for ind, i in enumerate(s) if i == "1"]) for s in ls]
ls.sort(reverse=True)
ct = Counter("".join(x_ls))
s = ""
flg = False
for key, v in sorted(ct.items(), key=lambda x:x[0], reverse=True):
    key = int(key)
    if v >= k:
        break
ls = [int(l[-int(key):], 2) for l in ls]
ls.sort(reverse=True)
print(ls[k-1])

Submission status: Wrong Answer

Solution code:
import sys
from itertools import accumulate, combinations
from collections import Counter
n, k = [int(i) for i in sys.stdin.readline().split()]
a_ls = [int(i) for i in sys.stdin.readline().split()]
s_ls = [i for i in accumulate([0] + a_ls)]
s_ls = [s_ls[j] - s_ls[i] for i, j in combinations(range(n+1), 2)]
ls = ["".join([str(ind) for ind, j in enumerate(bin(i)[2:][::-1]) if j == "1"]) for i in s_ls]
x = []
while len(ls) > k:
    ct = Counter("".join(ls))
    s = ""
    flg = False
    for key, v in sorted(ct.items(), key=lambda x:x[0], reverse=True):
        key = int(key)
        if key in x:
            continue
        if v >= k:
            s = str(key)
            flg = True
            x.append(int(key))
            break
    if not flg:
        break
    ls = [i for i in ls if s in i]
print(sum([2 ** i for i in x]) + 0)

Submission status: Wrong Answer

Solution code:
def examA():
    N = I(); A = LI()
    sumnale = sum(A)/N
    cur = 10**9; curL = 0
    for i in range(N):
        if cur>abs(A[i]-sumnale):
            cur = abs(A[i]-sumnale)
            curL = i
    ans = curL
    print(ans)
    return

def examB():
    N, K = LI(); A = LI()
    AK = []; ans = 0; ansC = 0
    d = defaultdict(int)
    for i in range(N):
        cur = A[i]
        AK.append(cur)
        for k in range(32):
            if cur&(1<<k)==(1<<k):
                d[k] +=1
        for j in range(i+1,N):
            cur += A[j]
            AK.append(cur)
            for k in range(32):
                if cur & (1 << k) == (1 << k):
                    d[k] += 1
#    print(AK)
#    print(d)
    k = 32; curA = []
    while(k>0):
        k -=1
        if d[k]<K:
            continue
        for i in range(len(AK)):
            if AK[i]&(1<<k)==(1<<k):
                curA.append(AK[i])
        if len(curA)>=K:
            ans = 2**k
            break
#    print(curA)
    while(k>0):
        k -=1; neA = []
        if d[k]<K:
            continue
        for i in range(len(curA)):
            if curA[i]&(1<<k)==(1<<k):
                neA.append(curA[i])
        if len(neA)<K:
            continue
        else:
            curA = neA
            ans += 2**k
    print(ans)
    return

import sys,copy,bisect,itertools,heapq,math
from heapq import heappop,heappush,heapify
from collections import Counter,defaultdict,deque
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LSI(): return list(map(str,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def SI(): return sys.stdin.readline().strip()
mod = 10**9 + 7
inf = float('inf')

if __name__ == '__main__':
    examB()

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
A = list(map(int,input().split()))
B = [0 for _ in range(N+1)]
for i in range(1,N+1):
    B[i] = B[i-1]+A[i-1]
C = A[:]
for i in range(N-1):
    for j in range(i+1,N):
        C.append(B[j+1]-B[i])
C = sorted(C,reverse=True)
a = C[0]
for i in range(1,K):
    a = a&C[i]
cmax = a
print(cmax)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
A = list(map(int,input().split()))
B = [0 for _ in range(N+1)]
for i in range(1,N+1):
    B[i] = B[i-1]+A[i-1]
C = A[:]
for i in range(N-1):
    for j in range(i+1,N):
        C.append(B[j+1]-B[i])
C = sorted(C,reverse=True)
a = C[0]
for i in range(1,K):
    a = a&C[i]
cmax = a
for i in range(1,len(C)-K+1):
    b = (a|~C[i-1])&C[i+K-1]
    a = a&C[i+K-1]
    a = a|b
    cmax = max(cmax,a)
print(cmax)

Submission status: Wrong Answer

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

csum = [0 for _ in range(N+1)]
for i in range(N):
  csum[i+1] = csum[i] + A[i]

B = []
for i in range(1,N+1):
  for j in range(i,N+1):
    B.append(csum[j]-csum[i-1])
B.sort(reverse = True)
#print(B)

ans = 0
for i in range(N*(N+1)//2-K):
  b = B[i]
  if ans > b:
    break
  for j in range(i,i+K):
    b &= B[j]
  ans = max(ans,b)
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(s[-1] + a[i])

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

ret = 0
for i in range(31, -1, -1) :
    sub_ = []
    for sv in sub :
        if sv & (1 << i) :
            sub_.append(sv)
            
    if len(sub_) >= K :
        ret += (1 << i)
        sub = sub_
    
print(ret)

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(s[-1] + a[i])

sub = []
for i in range(N + 1) :
    for j in range(i + 1, N + 1) :
        sub.append(s[j] - s[i])
        
for i in range(31, -1, -1) :
    sub_ = []
    for sv in sub :
        if sv & (1 << i) :
            sub_.append(sv)
            
    if len(sub_) >= K :
        sub = sub_
        
ret = sub[0]
for i in range(1, K) :
    ret &= sub[i]
    
print(ret)

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(s[-1] + a[i])

sub = []
for i in range(N + 1) :
    for j in range(i + 1, N + 1) :
        sub.append(s[j] - s[i])
        
for i in range(30, -1, -1) :
    sub_ = []
    for sv in sub :
        if sv & (1 << i) :
            sub_.append(sv)
            
    if len(sub_) >= K :
        sub = sub_
        
ret = sub[0]
for i in range(1, K) :
    ret &= sub[i]
    
print(ret)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.buffer.readline
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getlist():
    return list(map(int, input().split()))
import heapq
import bisect
import copy
import math

def warshall_floyd(d, N):
    for k in range(N):
        for i in range(N):
            for j in range(N):
                d[i * N +j] = min(d[i * N +j], d[i * N + k] + d[k * N + j])
    return d

MOD = 10**9 + 7

def main():
    n, k = getlist()
    nums = getlist()
    acc = [0]
    tmp = 0
    for num in nums:
        tmp += num
        acc.append(tmp)

    poss = []
    for r in range(n+1):
        for l in range(r):
            poss.append(acc[r] - acc[l])

    poss.sort()
    # print(poss)
    lborder, rborder = 0, (n * (n+1)) // 2

    ans, digit = 0, math.floor(math.log(poss[-1], 2)) + 1
    # print(digit)
    tmp = 0
    while(digit >= 0):
        tmp += 2 ** (digit - 1)
        mid = bisect.bisect_left(poss, tmp)
        if rborder - mid >= k:
            lborder = mid
        elif mid - lborder >= k:
            rborder = mid
            tmp -= 2 ** (digit - 1)
        else:
            print(tmp - 2 ** (digit - 1))
            return

        digit -= 1

    print(tmp)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#XorではなくAND
#上の桁優先、不可能なら無視
import bisect
n,k=map(int, input().split())
a_ = list(map(int, input().split()))

a = []

for i in range(n):
    a_sm = a_[i]
    a.append(a_sm)
    for j in range(i+1,n):
        a_sm+=a_[j]
        a.append(a_sm)

a.sort()
#print(a)
ans = 0
for j in range(41,-1,-1):##10**9を1000個足すので30では足りない
    x = 1<<j
    t = bisect.bisect_left(a,x)
    if t<=len(a)-k-1:###
        ans+=x
        a = a[t:]
        """
        for i in range(len(a)):
            a[i]&=(x-1)
        print(a)
        """
    for i in range(len(a)):
        a[i]&=(x-1)
        
    a.sort()
print(ans)

Submission status: Wrong Answer

Solution code:
#XorではなくAND
#上の桁優先、不可能なら無視
import bisect
n,k=map(int, input().split())
a_ = list(map(int, input().split()))

a = []

for i in range(n):
    a_sm = a_[i]
    a.append(a_sm)
    for j in range(i+1,n):
        a_sm+=a_[j]
        a.append(a_sm)

a.sort()
#print(a)
ans = 0
for j in range(30,-1,-1):
    x = 1<<j
    t = bisect.bisect_left(a,x)
    if t<=len(a)-k-1:###
        ans+=x
        a = a[t:]
        """
        for i in range(len(a)):
            a[i]&=(x-1)
        print(a)
        """
    for i in range(len(a)):
        a[i]&=(x-1)
        
    a.sort()
print(ans)

Submission status: Wrong Answer

Solution code:
#XorではなくAND
#上の桁優先、不可能なら無視
import bisect
n,k=map(int, input().split())
a_ = list(map(int, input().split()))

a = []

for i in range(n):
    a_sm = a_[i]
    a.append(a_sm)
    for j in range(i+1,n):
        a_sm+=a_[j]
        a.append(a_sm)

a.sort()
#print(a)
ans = 0
for j in range(30,-1,-1):
    x = 1<<j
    t = bisect.bisect_left(a,x)
    if t<=len(a)-k:
        ans+=x
        a = a[t:]
        """
        for i in range(len(a)):
            a[i]&=(x-1)
        print(a)
        """
    for i in range(len(a)):
        a[i]&=(x-1)
        
    a.sort()#大小関係変わるので必要
print(ans)

Submission status: Wrong Answer

Solution code:
#XorではなくAND
#上の桁優先、不可能なら無視
import bisect
n,k=map(int, input().split())
a_ = list(map(int, input().split()))

a = []

for i in range(n):
    a_sm = a_[i]
    a.append(a_sm)
    for j in range(i+1,n):
        a_sm+=a_[j]
        a.append(a_sm)

a.sort()
#print(a)
ans = 0
for j in range(30,-1,-1):
    x = 1<<j
    t = bisect.bisect_left(a,x)
    if t<=len(a)-k:
        ans+=x
        a = a[t:]
        """
        for i in range(len(a)):
            a[i]&=(x-1)
        print(a)
        """
    for i in range(len(a)):
        a[i]&=(x-1)
        
    #a.sort()
print(ans)

Submission status: Wrong Answer

Solution code:
from fractions import gcd
from datetime import date, timedelta
from heapq import*
import math
from collections import defaultdict, Counter, deque
import sys
from bisect import *
import itertools
import copy
sys.setrecursionlimit(10 ** 7)
MOD = 10 ** 9 + 7


def main():
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    
    rui = [a[0]]
    for i in range(1, n):
        rui.append(rui[i - 1] + a[i])
    rui = [0] + rui
    ansl = []
    for i in range(n):
        for j in range(i,n):
            ansl.append(rui[j + 1] - rui[i])
    ansl = sorted(ansl, reverse=True)
    
    cnt = 0
    ansbit = 0
    L = []
    for i in range(51):
        bit = (1 << (50 - i))
        l = []
        for j in range(len(ansl)):
            if ansl[j] & bit == 0:
                l.append(ansl[j])
            else:
                L.append(ansl[j])
                cnt += 1
        if cnt >= k:
            ansbit = i
            break
        else:
            L = []
        # ansl = l

    ans = 0
    for i in range(ansbit,50):
        bit = (1 << (50 - i))
        LL = []
        cnt = 0
        for j in range(len(L)):
            if L[j] & bit != 0:
                cnt+=1
                LL.append(L[j])

        if cnt >= k:
            L = LL
            ans |= bit

        if len(L) < k:
            break
                

    
    print(ans)
    
                    


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from fractions import gcd
from datetime import date, timedelta
from heapq import*
import math
from collections import defaultdict, Counter, deque
import sys
from bisect import *
import itertools
import copy
sys.setrecursionlimit(10 ** 7)
MOD = 10 ** 9 + 7


def main():
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    
    rui = [a[0]]
    for i in range(1, n):
        rui.append(rui[i - 1] + a[i])
    rui = [0] + rui
    ansl = []
    for i in range(n):
        for j in range(i,n):
            ansl.append(rui[j + 1] - rui[i])
    ansl = sorted(ansl, reverse=True)
    
    cnt = 0
    ansbit = 0
    L = []
    for i in range(51):
        bit = (1 << (50 - i))
        l = []
        for j in range(len(ansl)):
            if ansl[j] & bit == 0:
                l.append(ansl[j])
            else:
                L.append(ansl[j])
                cnt += 1
        if cnt >= k:
            ansbit = i
            break
        # else:
        #     L = []
        # ansl = l

    ans = 0
    for i in range(ansbit,50):
        bit = (1 << (50 - i))
        LL = []
        cnt = 0
        for j in range(len(L)):
            if L[j] & bit != 0:
                cnt+=1
                LL.append(L[j])

        if cnt >= k:
            L = LL
            ans |= bit

        if len(L) < k:
            break
                

    
    print(ans)
    
                    


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from fractions import gcd
from datetime import date, timedelta
from heapq import*
import math
from collections import defaultdict, Counter, deque
import sys
from bisect import *
import itertools
import copy
sys.setrecursionlimit(10 ** 7)
MOD = 10 ** 9 + 7


def main():
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    
    rui = [a[0]]
    for i in range(1, n):
        rui.append(rui[i - 1] + a[i])
    rui = [0] + rui
    ansl = []
    for i in range(n):
        for j in range(i,n):
            ansl.append(rui[j + 1] - rui[i])
    ansl = sorted(ansl, reverse=True)
    
    cnt = 0
    ansbit = 0
    L = []
    for i in range(51):
        bit = (1 << (50 - i))
        l = []
        for j in range(len(ansl)):
            if ansl[j] & bit == 0:
                l.append(ansl[j])
            else:
                L.append(ansl[j])
                cnt += 1
        if cnt >= k:
            ansbit = i
            break
        else:
            L = []
        ansl = l

    ans = 0
    for i in range(ansbit,50):
        bit = (1 << (50 - i))
        LL = []
        cnt = 0
        for j in range(len(L)):
            if L[j] & bit != 0:
                cnt+=1
                LL.append(L[j])

        if cnt >= k:
            L = LL
            ans |= bit

        if len(L) < k:
            break
                

    
    print(ans)
    
                    


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from fractions import gcd
from datetime import date, timedelta
from heapq import*
import math
from collections import defaultdict, Counter, deque
import sys
from bisect import *
import itertools
import copy
sys.setrecursionlimit(10 ** 7)
MOD = 10 ** 9 + 7


def main():
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    
    rui = [a[0]]
    for i in range(1, n):
        rui.append(rui[i - 1] + a[i])
    rui = [0] + rui
    ansl = []
    for i in range(n):
        for j in range(i,n):
            ansl.append(rui[j + 1] - rui[i])
    ansl = sorted(ansl, reverse=True)
    
    cnt = 0
    ansbit = 0
    L = []
    for i in range(52):
        bit = (1 << (52 - i))
        l = []
        for j in range(len(ansl)):
            if ansl[j] & bit == 0:
                l.append(ansl[j])
            else:
                L.append(ansl[j])
                cnt += 1
        if cnt >= k:
            ansbit = i
            break
        else:
            L = []
        ansl = l

    ans = 0
    for i in range(ansbit,50):
        bit = (1 << (50 - i))
        LL = []
        cnt = 0
        for j in range(len(L)):
            if L[j] & bit != 0:
                cnt+=1
                LL.append(L[j])

        if cnt >= k:
            L = LL
            ans |= bit

        if len(L) < k:
            break
                

    
    print(ans)
    
                    


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from fractions import gcd
from datetime import date, timedelta
from heapq import*
import math
from collections import defaultdict, Counter, deque
import sys
from bisect import *
import itertools
import copy
sys.setrecursionlimit(10 ** 7)
MOD = 10 ** 9 + 7


def main():
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    
    rui = [a[0]]
    for i in range(1, n):
        rui.append(rui[i - 1] + a[i])
    rui = [0] + rui
    ansl = []
    for i in range(n):
        for j in range(i,n):
            ansl.append(rui[j + 1] - rui[i])
    ansl = sorted(ansl, reverse=True)
    
    cnt = 0
    ansbit = 0
    L = []
    for i in range(50):
        bit = (1 << (50 - i))
        l = []
        for j in range(len(ansl)):
            if ansl[j] & bit == 0:
                l.append(ansl[j])
            else:
                L.append(ansl[j])
                cnt += 1
        if cnt >= k:
            ansbit = i
            break
        else:
            L = []
        ansl = l

    ans = 0
    for i in range(ansbit,50):
        bit = (1 << (50 - i))
        LL = []
        cnt = 0
        for j in range(len(L)):
            if L[j] & bit != 0:
                cnt+=1
                LL.append(L[j])

        if cnt >= k:
            L = LL
            ans |= bit

        if len(L) < k:
            break
                

    
    print(ans)
    
                    


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from fractions import gcd
from datetime import date, timedelta
from heapq import*
import math
from collections import defaultdict, Counter, deque
import sys
from bisect import *
import itertools
import copy
sys.setrecursionlimit(10 ** 7)
MOD = 10 ** 9 + 7


def main():
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    
    rui = [a[0]]
    for i in range(1, n):
        rui.append(rui[i - 1] + a[i])
    rui = [0] + rui
    ansl = []
    for i in range(n):
        for j in range(i,n):
            ansl.append(rui[j + 1] - rui[i])
    ansl = sorted(ansl, reverse=True)
    
    cnt = 0
    ans = (1 << 50) - 1
    for i in range(50):
        bit = (1 << (50 - i))
        l = []
        for j in range(len(ansl)):
            # print(bit , ansl[j])
            # print(bit & ansl[j])
            if ansl[j] & bit == 0:
                l.append(ansl[j])
            else:
                ans &= ansl[j]
                cnt += 1
            if cnt == k:
                break
        if cnt == k:
            break
        ansl = l
    #ans = ansl[0]
    
    print(ans)
    
                    


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from fractions import gcd
from datetime import date, timedelta
from heapq import*
import math
from collections import defaultdict, Counter, deque
import sys
from bisect import *
import itertools
import copy
sys.setrecursionlimit(10 ** 7)
MOD = 10 ** 9 + 7


def main():
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    
    rui = [a[0]]
    for i in range(1, n):
        rui.append(rui[i - 1] + a[i])
    rui = [0] + rui
    ansl = []
    for i in range(n):
        for j in range(i,n):
            ansl.append(rui[j + 1] - rui[i])
    ansl = sorted(ansl, reverse=True)
    
    bit = 1 << 40
    cnt = 0
    ans = (1 << 40) - 1
    for i in range(40):
        bit = (1 << (40 - i))
        # print(bit)
        l = []
        for j in range(len(ansl)):
            # print(bit , ansl[j])
            # print(bit & ansl[j])
            if ansl[j] & bit == 0:
                l.append(ansl[j])
            else:
                ans &= ansl[j]
                cnt += 1
            if cnt == k:
                break
        if cnt == k:
            break
        ansl = l
    #ans = ansl[0]
    
    print(ans)
    
                    


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from fractions import gcd
from datetime import date, timedelta
from heapq import*
import math
from collections import defaultdict, Counter, deque
import sys
from bisect import *
import itertools
import copy
sys.setrecursionlimit(10 ** 7)
MOD = 10 ** 9 + 7


def main():
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    
    rui = [a[0]]
    for i in range(1, n):
        rui.append(rui[i - 1] + a[i])
    rui = [0] + rui
    ansl = []
    for i in range(n):
        for j in range(n):
            ansl.append(rui[j + 1] - rui[i])
    ansl = sorted(ansl,reverse=True)
    ans = ansl[0]
    for i in range(1, k):
        ans &= ansl[i]
    print(ans)
    
                    


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:

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

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

s = [0]
q = []
r = [0] * 41

for i in range(N):

    s.append(s[-1] + a[i])

ans = 0

for i in range(N+1):

    for j in range(i):

        now = s[i]-s[j]
        q.append(now)

        for k in range(41):
            if (2 ** k) & now > 0:
                r[k] += 1

for i in range(len(q)):

    for k in range(41):

        if r[k] < K and (2**k) & q[i] > 0:
            q[i] -= 2 ** k

q.sort()
q.reverse()
#print (q)

ans = q[0]

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

print (ans)

Submission status: Wrong Answer

Solution code:

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

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

s = [0]
q = []
r = [0] * 31

for i in range(N):

    s.append(s[-1] + a[i])

ans = 0

for i in range(N+1):

    for j in range(i):

        now = s[i]-s[j]
        q.append(now)

        for k in range(31):
            if (2 ** k) & now > 0:
                r[k] += 1

for i in range(len(q)):

    for k in range(31):

        if r[k] < K and (2**k) & q[i] > 0:
            q[i] -= 2 ** k

q.sort()
q.reverse()
#print (q)

ans = q[0]

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

print (ans)