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: Accepted

Solution code:
import itertools

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

for i in range(N):
    temp = 0
    for n in a[i:]:
        temp += n
        beautiful_num.append("{:040b}".format(temp))

ma_b_cnt = 0
target_set = None

result = 0
for i in range(40):
        for b in beautiful_num:
            if b[i] == "1":
                ma_b_cnt += 1
        if ma_b_cnt >= K:
            result += 2**(40-i-1)
            beautiful_num = [b for b in beautiful_num if b[i] == "1"]
        ma_b_cnt = 0

print(result)

Submission status: Accepted

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
	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: Accepted

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

@author: maezawa
"""

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: Accepted

Solution code:
N,K=map(int,input().split())
 
a=list(map(int,input().split()))
 
ruisekia=[]
keta=[0]*30
for i in range(N):
  temp=0
  for j in range(i,N):
    temp+=a[j]
    ruisekia.append(temp)
ruisekia.sort(reverse=True)
 
#print(ruisekia)
answer=0
for i in range(40,-1,-1):
  count=0
  ruiseki=[]
  for j in ruisekia:
    if j&(1<<i)==1<<i:
      count+=1
      ruiseki.append(j)
  if count>=K:
    answer+=2**i
    ruisekia=ruiseki

print(answer)
'''
for i in range(len(ruisekia)):
  tempo=ruisekia[i]
  while(tempo!=0):
    x=1
    count=0
    while(tempo>=x*2):
      x*=2
      count+=1
    keta[count]+=1
    tempo-=x
print(keta)
'''

Submission status: Accepted

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

ans=0
for i in range(40,-1,-1):
    n2=2**i
    c2=[i for i in c if i&n2==n2]
    if len(c2)>=k:
        ans+=n2
        c=c2
print(ans)

Submission status: Accepted

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

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

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

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

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

        SUM=NLIST

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

print(ANS)

Submission status: Accepted

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())
v = list(map(int,input().split()))
bubunwa = [0]

for i in range(len(v)):
	bubunwa.append(v[i]+bubunwa[-1])

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

ans = 0
good = []
for i in range(40,-1,-1):

	bit = 1 << i

	for elem in sums:
		if elem & bit == bit:
			good.append(elem)

	if len(good) >= K:
		ans += bit
		sums = good
		good = []
	else:
		good = []

print(ans)

Submission status: Accepted

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

A_sum = [0]
[A_sum.append(A_sum[-1]+int(a)) for a in input().split()]

score = []
for r in range(1,N+1):
    for l in range(0,r):
        score.append(A_sum[r]-A_sum[l])

ans = 0
for i in range(40,-1,-1):
    count = 0
    score_a = []
    for j in score:
        if (j & (1<<i)):
            count += 1
            score_a.append(j)
    if count >= K:
        ans += (1<<i)
        score = [i for i in score_a]

print(ans)

Submission status: Accepted

Solution code:
n, k = [int(v) for v in input().split()]
a = [int(v) for v in input().split()]

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

MAX = 40
ans = 0
for t in range(MAX, 0, -1):
    x = ans + (1 << (t-1))
    cnt = 0
    for v in b:
        if x & v == x:
            cnt += 1
    if cnt >= k:
        ans = x

print(ans)

Submission status: Accepted

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

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

score = []
for r in range(1,N+1):
    for l in range(0,r):
        score.append(A_sum[r]-A_sum[l])

ans = 0
for i in range(40,-1,-1):
    count = 0
    score_a = []
    for j in score:
        if (j & (1<<i)):
            count += 1
            score_a.append(j)
    if count >= K:
        ans += (1<<i)
        score = [i for i in score_a]

print(ans)

Submission status: Accepted

Solution code:
import sys

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

C = []

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

D = C
ans = 0
for i in range(40,-1,-1):
    yes = []
    no = []
    for l in D:
        if l & (1 << i):
            yes.append(l)
        else:
            no.append(l)

    if len(yes) >= K:
        ans += 1 << i
        D = yes

print(ans)

sys.exit(0)

Submission status: Accepted

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

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

score = []
for r in range(1,N+1):
    for l in range(0,r):
        score.append(A_sum[r]-A_sum[l])

ans = 0
for i in range(40,-1,-1):
    count = 0
    score_a = []
    for j in score:
        if (j & (1<<i)):
            count += 1
            score_a.append(j)
    if count >= K:
        ans += (1<<i)
        score = copy.deepcopy(score_a)

print(ans)

Submission status: Accepted

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 = 0
# print(ans,max_2pow)
while max_2pow > 0:
    tmp = ans + 2**(max_2pow-1)
    if sum([1 for v in sum_list if v & tmp == tmp]) >= K:
        ans += 2**(max_2pow-1)
    max_2pow -= 1

print(ans)

Submission status: Accepted

Solution code:
from math import log, ceil

n, k = map(int, input().split())
a = tuple(map(int, input().split()))

# A_MAX = 10 ** 9
# MAX_BIT_LENGTH = ceil(log(A_MAX * n, 2))

cs = [0]
t = 0
for aa in a:
    t += aa
    cs.append(t)

ps = []
for r in range(1, n + 1):
    for l in range(r):
        ps.append(cs[r] - cs[l])

MAX_BIT_LENGTH = ceil(log(max(ps), 2))

ans = 0
for b in range(MAX_BIT_LENGTH)[::-1]:
    cnt = 0
    t = ans + (1 << b)
    for p in ps:
        if p & t == t:
            cnt += 1
            if cnt == k:
                ans = t
                break

print(ans)

Submission status: Accepted

Solution code:
from math import log, ceil

n, k = map(int, input().split())
a = tuple(map(int, input().split()))

A_MAX = 10 ** 9
MAX_BIT_LENGTH = ceil(log(A_MAX * n, 2))

cs = [0]
t = 0
for aa in a:
    t += aa
    cs.append(t)

ps = []
for r in range(1, n + 1):
    for l in range(r):
        ps.append(cs[r] - cs[l])

ans = 0
for b in range(MAX_BIT_LENGTH)[::-1]:
    cnt = 0
    t = ans + (1 << b)
    for p in ps:
        if p & t == t:
            cnt += 1
            if cnt == k:
                ans = t
                break

print(ans)

Submission status: Accepted

Solution code:
n, k = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]

B = [0]

for a in A:
    B.append(B[-1] + a)

C = []

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

ans = 0
flag = False
D = []

for i in range(40, -1, -1):
    if flag:
        C = D
        flag = False
    D = []
    bit = 1 << i
    for c in C:
        if bit & c:
            D.append(c)
    if len(D) >= k:
        flag = True
        ans += bit
    
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict
from itertools import accumulate

n, k = map(int, input().split())
aaa = list(map(int, input().split()))
acc = [0] + list(accumulate(aaa))
subs = defaultdict(lambda: 0)

for l in range(n):
    acl = acc[l]
    for r in range(l + 1, n + 1):
        beu = acc[r] - acl
        subs[beu] += 1

ans = 0

for i in range(40):
    bit = 1 << (39 - i)
    new_subs = {}
    remain = 0

    for sub, cnt in subs.items():
        if sub & bit > 0:
            new_subs[sub] = cnt
            remain += cnt

    if remain >= k:
        subs = new_subs
        ans |= bit

print(ans)

Submission status: Accepted

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

def f(m, A):
    if m < 0:
        return 0

    T = len(A)
    M = pow(2, m)
    r = bisect.bisect_left(A, M)
    num = T - r
    if num < K:
        return f(m - 1, sorted([a % M for a in A]))
    else:
        return M + f(m - 1, sorted([a % M for a in A[r:]]))

print(f(len(bin(sum_A[-1])) - 2, sum_A))

Submission status: Accepted

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

current_num = 0
for t in range(40, 0, -1):
    count = 0
    comp = current_num + 2**(t - 1)
    for b in all_beautifulness:
        if comp & b == comp:
            count += 1
    if count >= K:
        current_num += 2**(t - 1)
print(current_num)

Submission status: Accepted

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(sum(temp))
    if i==n:
        break
    i+=1

def check(num):
    for i in range(50):
        if (num < 2**i):
            return i-1, 2**(i-1)
mx = 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 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: Accepted

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

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

    MAX = x[0]
    two = 1
    POW = 1
    while(MAX>=two*2):
        two*=2
        POW+=1

#MAXBITソート
    for p in range(POW)[::-1]:
        C = list(filter(lambda y:y>=two,x))
        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))
        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)

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
def getmaxand(bs, K):
    for k in range(40, -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: Accepted

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: Accepted

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
    s = set()
    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: Accepted

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

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

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

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

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

    #print(COUNT)#,SUM)

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

        SUM=NLIST

    #print(SUM)

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

print(ANS)

Submission status: Accepted

Solution code:
def solve(N, K, As):
    alls = []
    for i in range(N):
        for j in range(i + 1, N + 1):
            alls.append("{:041b}".format(sum(As[i:j])))
    def find(i, alls):
        if i > 40:
            return []
        ones = [a for a in alls if a[i] == "1"]
        if len(ones) < K:
            return find(i + 1, alls)
        elif len(ones) == K:
            return ones
        elif len(ones) > K:
            ret = find(i + 1, ones)
            if len(ret) == 0:
                return ones[0:K]
            else:
                return ret

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

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]

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

ans = 0
for m in range(40, -1, -1):
    count = 0
    for i in range(len(b)):
        if ((b[i] >> m) & 1) == 1:
            count += 1
    if count >= k:
        ans += (1 << m)
        c = []
        for i in range(len(b)):
            if ((b[i] >> m) & 1) == 1:
                c.append(b[i])
        b = c

print(ans)

Submission status: Accepted

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

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

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

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

#for i in range(len(btyarr)):
#    for j in range(30, -1, -1):
#        b = 1 << j
#        if btyarr[i] & b:
#            chb = 1
#        else:
#            chb = 0
#        print(chb, end="")
#    print("")

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

print(ans)

Submission status: Accepted

Solution code:
import itertools
import bisect
n,K=map(int, input().split(" "))
A=list(map(int, input().split(" ")))

A2=[0]
sum=0
for i in A:
    A2.append(i+sum)
    sum+=i

#print(A2)
A3=[]
for i in range(len(A2)):
    for k in range(len(A2)-i-1):
        A3.append(A2[k+1+i]-A2[i])
A3.sort()
#print(A3)
#print(len(A3))

tmparr=[]
for i in range(50):
    if 2**i-1>A3[-1]:
        break
    tmparr.append(2**i-1)
#print(tmparr)


"""
for i in tmparr[::-1]:
    rem=len(A3)-bisect.bisect_right(A3,i)
    if rem>K:
        A4=A3[-rem:]
        break
