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

sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    n, k = map(int, input().split())
    A = list(map(int, input().split()))

    R = [0] + list(accumulate(A))
    subs = []
    for left in range(n):
        for right in range(left + 1, n + 1):
            subs.append(R[right] - R[left])

    res = 0
    for mask in reversed(range(40)):
        if len(subs) == 0:
            break
        tmp = []
        for i in range(len(subs)):
            if subs[i] & (1 << mask):
                tmp.append(subs[i])
        if len(tmp) >= k:
            res += 1 << mask
            subs = tmp
    print(res)


if __name__ == '__main__':
    resolve()

Submission status: Accepted

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

#############
# Libraries #
#############

import sys
input = sys.stdin.readline

import math
#from math import gcd
import bisect
import heapq
from collections import defaultdict
from collections import deque
from collections import Counter
from functools import lru_cache

#############
# Constants #
#############

MOD = 10**9+7
INF = float('inf')
AZ = "abcdefghijklmnopqrstuvwxyz"

#############
# Functions #
#############

######INPUT######
def I(): return int(input().strip())
def S(): return input().strip()
def IL(): return list(map(int,input().split()))
def SL(): return list(map(str,input().split()))
def ILs(n): return list(int(input()) for _ in range(n))
def SLs(n): return list(input().strip() for _ in range(n))
def ILL(n): return [list(map(int, input().split())) for _ in range(n)]
def SLL(n): return [list(map(str, input().split())) for _ in range(n)]

######OUTPUT######
def P(arg): print(arg); return
def Y(): print("Yes"); return
def N(): print("No"); return
def E(): exit()
def PE(arg): print(arg); exit()
def YE(): print("Yes"); exit()
def NE(): print("No"); exit()

#####Shorten#####
def DD(arg): return defaultdict(arg)

#####Inverse#####
def inv(n): return pow(n, MOD-2, MOD)

######Combination######
kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n): return kaijo_memo[n]
  if(len(kaijo_memo) == 0): kaijo_memo.append(1)
  while(len(kaijo_memo) <= n): kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n): return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0): gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n): gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if n == r: return 1
  if n < r or r < 0: return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret

