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 sys
from itertools import accumulate, combinations
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
def check(x):
    cnt = 0
    for s in s_ls:
        if x == (x & s):
            cnt += 1
        if cnt >= k:
            return 1
    return 0

x = 0
res = []
for i in range(str_len - 1, -1, -1):
    cnt = 0
    for s in s_ls:
        if x + 2**i == ((x + 2**i) & s):
            cnt += 1
        if cnt >= k:
            x += 2 ** i
            break
print(x)

Submission status: Accepted

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
    d = defaultdict(int)
    for i in range(N):
        cur = A[i]
        AK.append(cur)
        for k in range(40):
            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(40):
                if cur & (1 << k) == (1 << k):
                    d[k] += 1
#    print(AK)
#    print(d)
    k = 40; 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 = copy.deepcopy(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: Accepted

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
li = []
for l in range(n):
    s = 0
    i = l
    while i < n:
        s += a[i]
        li.append(s)
        i += 1
# print(li)
ans = 0
for i in range(40):
    j = 39 - i
    if sum(map(lambda x: (x >> j) & 1, li)) >= k:
        ans += 1 << j
        tmp = []
        for x in li:
            if (x >> j) & 1:
                tmp.append(x)
        li = tmp
print(ans)

Submission status: Accepted

Solution code:
N, K = map(int,input().split())
A = list(map(int,input().split()))
L = []
for i in range(N):
  for j in range(i+1,N+1):
    L.append(sum(A[i:j]))
L.sort(reverse=True)
ans = 0
remains = []
for i in range(40):
  bit = 1 << (39 - i)
  l = []
  cnt = 0
  #print(bit)
  for j in range(len(L)):
    if bit & L[j] == bit:
      cnt += 1
      l.append(L[j])
  if cnt >= K:
    ans += bit
    L = l
print(ans)

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
A = list(map(int,input().split()))
S = [0]
beauties = []
ans = 0

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

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

for i in reversed(range(64)):
    tmp = []
    for j in range(len(beauties)):
        if (beauties[j]>>i)&1:
            tmp.append(beauties[j])
    if len(tmp)>=K:
        ans += 2**i
        beauties = tmp
        
print(ans)

Submission status: Accepted

Solution code:
import math
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)
n = math.ceil(math.log2(C[0]))
Cb = []
for i in range(len(C)):
    x = bin(C[i])[2:]
    k = len(x)
    x = "0"*(n-k)+x
    Cb.append(x)
inds = [i for i in range(len(C))]
nlow = 0
while len(inds)>K:
    for j in range(nlow,n):
        E = []
        cnt = 0
        for i in inds:
            if Cb[i][j]=="1":
                E.append(i)
                cnt += 1
        if cnt>=K:
            inds = E[:]
            nlow = j+1
            break
    if cnt<K:break
cnt = int("1"*n,2)
for i in inds: 
    cnt = cnt&C[i]
print(cnt)

Submission status: Accepted

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])
#print(B)

ans = 0
BB = B 
for i in range(41,-1,-1):
  BB_ = []
  for b in BB:
    if (b>>i)&1 == 1:
      BB_.append(b)
  if len(BB_) >= K:
    ans += (1<<i)
    BB = BB_
print(ans)

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) :
    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(40, -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: Accepted

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

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
    L = []
    for i in range(n):
        for j in range(i,n):
            L.append(rui[j + 1] - rui[i])

    ans = 0
    for i in range(51):
        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: Accepted

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(51):
        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: Accepted

Solution code:
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(100000)
input = sys.stdin.readline
INF = 2**62-1


def read_int():
    return int(input())


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


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, K, A):

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


    pool = s
    for b in range(s[-1].bit_length(), -1, -1):
        cnd = []
        v = 1 << b
        for a in pool:
            if a & v > 0:
                cnd.append(a)
        if len(cnd) >= K:
            pool = cnd

    return reduce(lambda x, y: x&y,  pool)

        