"""
arr=A3
result=0
for i in tmparr[::-1]:
    count=0
    tmparr=[]
    for k in arr:
        if k & i+1 > 0:
            count+=1
            tmparr.append(k)
    if count>=K:
        arr=tmparr
        result+=i+1
print(result)

Submission status: Accepted

Solution code:
from itertools import accumulate
N, K = map( int, input().split())
A = list( map( int, input().split()))
accA = [0] + list( accumulate(A))
V = [1]*(N*(N+1)//2)
ans = 0
check = False
for i in range(40,-1,-1):
    W = [0]*(N*(N+1)//2)
    for l in range(N):
        for r in range(l+1,N+1):
            if V[l*(2*N+1-l)//2+r-l-1] == 0:
                continue
            if (accA[r]-accA[l])%(2**(i+1)) >= 2**i:
                W[l*(2*N+1-l)//2+r-l-1] = 1
    if check == False:
        if sum(W) >= K:
            check == True
        else:
            continue
    if sum( V and W) >= K:
        ans += 2**i
        V = V and W
print(ans)

Submission status: Accepted

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

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

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


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

print(ans)

Submission status: Accepted

Solution code:
import math


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

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


    print(ret)


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


main()

Submission status: Accepted

Solution code:
import math

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

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

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

res = 0

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

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

print(res)

Submission status: Accepted

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

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

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

print(ans)

Submission status: Accepted

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 & 2 ** i:
            B_new += [b]
    if len(B_new) >= K:
        ans += 2 ** i
        B = [_ - 2 ** i for _ in B_new]
    else:
        B = [_ % 2 ** i for _ in B]
print(ans)

Submission status: Accepted

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

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

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

b_nums = []
for num in nums:
    b_nums.append(num)

target_group = b_nums
for i in range(l_b_max_nums):
    p = pow(2, l_b_max_nums-i)
    _target_group = []
    l_target_group = len(target_group)
    for t in target_group:
        if (p & t >= p):
            _target_group.append(t)
    if len(_target_group) >= K:
        target_group = _target_group
ans = target_group[0]
l_target_group = len(target_group)
for i in range(1, l_target_group):
    ans = ans & target_group[i]
print(ans)


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

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

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

Submission status: Accepted

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

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

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



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


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

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

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

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

    
print(result)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
def inpl(): return list(map(int, input().split()))

N, K = inpl()
A = inpl()
tmp = 0
S = [0]*N
Q = []
for i in range(N):
    tmp += A[i]
    S[i] = tmp
    Q.append(tmp)

tmp = 0
for i in range(1, N):
    tmp = S[i-1]
    for j in range(i, N):
        #print(i, j)
        v = S[j] -S[i-1]
        Q.append(v)

res = 0
while True:
    ok = 0
    for i in range(40):
        num = res + int("1" + "0"*i, 2)
        tmp = 0
        for j in range(len(Q)):
            if num & Q[j] == num:
                tmp += 1
        if tmp >= K:
            ok = num
    if res < ok:
        res = ok
    else:
        break
print(res)

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
a=[int(i) for i in input().split()]
S=[0 for i in range(N+1)]
for i in range(1,N+1):
    S[i]=S[i-1]+a[i-1]
List=[]
for i in range(N+1):
    for j in range(i):
        List.append(S[i]-S[j])
List.sort(reverse=True)
def program(Y,L):
    #print(L)
    M=len(L)
    t=[bin(i)[2:][::-1] for i in L]
    b=[i+"0"*(Y-len(i)) for i in t]
    X=[[] for i in range(Y)]
    for i in b:
        for j in range(Y):
            if i[j]=="1":
                X[j].append(i)
    #print("X",X)
    l=Y
    for i in range(Y-1,-1,-1):
        if len(X[i])>=K:
            l=i
            break
    if l==Y:
        return 0
    #print(X[l])
    R=[]
    for i in X[l]:
        if i[:l]=="":
            R.append(0)
            continue
        R.append(int(i[:l][::-1],2))
    R.sort(reverse=True)
    return program(l,R)+2**l
#print(K,N)
print(program(42,List))

Submission status: Accepted

Solution code:
import bisect
import itertools

def find_ge(a, x):
    'Find leftmost item greater than or equal to x'
    i = bisect.bisect_left(a, x)
    return i
def find_le(a, x):
    'Find rightmost value less than or equal to x'
    i = bisect_right(a, x)
    return i-1

ans=0
pmax=41
N,K = [int(x) for x in raw_input().split()]
A = [int(x) for x in raw_input().split()]
B=A[:]
for i in range(1,N):
    B[i]=B[i-1]+A[i]
B = [0] + B[:]
C=[0 for x in range(N*(N+1)/2)]
i=0
for xx in itertools.combinations_with_replacement(range(N), 2):
    l,r=sorted(xx)
    r+=1
    q = B[r]-B[l]
    C[i] = q
    i+=1

while(1):
    D=[ [] for x in range(pmax)]
    for q in C:
        for j in range(pmax):
            if q & pow(2, j)==pow(2, j):
                D[j].append(q)
    for j in range(pmax-1 ,-1 ,-1):
        if len(D[j])>=K:
            ans += 2**j
            C = D[j][:]
            pmax = j
            break
    else:
        break
print ans
#C.sort()

# p=0
# while C[-1]<2**p:
#     p+=1
# for n in range(p,-1,-1):

Submission status: Accepted

Solution code:


N, K = map(int, input().split())
A = list(map(int, input().split()))
"""
if A == [1, 1] and K==2:
    print(1)
    exit()
"""
cumA = [0]
for a in A:
    cumA.append(a+cumA[-1])
from itertools import combinations
AA = []
for l, r in combinations(range(N+1), 2):
    AA.append(cumA[r] - cumA[l])

AA.sort(reverse=True)
ans = 0
d = len(bin(AA[0])) - 2
from bisect import bisect_right
while d>=0:
    #print(d, 1<<d, AA[K-1], AA)
    if AA[K-1] >= 1<<d:
        ans |= 1<<d
        #iaa = bisect_right(AA, 1<<d)
        #print(iaa, "iaa", 1<<d)
        #AA = AA[:iaa]
        i= 0
        while i < len(AA) and AA[i] & 1<<d:
            AA[i] ^= 1 << d
            i += 1
            if i==len(AA):
                break
        AA = AA[:i]

        d -= 1
        #print(AA)
    else:
        i=0
        #print("aaa")
        while i < len(AA) and AA[i] & 1<<d:
            AA[i] ^= 1<<d
            i += 1
        d -= 1
    AA.sort(reverse=True)
print(ans)
#print(AA)