######Factorization######
def factorization(n):
  arr = []
  temp = n
  for i in range(2, int(-(-n**0.5//1))+1):
    if temp%i==0:
      cnt=0
      while temp%i==0: 
        cnt+=1 
        temp //= i
      arr.append([i, cnt])
  if temp!=1: arr.append([temp, 1])
  if arr==[]: arr.append([n, 1])
  return arr

#####MakeDivisors######
def make_divisors(n):
  divisors = []
  for i in range(1, int(n**0.5)+1):
    if n % i == 0:
      divisors.append(i)
      if i != n // i: 
        divisors.append(n//i)
  return divisors

#####MakePrimes######
def make_primes(N):
  max = int(math.sqrt(N))
  seachList = [i for i in range(2,N+1)]
  primeNum = []
  while seachList[0] <= max:
    primeNum.append(seachList[0])
    tmp = seachList[0]
    seachList = [i for i in seachList if i % tmp != 0]
  primeNum.extend(seachList)
  return primeNum

#####GCD#####
def gcd(a, b):
    while b: a, b = b, a % b
    return a

#####LCM#####
def lcm(a, b):
    return a * b // gcd (a, b)

#####BitCount#####
def count_bit(n):
  count = 0
  while n:
    n &= n-1
    count += 1
  return count

#####ChangeBase#####
def base_10_to_n(X, n):
  if X//n: return base_10_to_n(X//n, n)+[X%n]
  return [X%n]

def base_n_to_10(X, n):
  return sum(int(str(X)[-i-1])*n**i for i in range(len(str(X))))

def base_10_to_n_without_0(X, n):
  X -= 1
  if X//n: return base_10_to_n_without_0(X//n, n)+[X%n]
  return [X%n]

#####IntLog#####
def int_log(n, a):
  count = 0
  while n>=a:
    n //= a
    count += 1
  return count

#############
# Main Code #
#############
def ACC1(l):
  N = len(l)
  ret = [0]*(N+1)
  for i in range(N):
    ret[i+1] = ret[i]+l[i]
  return ret

N,K = IL()
A = IL()
ACC = ACC1(A)
SUMS = []
for i in range(N+1):
  for j in range(i+1,N+1):
    SUMS.append(ACC[j]-ACC[i])
ans = 0

for k in range(int_log(max(SUMS),2)+1)[::-1]:
  HERE = list(filter(lambda x: (x>>k)&1, SUMS))
  if len(HERE)>=K:
    ans += 1<<k
    SUMS = HERE
print(ans)

Submission status: Accepted

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

#############
# Libraries #
#############

import sys
input = sys.stdin.readline

import math
#from math import gcd
import bisect
import heapq
from collections import defaultdict
from collections import deque
from collections import Counter
from functools import lru_cache

#############
# Constants #
#############

MOD = 10**9+7
INF = float('inf')
AZ = "abcdefghijklmnopqrstuvwxyz"

#############
# Functions #
#############

######INPUT######
def I(): return int(input().strip())
def S(): return input().strip()
def IL(): return list(map(int,input().split()))
def SL(): return list(map(str,input().split()))
def ILs(n): return list(int(input()) for _ in range(n))
def SLs(n): return list(input().strip() for _ in range(n))
def ILL(n): return [list(map(int, input().split())) for _ in range(n)]
def SLL(n): return [list(map(str, input().split())) for _ in range(n)]

######OUTPUT######
def P(arg): print(arg); return
def Y(): print("Yes"); return
def N(): print("No"); return
def E(): exit()
def PE(arg): print(arg); exit()
def YE(): print("Yes"); exit()
def NE(): print("No"); exit()

#####Shorten#####
def DD(arg): return defaultdict(arg)

#####Inverse#####
def inv(n): return pow(n, MOD-2, MOD)

######Combination######
kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n): return kaijo_memo[n]
  if(len(kaijo_memo) == 0): kaijo_memo.append(1)
  while(len(kaijo_memo) <= n): kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n): return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0): gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n): gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if n == r: return 1
  if n < r or r < 0: return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret

######Factorization######
def factorization(n):
  arr = []
  temp = n
  for i in range(2, int(-(-n**0.5//1))+1):
    if temp%i==0:
      cnt=0
      while temp%i==0: 
        cnt+=1 
        temp //= i
      arr.append([i, cnt])
  if temp!=1: arr.append([temp, 1])
  if arr==[]: arr.append([n, 1])
  return arr

#####MakeDivisors######
def make_divisors(n):
  divisors = []
  for i in range(1, int(n**0.5)+1):
    if n % i == 0:
      divisors.append(i)
      if i != n // i: 
        divisors.append(n//i)
  return divisors

#####MakePrimes######
def make_primes(N):
  max = int(math.sqrt(N))
  seachList = [i for i in range(2,N+1)]
  primeNum = []
  while seachList[0] <= max:
    primeNum.append(seachList[0])
    tmp = seachList[0]
    seachList = [i for i in seachList if i % tmp != 0]
  primeNum.extend(seachList)
  return primeNum

#####GCD#####
def gcd(a, b):
    while b: a, b = b, a % b
    return a

#####LCM#####
def lcm(a, b):
    return a * b // gcd (a, b)

#####BitCount#####
def count_bit(n):
  count = 0
  while n:
    n &= n-1
    count += 1
  return count

#####ChangeBase#####
def base_10_to_n(X, n):
  if X//n: return base_10_to_n(X//n, n)+[X%n]
  return [X%n]

def base_n_to_10(X, n):
  return sum(int(str(X)[-i-1])*n**i for i in range(len(str(X))))

def base_10_to_n_without_0(X, n):
  X -= 1
  if X//n: return base_10_to_n_without_0(X//n, n)+[X%n]
  return [X%n]

#####IntLog#####
def int_log(n, a):
  count = 0
  while n>=a:
    n //= a
    count += 1
  return count

#############
# Main Code #
#############
def ACC1(l):
  N = len(l)
  ret = [0]*(N+1)
  for i in range(N):
    ret[i+1] = ret[i]+l[i]
  return ret

N,K = IL()
A = IL()
ACC = ACC1(A)
SUMS = []
for i in range(N+1):
  for j in range(i+1,N+1):
    SUMS.append(ACC[j]-ACC[i])
ans = 0

for k in range(40)[::-1]:
  HERE = list(filter(lambda x: (x>>k)&1, SUMS))
  if len(HERE)>=K:
    ans += 1<<k
    SUMS = HERE
print(ans)

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/dwacon5th-prelims/tasks/dwacon5th_prelims_b

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

# 累積和，和の候補
cA = [0] * (N + 1)
for i in range(N):
    cA[i + 1] = cA[i] + A[i]
nums = []
for i in range(N + 1):
    for j in range(i + 1, N + 1):
        nums.append(cA[j] - cA[i])

# 解は和のandのmax
# 解xは，x ∧ Ai = x を満たすiが少なくともK個存在する

# 解の計算 2⁴⁰ ~ 2⁰
now = 0
for d in range(40, -1, -1):
    count = 0
    # 今の解(上位から構築)が成り立ち，考えている桁でK以上1が
    # 立てられるなら，この桁を採用して良い
    for s in nums:
        if s & (now | (1 << d)) == now | (1 << d):
            count += 1
    if count >= K:
        now = now | (1 << d)
print(now)

Submission status: Accepted

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

from itertools import accumulate
acc = [0]+list(accumulate(As))
dp = [0] * 50
mx = 0
sums = []
for i in range(n+1):
    for j in range(i+1,n+1):
        s = acc[j]-acc[i]
        sums.append(s)

# 最大の桁数
for d in range(50):
    if max(sums) & (1<<d):
        mx = max(mx, d)
        dp[d] += 1
mx = 0
for i in range(50):
    if dp[i] >= k:
        mx = max(mx,i)

# 計算
ans = 0
now = 0
for d in range(50,-1,-1):
    cnt = 0
    for s in sums:
        if s & (now+(1<<d)) == now+(1<<d):
            cnt += 1
    # print(d,cnt)
    if cnt >= k:
        now += 1<<d
print(now)

Submission status: Accepted

Solution code:
#第5回 ドワンゴからの挑戦状 予選-B Sum AND Subarrays
"""
数列Aが与えられる。
整数kが与えられる。
数列Aの空でない部分列からk個取る。
各部分列の合計を"美しさ"と言う。
この時、k個すべての"美しさ"のbit andを取った時、最大値がいくつになるか
求めよ。
n <= 1000, k <= n*(n+1)//2

解法：
まず直感的に、すべての要素の合計sum(lst1)を使うとして良い。
また、回答は必ずこれ未満の値となる。
更に、andを用いることから、上位桁のbit積を優先して良い。

となると、結局大きい値が上位桁のbitも多いことから、
和が大きい順にk個取った時の論理積ということになる。

N<1000という制約から、n*(n+1)//2が愚直に間に合うので、これを
実装すれば良い。

WA:
下位桁を優先したほうが良い場合が存在する。
解説：
下位桁を優先する、のではなく、上位桁を満たすようなk個を用意できなかった場合、
次に下位の桁を探すという解き方をする必要がある。つまり、上位桁を優先にするのは
変わらず、その桁のbitが1であるようなものをどんどん絞っていく形になる。
"""
import sys
readline = sys.stdin.buffer.readline
def even(n): return 1 if n%2==0 else 0
n,k = map(int,readline().split())
lst1 = list(map(int,readline().split()))

def cumsum(lst): #元のリストを保持
    res = lst[:]
    for i in range(1,len(res)):
        res[i] += res[i-1]
    return res
def cumsumdif(l,r,lst): #求めたい差のインデックスを入れる
    if l == 0:
        return lst[r]
    else:
        return lst[r] - lst[l-1]
lst2 = cumsum(lst1)

lst3 = []

for i in range(n):
    for j in range(i,n):
        lst3.append(cumsumdif(i,j,lst2))

lst3.sort(reverse=True)

for i in range(40,-1,-1): #まず最上位桁について決定
    ct = 0
    res = []
    for j in lst3:
        if j>>i&1:
            ct += 1
            res.append(j)
    if ct >= k:
        m = i
        ans = pow(2,m)
        break

for i in range(m-1,-1,-1): #次に、上位桁からその桁においてandが1になるようなものを優先して絞っていく
    ct = 0
    resres = []
    for j in res:
        if j>>i&1:
            ct += 1
            resres.append(j)
    if ct >= k:
        ans += pow(2,i)
        res = resres

print(ans)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
tmp=0
ca=[tmp]
for ai in a:
  tmp+=ai
  ca.append(tmp)
r=[]
for i in range(n):
  for j in range(i+1,n+1):
    r.append(ca[j]-ca[i])
x=0
s=set([])
for t in range(40,-1,-1):
  s=set([])
  tmp=0
  for ri in r:
    if (ri>>t)&1:
      tmp+=1
      s.add(ri)
  if tmp>=k:
    x+=pow(2,t)
    m=t
    break
for t in range(m-1,-1,-1):
  tmp=0
  ss=set([])
  for si in s:
    if (si>>t)&1:
      tmp+=1
      ss.add(si)
  if tmp>=k:
    x+=pow(2,t)
    s=ss
print(x)

Submission status: Accepted

Solution code:
def main():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))
    B = [0]
    t = 0
    C = []
    for i, a in enumerate(A):
        t += a
        C.extend(t - b for b in B)
        B.append(t)
    r = 0
    k = B[-1].bit_length()
    for i in reversed(range(k + 1)):
        t = 0
        for c in C:
            if c & r != r:
                continue
            if c & (1 << i):
                t += 1
        if t >= K:
            r |= 1 << i
    return r
        
print(main())

Submission status: Accepted

Solution code:
def main():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))
    B = []
    r = 0
    for i in range(N):
        r = A[i]
        B.append(r)
        for j in range(i+1, N):
            r += A[j]
            B.append(r)
    for i in range(40, -1, -1):
        n = []
        for k in B:
            if k & 2**i == 2**i:
                n.append(k)
        if len(n) >= K:
                break
    else:
        return 0
    c = n
    r = [i]
    for j in range(i-1, -1, -1):
        n = []
        for i in c:
            if i & 2**j == 2**j:
                n.append(i)
        if len(n) >= K:
            r.append(j)
            c = n
    return sum([2**i for i in r])