def main():
    N, K = read_int_n()
    A = read_int_n()
    print(slv(N, K, A))


if __name__ == '__main__':
    main()

Submission status: Accepted

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)

q.sort()
ans = 0

for i in range(41):

    i = 40 - i

    nq = []

    for j in range(len(q)):
        if q[j] & (2 ** i) > 0:
            nq.append(q[j])

    if len(nq) >= K:
        q = []
        ans += 2 ** i
        for i in nq:
            q.append(i)

print (ans)

Submission status: Accepted

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

r = [0] * (N + 1)
for i in range(N):
  r[i + 1] = r[i] + l[i]
z = []
for i in range(N):
  for j in range(i, N):
    z.append(r[j + 1] - r[i])
z.sort()
def check(m):
  c = 0
  for x in z:
    if x & m == m:
      c += 1
  return c >= K
x = z[-1].bit_length()
res = 0
while x > 0:
  if check(res + pow(2, x - 1)):
    res += pow(2, x - 1)
  x -= 1
print(res)

Submission status: Accepted

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

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

cs = [0] * (N + 1)
for i in range(N):
  cs[i + 1] = cs[i] + a[i]
lrsums = []
for i in range(N):
  for j in range(i, N):
    lrsums.append(cs[j + 1] - cs[i])
lrsums.sort()
#print(lrsums)
def check(m):
  c = 0
  for x in lrsums:
    if x & m == m:
      c += 1
  return c >= K
x = lrsums[-1].bit_length()
res = 0
while x > 0:
  if check(res + pow(2, x - 1)):
    res += pow(2, x - 1)
  x -= 1
print(res)

Submission status: Accepted

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


cand = []
for i in range(N):
    sum = 0
    for j in range(i, N):
        sum += a[j]
        cand.append(sum)


def countMask(a):
    ret = 0
    for x in cand:
        if (x & a) == a:
            ret += 1
    return ret


ans = 0

for i in range(40, -1, -1):
    if (countMask(ans | (1 << i)) >= K):
        ans |= 1 << i

print(ans)

Submission status: Accepted

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

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

data.sort()
ans = 0
for bit in range(45,-1,-1):
    new_data = []
    for d in data:
        if d>>bit&1:
            new_data.append(d)
    if len(new_data) >=k:
        data = new_data
        ans += 2**bit

print(ans)

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 6)
input = sys.stdin.readline

def main():
    n, k = map(int, input().split())
    aa = list(map(int, input().split()))
    ss = []
    for i in range(n):
        s = 0
        for j in range(i, n):
            s += aa[j]
            ss.append(s)
    ss.sort(reverse=True)
    # print(ss)
    msk = 1 << 50
    ans = 0
    for _ in range(50):
        msk >>= 1
        # print(format(msk,"b"),ss)
        if msk > ss[0]: continue
        pen = []
        for s in ss:
            if s & msk:
                pen.append(s)
        if len(pen) >= k:
            ss = pen
            ans += msk
    print(ans)

main()

Submission status: Accepted

Solution code:
N,K = [int(x) for x in input().split()]
A = [int(x) for x in input().split()]
cumu = [0]
for i in A:
    cumu.append(cumu[-1]+i)
cand = []

# i<j O(N^2)
for i in range(N):
    for j in range(N+1):
        if(i>=j): continue
        cand.append(cumu[j]-cumu[i])

# 10**18~=2**60
ans = 0
for i in range(61,-1,-1):
    li = []
    for j in cand:
        if((j>>i&1)==1):
            li.append(j)
    if(len(li)>=K):
        ans += 1<<i
        cand = li
print(ans)

Submission status: Accepted

Solution code:
N, K = map(int,input().split())
A = list(map(int,input().split()))
X = []
for l in range(N):
  r = l
  v = 0
  while r < N:
    v += A[r]
    X.append(v)
    r += 1
ans = 0
for i in range(60, -1, -1):
  Y = [ x for x in X if (x >> i & 1) == 1 ]
  if len(Y) >= K:
    ans += 1 << i
    X = Y
print(ans)

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
l=list(map(int,input().split()))
L=[0]
for i in range(N):
    L.append(L[-1]+l[i])
#print(L)
A=[]
for i in range(N):
    for j in range(i+1,N+1):
        A.append(L[j]-L[i])
#print(A)
B=[]
for i in range(len(A)):
    B.append(format(A[i],'040b'))
#print(B)


for i in range(40):
    cnt=0
    for j in B:
        if j[i]=="1":
            cnt+=1
    if cnt>=K:
        S=i
        break

ans=2**(39-S)
C=[]
for j in B:
    if j[S]=="1":
        C.append(j)
        
#print(ans)
#print(C)

for i in range(S+1,40):
    D=[]
    for j in C:
        if j[i]=="1":
            D.append(j)
    if len(D)>=K:
        ans+=2**(39-i)
        C=D
print(ans)

Submission status: Accepted

Solution code:
n,k = map(int, input().split())
l = [0] + list(map(int, input().split()))
a = list()
for i in range(n):
  l[i+1] += l[i]
a = [l[j]-l[i] for i in range(n) for j in range(i+1,n+1)]
x = 0
for i in range(40,-1,-1):
  t = 2**i
  b = list(filter(lambda y: y&(t+x) == (t+x),a))
  if len(b) >= k:
    a = b
    x += t
print(x)

Submission status: Accepted

Solution code:
n,k = map(int, input().split())
l = [0] + list(map(int, input().split()))
a = list()
for i in range(n):
  l[i+1] += l[i]
a = [l[j]-l[i] for i in range(n) for j in range(i+1,n+1)]
x = 0
for i in range(40,-1,-1):
  t = 2**i 
  if sum(map(lambda y:((y&(t+x)) == t+x),a)) >= k:
    x += t
print(x)

Submission status: Accepted

Solution code:
from collections import Counter, defaultdict
import sys
sys.setrecursionlimit(10 ** 5 + 10)
# input = sys.stdin.readline
from math import factorial
import heapq, bisect
import math
import itertools


import queue
from collections import deque