print(main())

Submission status: Accepted

Solution code:
from itertools import accumulate
n, k = map(int, input().split())
a = [0] + list(accumulate(map(int, input().split())))

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

# 解説AC
ans = 0
for i in range(50)[::-1]:
    num, cnt = ans + pow(2, i), 0
    for b in btfl:
        if num == num & b:
            cnt += 1
    if cnt >= k:
        ans = num
print(ans)

Submission status: Accepted

Solution code:
from itertools import accumulate

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

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

    # 解説AC
    ans = 0
    for i in range(50)[::-1]:
        num, cnt = ans + pow(2, i), 0
        for b in btfl:
            if num == num & b:
                cnt += 1
        if cnt >= k:
            ans = num
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

ans = 0
for i in range(44,-1,-1):
    x = 1<<i
    check = ans+x
    count = 0
    for j in l:
        if (check&j) == check:
            count += 1
    if count >= k:
        ans += x
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):
    count = 0
    for j in range(i,n):
        count += a[j]
        l.append(count)
l.sort(reverse=True)

le = 0
ri = 0

count = [0]*45
for i in range(44,-1,-1):
    x = 1<<i
    while ri < len(l) and l[ri] >= x:
        ri += 1
    
    for j in range(le,ri):
        for t in range(45):
            if l[j] >> t & 1:
                count[t] += 1
    
    if count[i] >= k:
        m = i
        break
    else:
        le = ri
    