def main():
    num, k = map(int, input().split())
    data = list(map(int, input().split()))

    a_list = [0 for i in range(((num + 1) * num) // 2)]
    ind = 0
    for i in range(num):
        for j in range(i, num):
            if j == i:
                a_list[ind] = data[j]
            else:
                a_list[ind] = a_list[ind - 1] + data[j]
            ind += 1

    ans = 0
    now_x = 2 ** 40

    for i in range(41):
        kari = ans + now_x
        count = 0
        for i in range(len(a_list)):
            if a_list[i] & kari == kari:
                count += 1

        if count >= k:
            ans = kari
        now_x //= 2

    print(ans)



if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
NDIGIT=50
N,K=map(int,input().split())
A=list(map(int,input().split()))
acc=[0]*(N+1)
tmp=0
for i in range(N):
    tmp+=A[i]
    acc[i+1]=tmp

nums=[]
for i in range(N):
    for j in range(i+1,N+1):
        n=acc[j]-acc[i]
        s=bin(n)[2:]
        s="0"*(NDIGIT-len(s))+s
        nums.append(s)
        
ans=[]
for i in range(NDIGIT):
    tmp = [s for s in nums if s[i]=="1"]
    if len(tmp)>=K:
        ans.append("1")
        nums=tmp
    else:
        ans.append("0")

print(int("".join(ans),2))

Submission status: Accepted

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

sums = []
for i in range(n):
    b = a[i]
    sums.append(b)
    for j in range(i+1,n):
        b+=a[j]
        sums.append(b)
sums.sort()
b = [format(x,'040b') for x in sums]

for i in range(40):
    one = []
    for j in range(len(b)):
        if b[j][i]=='1':
            one.append(b[j])
    if len(one)>=k:
        b = []+one

ans = int(b[0],2)
for i in range(1,len(b)):
    ans = ans&int(b[i],2)
print(ans)

Submission status: Accepted

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

cumA = [0] * (N + 1)
for i in range(1, N + 1):
    cumA[i] = cumA[i - 1] + A[i - 1]

beauty = []
for left in range(N):
    for right in range(left + 1, N + 1):
        b = cumA[right] - cumA[left]
        beauty.append(b)

ans = 0
M = max(beauty).bit_length()

for digit in range(M)[:: -1]:
    new = ans + (1 << digit)
    cnt = 0
    for b in beauty:
        if b & new == new:
            cnt += 1
    if cnt >= K:
        ans = new

print(ans)

Submission status: Accepted

Solution code:
import sys
stdin = sys.stdin

sys.setrecursionlimit(10 ** 7)

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x) - 1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

n, k = li()
a = list(li())

cum = [0]
for ai in a:
    cum.append(cum[-1] + ai)

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

MAX_BIT = 41
ans = 0
for bit in range(MAX_BIT, -1, -1):
    new_pool = []
    for poli in pool:
        if poli & (1<<bit):
            new_pool.append(poli)

    if len(new_pool) >= k:
        ans += (1<<bit)
        pool = new_pool

print(ans)

Submission status: Accepted

Solution code:
n,k = map(int, input().split())
a = list(map(int, input().split()))
b = []
for i in range(n):
    for j in range(i, n):
        b.append(sum(a[i:j+1]))
assert(len(b) == n*(n+1)//2)

b.sort(reverse=True)
m = max(b).bit_length()
mask = 0
for i in range(m-1, -1, -1):
    mask += 1<<i
    cnt = 0
    for j in b:
        if j < mask:
            break
        if j&mask == mask:
            cnt += 1
    if cnt < k:
        mask -= 1<<i
print(mask)

Submission status: Accepted

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

B = []
for l in range(N):
    b = 0
    for r in range(l, N):
        b += A[r]
        B.append(b)
B.sort(reverse=True)
#print(B)

def trans2(n):
    A = bin(n)
    return str(A[2:])

C = []
maxl = 0
for i, b in enumerate(B):
    l = len(trans2(b))
    if i+1 <= K:
        maxl = l
    if i+1 > K and maxl != l:
        break
    C.append(trans2(b))
#print(C)
OK = [1 for _ in range(len(C))]

ans = 0
for l in range(maxl-1, -1, -1):
    update = []
    for i, c in enumerate(C):
        if c[-l-1] == '0' and OK[i] == 1:
            update.append(i)
            OK[i] = 0
    oks = sum(OK)
    if oks < K:
        for u in update:
            OK[u] = 1
    else:
        ans += 2**l
print(ans)

Submission status: Accepted

Solution code:
import os
import sys

import numpy as np

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(2147483647)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7

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

A = np.array(A, dtype=int)

cs = A.cumsum()
nums = list(cs)
for i in range(1, N):
    nums.extend(cs[i:] - cs[i - 1])
nums.sort(reverse=True)
nums = np.array(nums, dtype=int)

ans = 0
d = int(nums[K - 1]).bit_length() - 1
while d >= 0:
    mask = 1 << d
    idx = (nums & mask).nonzero()[0]
    if len(idx) >= K:
        nums = nums[idx]
        ans += mask
    d -= 1
print(ans)

Submission status: Accepted

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


li = []
for i in range(n):
    num = 0
    for j in range(i, n):
        num += a[j]
        li.append(num)

ans = 0
for i in range(40)[::-1]:
    bit = 2 ** i
    new_li = [num for num in li if num&bit != 0]
    if len(new_li) >= k:
        li = new_li[0:]
        ans += bit
print(ans)

Submission status: Accepted

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

ruiseki = [0] * (n + 1)
for i in range(n):
    ruiseki[i+1] = ruiseki[i] + a[i]

li = []
for i in range(n):
    for j in range(i, n):
        tmp = format(ruiseki[j + 1] - ruiseki[i], '040b')
        li.append(tmp)

for i in range(40):
    cnt = 0
    new_li = []
    for j in range(len(li)):
        if li[j][i] == "1":
            new_li.append(li[j])
            cnt += 1
    if cnt >= k:
        li = new_li[0:]

ans = 2**41 -1
for i in range(k):
    ans = ans & int(li[i], 2)
print(ans)

Submission status: Accepted

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

ruiseki = [0] * (n + 1)
for i in range(n):
    ruiseki[i+1] = ruiseki[i] + a[i]

li = []
for i in range(n):
    for j in range(i, n):
        tmp = format(ruiseki[j + 1] - ruiseki[i], '050b')
        li.append(tmp)

for i in range(50):
    cnt = 0
    new_li = []
    for j in range(len(li)):
        if li[j][i] == "1":
            new_li.append(li[j])
            cnt += 1
    if cnt >= k:
        li = new_li[0:]

ans = 2**50 -1
for i in range(k):
    ans = ans & int(li[i], 2)
print(ans)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
l=[]
for i in range(n):
    c=0
    for j in a[i:]:
        c+=j
        l.append(c)
t=0
for i in range(40,-1,-1):
    s=2**i
    b=[u for u in l if s&u!=0]
    if len(b)>=k:
        l=b
        t+=s
print(t)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

import numpy as np

N,K = map(int,input().split())
A = np.array(input().split(), dtype=np.int64)

Acum = A.cumsum()
range_sums = Acum[:,None] - Acum[None,:] + A[None,:]

range_sums = range_sums.flatten()
range_sums = range_sums[range_sums>0]

def test(x):
    # xを部分集合に持つ値がK個以上ある
    return np.sum(range_sums & x == x) >= K

left = 0 # leftを部分集合に持つものがK個以上ある
right = 1<<40 # rightを部分集合に持つものはK個未満
while left+1 < right:
    mid = (left+right)//2
    if test(mid):
        left = mid
    else:
        right = mid
answer = left
print(answer)

Submission status: Accepted

Solution code:
from heapq import heappush, heappop
from collections import deque
import itertools
from itertools import permutations
import sys

import bisect
sys.setrecursionlimit(10**6)
def SI():
    return input().split()
def MI():
    return map(int,input().split())
def I():
    return int(input())
def LI():
    return [int(i) for i in input().split()]
YN=['Yes','No']
YNeos='YNeos'
GYN=['Yes','trumpet']
mo=10**9+7
imp='IMPOSSIBLE'

n,k=MI()
a=LI()
A=[0]
for i in a:
    A+=[A[-1]+i]
b=[]
for i in range(n+1):
    for j in range(i+1,n+1):
        b+=[A[j]-A[i]]
        
c=sorted(b)#[-k:]
ans=0
i=0
i2=2**40
while i2>0:
#    print(i2,sum([(j&(ans+i2))==(ans+i2) for j in c]))
    ans+=i2 if sum([(j&(ans+i2))==(ans+i2) for j in c])>=k else 0
    i2//=2
    i+=1
    
print(ans)#,A,c)
#print()

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
#第5回　ドワンゴからの挑戦状予選 B

import sys
import math
import bisect
sys.setrecursionlimit(1000000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

n,k = LI()
a = LI()
ans = 0
s = [0] + list(accumulate(a))
lst = []
for i in range(n):
    for j in range(i+1,n+1):
        lst.append(s[j] - s[i])
ans = 0
for b in range(40)[::-1]:
    num = sum([(i>>b)&1 for i in lst])
    if num >= k:
        ans += 1 << b
        lst = [i for i in lst if (i>>b) & 1]
print(ans)

Submission status: Accepted

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

# 累積和
vC = [0,]
for a in vA:
    vC.append(a + vC[-1])

# 部分列の和 (美しさ)
vB = []
for i in range(len(vC)):
    vB.extend([vC[i+j+1] - vC[i] for j in range(N-i)])

# 最上位bitが何桁目かを取得
blenB = max(vB).bit_length()

# K個に満たない上位bitは最終的に残らないので除去する
# 上からK個以上あるものを決めていく
x = 0
for i in reversed(range(blenB+1)):
    x_new = x + (1<<i)
    if sum((x_new & b) == x_new for b in vB) >= K:
        x = x_new

print(x)

Submission status: Accepted

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

from itertools import accumulate
l=[]
for i in range(n):
    l+=list(accumulate(a[i:]))

ans=0
for i in range(40,-1,-1):
    j=2**i
    temp=[k for k in l if k&j!=0]
    if len(temp)>=k:
        l=temp
        ans+=j
        
print(ans)

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
a = list(map(int,input().split()))
A = []
for l in range(N):
    ans = 0
    for r in range(l,N):
        ans += a[r]
        A.append(ans)
bi = 0
while 2**bi <= max(A):
    bi += 1
bi -= 1

def funk(x,L):
    if x == -1:
        return L
    M = []
    b = 2**x
    for i in L:
        if i&b == b:
            M.append(i)
    if len(M) >= K:
        return funk(x-1,M)
    else:
        return funk(x-1,L)

B = funk(bi,A)
ans = B[0]
for i in B[:K]:
    ans &= i
print(ans)

Submission status: Accepted

Solution code:
import sys
from collections import deque, defaultdict
import copy
input=sys.stdin.readline
sys.setrecursionlimit(10 ** 9)

N, K = list(map(int, input().split()))
a = list(map(int, input().split()))
sum_a = [0, a[0]]
for i in range(1, N):
    sum_a.append(sum_a[-1]+a[i])

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

max_bit = 2
while True:
    if sum_a[-1] < 2**max_bit:
        break
    max_bit += 1

sum = 0
for i in range(max_bit ,-1, -1):
    kouho = []
    for n in bubunwa:
        if n & 2**i == 2**i:
            kouho.append(n)
    if len(kouho) >= K:
        bubunwa = copy.deepcopy(kouho)
        sum += 2**i

print(sum)

Submission status: Accepted

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

sum_list=[]
for i in range(N):
  s=0
  for j in range(i,N):
    s+=alist[j]
    sum_list.append(s)
    
sum_list.sort(reverse=True)
#print(sum_list)

answer=0
for i in reversed(range(41)):
  x=2**i
  cnt=0
  for s in sum_list:
    if answer+x>s:
      break
    elif (answer+x)&s==answer+x:
      cnt+=1
      if cnt>=K:
        answer+=x
        break
  
print(answer)

Submission status: Accepted

Solution code:
from itertools import accumulate

N, K = map(int, input().split())
*A, = map(int, input().split())
cumsum = [0] + list(accumulate(A))

def beauty(l, r): return cumsum[r+1] - cumsum[l]

beauties = [beauty(i, j) for i in range(N) for j in range(i, N)]
ans = ''
for i in reversed(range(len(format(max(beauties), 'b')))):
    temp = [b for b in beauties if b & (1 << i)]
    if len(temp) >= K:
        ans += '1'
        beauties = temp
    else:
        ans += '0'
print(int(ans, 2))

Submission status: Accepted

Solution code:
def count(value, P):
    cnt = 0
    for p in P:
        if p & value == value:
            cnt += 1
    return cnt


def main():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))
    P = []
    for i in range(N):
        s = 0
        for j in range(i, N):
            s += A[j]
            P.append(s)
    retval = 0
    for idx in range(40, -1, -1):
        tmp = retval + 2**idx
        if count(tmp, P) >= K:
            retval = tmp
    print(retval)


if __name__ == "__main__":
    main()