check = []
for i in range(ri):
    if l[i] >> m & 1:
        check.append(l[i]%x)

ans = x
for i in range(m-1,-1,-1):
    count = 0
    for j in check:
        if j >> i & 1:
            count += 1
    if count >= k:
        x = 1<<i
        ans += x
        checkn = []
        for c in check:
            if c >> i & 1:
                checkn.append(c%x)
        check = checkn
print(ans)

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
a=list(map(int,input().split()))
cum=[0]*(N+1)
for i in range(N):
    cum[i+1]=cum[i]+a[i]
A=[]
for i in range(N+1):
    for j in range(i):
        A.append(cum[i]-cum[j])
L=len(A)
ans=0
for j in reversed(range(46)):
    li=[]
    for i in range(L):
        if A[i]>>j&1:
            li.append(A[i])
    if len(li)<K:
        continue
    ans+=1<<j
    A=li
    L=len(A)
print(ans)

Submission status: Accepted

Solution code:
N,K=map(int,input().split())
a=list(map(int,input().split()))
from collections import defaultdict
d = defaultdict(int)
l=[0]
t=0
for i in a:
    t+=i
    l.append(t)
for i in range(N):
    for j in range(i+1,N+1):
        d[l[j]-l[i]]+=1
s=0
for i in range(39,-1,-1):
    t=0
    for j in d.keys():
        if (j>>i)%2:
            t+=d[j]
    if t<K:
        continue
    s+=2**i
    l=[]
    for j in d.keys():
        if (j>>i)%2==0:
            l.append(j)
    for j in l:
        d.pop(j)
print(s)

Submission status: Accepted

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

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

ans = 0

for i in range(40, -1, -1):
    cnt = 0
    for l in L:
        if (ans + (1 << i)) & l == ans + (1 << i):
            cnt += 1
    if cnt >= K:
        ans += 1 << i

print(ans)

Submission status: Accepted

Solution code:
def main():
	n,k=map(int,input().split())
	a=[0]+list(map(int,input().split()))
	p=[]
	for i in range(n):
		a[i+1]+=a[i]
	for i in range(n+1):
		for j in range(i):
			p.append(a[i]-a[j])
	p.sort()
	ans=0
	from math import log,ceil
	r=ceil(log(sum(p))+10**-3)
	for i in range(r+2,-1,-1):
		cnt=0
		for j in range(len(p)):
			if (ans+2**i)&p[j]==ans+2**i:
				cnt+=1
				if cnt==k:
					break
		if cnt==k:
			ans+=2**i
	print(ans)
if __name__ == '__main__':
	main()

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
a=[0]+list(map(int,input().split()))
p=[]
for i in range(n):
	a[i+1]+=a[i]
for i in range(n+1):
	for j in range(i):
		p.append(a[i]-a[j])
p.sort(reverse=True)
ans=0
from math import log,ceil
r=ceil(log(sum(p))+10**-3)
for i in range(r+3,-1,-1):
	cnt=0
	for j in range(len(p)):
		if (ans+2**i)&p[j]==ans+2**i:
			cnt+=1
		if cnt==k:
			break
	if cnt==k:
		ans+=2**i
print(ans)

Submission status: Accepted

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

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7
    N,K=MI()
    a=LI()
    L=[]
    S=[0]*(N+1)
    
    for i in range(N):
        S[i+1]=S[i]+a[i]
        
    for i in range(N+1):
        for j in range(i+1,N+1):
            s=S[j]-S[i]
            L.append(s)
            
    x=0
    #答えがxになりうるか
    N2=43
    p=[1]*N2
    for i in range(N2-1):
        p[i+1]=p[i]*2
        
    for i in range(N2):
        temp=x+p[N2-1-i]
        cnt=0
        for a in L:
            if (a&temp)==temp:
                cnt+=1
        if cnt>=K:
            x=temp
            
    print(x)
            

main()

Submission status: Accepted

Solution code:
def main():
    N, K = map(int, input().split())
    A = [0] + list(map(int, input().split()))

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

    B = [A[j] - A[i] for i in range(N) for j in range(i + 1, N + 1)]

    ans = 0
    tmp = pow(2, 40)
    for _ in range(40):
        count = 0
        tmp //= 2
        tmp_ = ans + tmp
        for b in B:
            if ((b & (tmp_)) == (tmp_)):
                count += 1
            if count >= K:
                ans += tmp
                break

    print (ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import defaultdict
from itertools import accumulate
n, k = map(int, input().split())
A = list(map(int, input().split()))
Acum = [0]
Acum.extend(accumulate(A))

cnt = defaultdict(int)
for i in range(n):
    for j in range(i+1, n+1):
        beauty = Acum[j]-Acum[i]
        cnt[beauty] += 1


def check(x):
    res = 0
    for beauty, c in cnt.items():
        if x & beauty == x:
            res += c
    return k <= res


L = 0
R = 1 << 40
while L != R-1:
    M = (L+R)//2
    if check(M):
        L = M
    else:
        R = M
print(L)

Submission status: Accepted

Solution code:
from collections import defaultdict
from itertools import accumulate
n, k = map(int, input().split())
A = list(map(int, input().split()))
Acum = [0]
Acum.extend(accumulate(A))
cnt = defaultdict(int)
for i in range(n):
    for j in range(i+1, n+1):
        beauty = Acum[j]-Acum[i]
        cnt[beauty] += 1

x = 0
for t in reversed(range(41)):
    y = 1 << t
    cnt_k = 0
    for beauty, c in cnt.items():
        if (x+y) & beauty == (x+y):
            cnt_k += c
    if k <= cnt_k:
        x += y

print(x)

Submission status: Accepted

Solution code:
import sys
import copy

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


def input():
    return sys.stdin.readline().strip()


def main():
    N, K = map(int, input().split())
    A = list(map(int, input().split()))

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

    s.sort(reverse=True)
    M = s[0]
    ans = 0
    n = M.bit_length()
    for i in range(n):
        tmp = []
        for v in s:
            if 1 << (n - i - 1) & v:
                tmp.append(v)
        if len(tmp) >= K:
            ans += 1 << (n - i - 1)
            s = copy.deepcopy(tmp)

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import math
import numpy as np
import queue
from collections import deque,defaultdict
import heapq
from sys import stdin,setrecursionlimit
#from scipy.sparse.csgraph import dijkstra
#from scipy.sparse import csr_matrix
ipt = stdin.readline
setrecursionlimit(10**7)

def main():
    n,k = map(int,ipt().split())
    a = [int(i) for i in ipt().split()]
    aa = np.zeros(n*(n+1)//2,dtype=int)
    ni = 0
    for i in range(n):
        sa = 0
        for j in range(i,n):
            sa += a[j]
            aa[ni] = sa
            ni += 1

    ans = 0
    for i in range(40):
        if np.count_nonzero((ans+2**(39-i))&aa==(ans+2**(39-i))) >= k:
            ans += 2**(39-i)

    print(ans)
    return

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
    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(-1, n-1):
        for j in range(i+1, n):
            if i == -1:
                b.append(a[j])
            else:
                b.append(a[j]-a[i])
    ans = 0
    for i in reversed(range(51)):
        cnt = 0
        for v in b:
            if ans & v == ans and (1<<i) & v != 0:
                cnt += 1
        if k <= cnt:
            ans += (1<<i)
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Accepted

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

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

L = 40
b = [set() for _ in range(L)]
u = set(range(len(s)))
ans = 0

for i in range(L):
    for j, x in enumerate(s):
        if x >> i & 1:
            b[i].add(j)

for i in range(L)[::-1]:
    if len(u & b[i]) >= k:
        u &= b[i]
        ans += 2**i

print(ans)

Submission status: Accepted

Solution code:
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")



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

# 累積和
def cumsum(l):
    """a[i] = sum(l[:i]) なるaを返す
    """
    c = 0
    a = [None] * (len(l)+1)
    for i,num in enumerate(l):
        a[i] = c
        c += num
    a[-1] = c
    return a
c = cumsum(a)
l = 50
s = set()
for j in range(n+1):
    for i in range(j):
        s.add(bin(c[j]-c[i])[2:].rjust(l, "0"))
        
ans = 0
for i in range(l):
    count = 0
    tmp = set()
    for item in s:
        if item[i]=="0":
            pass
        else:
            count += 1
            tmp.add(item)
    if count>=k:
        ans+= 2**(l-i-1)
        s = tmp
print(ans)

Submission status: Accepted

Solution code:
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")

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

import numpy as np
c = np.cumsum(a)
b = c.reshape(-1,1) - c
l = int(np.log2(b.max())) + 1
s = set()
for i in range(1,n+1):
    for j in range(i):
        s.add(bin(b[i,j])[2:].rjust(l, "0"))
ans = 0
for i in range(l):
    count = 0
    tmp = set()
    for item in s:
        if item[i]=="0":
            pass
        else:
            count += 1
            tmp.add(item)
    if count>=k:
        ans+= 2**(l-i-1)
        s = tmp
print(ans)

Submission status: Accepted

Solution code:



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

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

B = []
for l in range(N):
    for r in range(l,N):
        if l == 0:
            B.append(accum[r])
        else:
            B.append(accum[r] - accum[l-1])


ans = 0
for i in range(45,-1,-1):
    used = []
    cnt = 0
    for b in B:
        if b & (1 << i):
            cnt += 1
            used.append(b)

    if cnt >= K:
        ans += pow(2,i)
        B = used


print(ans)

Submission status: Accepted

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

a_cum = [0 for _ in range(n+1)]
for i in range(n):
    a_cum[i+1] = a_cum[i] + a[i]

sum_list = []
for i in range(n+1):
    for j in range(i+1, n+1):
        sum_list.append(a_cum[j] - a_cum[i])
sum_list.sort()
#print(sum_list)
x = len(bin(a_cum[n])) - 2


def search(x, l):
    if x == 0:
        return 0
    m = len(l)
    next_l = []
    cnt = 0
    for i in range(m):
        if (l[i] >> (x-1)) & 1:
            cnt += 1
            next_l.append(l[i])
    if cnt >= k:
        return search(x-1, next_l) + 2 ** (x-1)
    else:
        return search(x-1, l)

print(search(x, sum_list))

Submission status: Accepted

Solution code:
N,K=map(int, input().split())
A=list(map(int, input().split()))
D=[]
for i in range(N):
  for j in range(i,N):
    D.append(sum(A[i:j+1]))
ans=0
k=42
for i in range(1,k+1):
  cnt=0
  DD=[]
  for j in D:
    if (j>>(k-i))&1==1:
      cnt+=1
      DD.append(j)
  if cnt>=K:
    ans+=2**(k-i)
    D=DD
  elif cnt>=K:
    ans+=2**(k-i)
print(ans)

Submission status: Accepted

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

accum = [0] * N
for i in range(N):
    if i == 0:
        accum[i] = A[i]
    else:
        accum[i] = accum[i-1] + A[i]



B = []
for l in range(N):
    for r in range(l, N):
        if l == 0:
            B.append(accum[r])
        else:
            B.append(accum[r] - accum[l-1])




ans = 0
for i in range(50, -1, -1):
    
    cnt = 0
    nxt_B = []
    for b in B:
        if b & (1 << i):
            cnt += 1
            nxt_B.append(b)

    if cnt >= K:
        ans += 2**i
        B = nxt_B

print(ans)

Submission status: Accepted

Solution code:




"""


第5回 ドワンゴからの挑戦状 予選　　B - Sum AND Subarrays

N<=1000より美しさの全列挙は可能
K個選んでbitごとの論理積の最大値

K個選ぶ方法はnCk(10**6?, K)とかなので、全探索は間に合わない

K個選んだもののうちの最大、を言い換えを考える。
bitごとの論理積をとるので、選ばれたK個の中で、bitが大きいものについて、K個すべてで1が立っていると嬉しい。
また、2**0 + ... + 2**(N-1) <= 2**N なので、大きいbitが一個立ってる方が、それより小さいもの全てが立っているより嬉しい。
→大きいbitから見ていって、各美しさでそのbitに1が立っているか調べて、K個以上立っていれば、その美しさの中からK個採用される
　この時いずれの美しさも、今見ているbitが立っているので、答えにそのbit分加える。
　で、次のbitをみていって～を繰り返す。
　最下位bitまで見ても、美しさがK個以上のこっているなら、そこから適当にK個選んだら同じ答えになるので気にしなくてよい

bitごとの論理積が大きくなるようにK個選ぶ　→　大きいbitから、そのbitが立っているものがK個以上ある物が選ばれる
の言いかえ
"""

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

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

beauty = []
for l in range(N):
    for r in range(l, N):
        if l == 0:
            beauty.append(accum[r])
        else:
            beauty.append(accum[r] - accum[l-1])


# 10**9 * 1000 から、だいたい2**40ぐらい
ans = 0
candidate = beauty
for i in range(40, -1, -1):
    nxt_candidate = []
    cnt = 0
    for b in candidate:
        if b & (1 << i):
            cnt += 1
            nxt_candidate.append(b)
    
    # iビット目が立っている個数がKに満たなければ、現在の美しさ集合について、より下位のビットを見る
    # K個以上あれば、iビット目が立ってない奴は選ばれないので除外できる
    if cnt >= K:
        ans += 2**i
        candidate = nxt_candidate

print(ans)

Submission status: Accepted

Solution code:




"""


第5回 ドワンゴからの挑戦状 予選　　B - Sum AND Subarrays

N<=1000より美しさの全列挙は可能
K個選んでbitごとの論理積の最大値

K個選ぶ方法はnCk(10**6?, K)とかなので、全探索は間に合わない

K個選んだもののうちの最大、を言い換えを考える。
bitごとの論理積をとるので、選ばれたK個の中で、bitが大きいものについて、K個すべてで1が立っていると嬉しい。
また、2**0 + ... + 2**(N-1) <= 2**N なので、大きいbitが一個立ってる方が、それより小さいもの全てが立っているより嬉しい。
→大きいbitから見ていって、各美しさでそのbitに1が立っているか調べて、K個以上立っていれば、その美しさの中からK個採用される
　この時いずれの美しさも、今見ているbitが立っているので、答えにそのbit分加える。
　で、次のbitをみていって～を繰り返す。
　最下位bitまで見ても、美しさがK個以上のこっているなら、そこから適当にK個選んだら同じ答えになるので気にしなくてよい

bitごとの論理積が大きくなるようにK個選ぶ　→　大きいbitから、そのbitが立っているものがK個以上ある物が選ばれる
の言いかえ
"""

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

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

beauty = []
for l in range(N):
    for r in range(l, N):
        if l == 0:
            beauty.append(accum[r])
        else:
            beauty.append(accum[r] - accum[l-1])


# 10**9 * 1000 から、だいたい2**40ぐらい
ans = 0
candidate = beauty
for i in range(40, -1, -1):
    nxt_candidate = []
    cnt = 0
    for b in candidate:
        if b & (1 << i):
            cnt += 1
            nxt_candidate.append(b)
    
    if cnt >= K:
        ans += 2**i
        candidate = nxt_candidate

print(ans)