Problem:


Score : 400 points

Problem Statement

One day, Niwango-kun, an employee of Dwango Co., Ltd., found an integer sequence (a_1, ..., a_N) of length N.
He is interested in properties of the sequence a.

For a nonempty contiguous subsequence a_l, ..., a_r (1 \leq l \leq r \leq N) of the sequence a, its beauty is defined as a_l + ... + a_r. Niwango-kun wants to know the maximum possible value of the bitwise AND of the beauties of K nonempty contiguous subsequences among all N(N+1)/2 nonempty contiguous subsequences. (Subsequences may share elements.)

Find the maximum possible value for him.

Constraints

2 \leq N \leq 1000

1 \leq a_i \leq 10^9

1 \leq K \leq N(N+1)/2

All numbers given in input are integers

Input

Input is given from Standard Input in the following format:

N K
a_1 a_2 ... a_N

Output

Print the answer.

Sample Input 1

4 2
2 5 2 5

Sample Output 1

12

There are 10 nonempty contiguous subsequences of a. Let us enumerate them:

contiguous subsequences starting from the first element: \{2\}, \{2, 5\}, \{2, 5, 2\}, \{2, 5, 2, 5\}

contiguous subsequences starting from the second element: \{5\}, \{5, 2\}, \{5, 2, 5\}

contiguous subsequences starting from the third element: \{2\}, \{2, 5\}

contiguous subsequences starting from the fourth element: \{5\}

(Note that even if the elements of subsequences are equal, subsequences that have different starting indices are considered to be different.)

The maximum possible bitwise AND of the beauties of two different contiguous subsequences is 12.
This can be achieved by choosing \{5, 2, 5\} (with beauty 12) and \{2, 5, 2, 5\} (with beauty 14).

Sample Input 2

8 4
9 1 8 2 7 5 6 4

Sample Output 2

32



Sample input:
4 2
2 5 2 5


Sample output:
12

Submission status: Wrong Answer

Solution code:
# -*- 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(30)[::-1]:
  HERE = list(filter(lambda x: x>=(1<<k), SUMS)) 
  if len(HERE)>=K:
    ans += 1<<k
    SUMS = [i-(1<<k) for i in HERE]
print(ans)

Submission status: Wrong Answer

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(31)[::-1]:
  HERE = list(filter(lambda x: x>=(1<<k), SUMS)) 
  if len(HERE)>=K:
    ans += 1<<k
    SUMS = [i-(1<<k) for i in HERE]
  break
#print(ans)

Submission status: Wrong Answer

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(31)[::-1]:
#  HERE = list(filter(lambda x: x>=(1<<k), SUMS)) 
#  if len(HERE)>=K:
#    ans += 1<<k
#    SUMS = [i-(1<<k) for i in HERE]
#print(ans)

Submission status: Wrong Answer

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(31)[::-1]:
  HERE = list(filter(lambda x: x>=(1<<k), SUMS)) 
  if len(HERE)>=K:
    ans += 1<<k
    SUMS = [i-(1<<k) for i in HERE]
print(ans)

Submission status: Wrong Answer

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が愚直に間に合うので、これを
実装すれば良い。
"""
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)

ans = lst3[0]
for i in range(1,k):
    ans = ans&lst3[i]

print(ans)

Submission status: Wrong Answer

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が愚直に間に合うので、これを
実装すれば良い。
"""
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-1):
    for j in range(i+1,n):
        lst3.append(cumsumdif(i,j,lst2))

lst3.sort(reverse=True)

ans = lst3[0]
for i in range(1,k):
    ans = ans&lst3[i]

print(ans)

Submission status: Wrong Answer

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が愚直に間に合うので、これを
実装すれば良い。
"""
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-1):
    for j in range(i,n):
        lst3.append(cumsumdif(i,j,lst2))

lst3.sort(reverse=True)

ans = lst3[0]
for i in range(1,k):
    ans = ans&lst3[i]

print(ans)

Submission status: Wrong Answer

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(32, -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: Wrong Answer

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
    print(num,cnt)
    if cnt >= k:
        ans = num
print(ans)

Submission status: Wrong Answer

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]*31
for i in range(30,-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(31):
            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: Wrong Answer

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)
ans = (1<<31) -1

for i in l[:k]:
    ans &= i
print(ans)

Submission status: Wrong Answer

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(32)):
    li=[]
    for i in range(L):
        if 1<<j&A[i]:
            li.append(A[i])
    if len(li)<K:
        continue
    ans+=1<<j
    A=li
    L=len(A)
print(ans)

Submission status: Wrong Answer

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])
ans=0
from math import log,ceil
r=ceil(log(sum(p))+10**-3)
for i in range(r+1,-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: Wrong Answer

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=p[0]
for i in range(k-1):
	ans&=p[i+1]
print(ans)

Submission status: Wrong Answer

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)
            
    L.sort(reverse=True)
    print(L[K-1])
            

main()

Submission status: Wrong Answer

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

ans = int('1'*30, 2)
for beauty, v in sorted(cnt.items(), reverse=True):
    ans &= beauty
    k -= v
    if k <= 0:
        break
print(ans)

Submission status: Wrong Answer

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[0]
        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: Wrong Answer

Solution code:
import sys
import numpy as np
from collections import defaultdict

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 = [0] + list(map(int, input().split()))
    cumsum = np.cumsum(A)

    d = defaultdict(int)
    for i in range(N):
        for j in range(i + 1, N + 1):
            a = cumsum[j] - cumsum[i]
            d[a] += 1

    d = sorted(d.items(), key=lambda x: x[0], reverse=True)
    n = len(d)
    cnt = d[0][1]
    ans = d[0][0]
    for i in range(1, n):
        k, v = d[i]
        cnt += v
        ans &= k
        if cnt >= K:
            print(ans)
            return


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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(31)):
        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: Wrong Answer

Solution code:
def main():
    n, k = map(int, input().split())
    a = list(map(int, input().split()))
    t = [0]*31
    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])
    for i in reversed(range(31)):
        for j in range(len(b)):
            if (1<<i) & b[j] != 0:
                t[i] += 1
    ans = 0
    for i in reversed(range(31)):
        if t[i] < k:
            continue
        ans += (1<<i)
        t = [0]*31
        for j in range(i):
            for v in b:
                if ans & v == ans and (1<<j) & v != 0:
                    t[j] += 1
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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 = 20
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: Wrong Answer

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 = 20
b = [set() for _ in range(L)]
u = set(s)
ans = 0

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

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

print(ans)

Submission status: Wrong Answer

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: Wrong Answer

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 = len(bin(b.max())[2:])
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: Wrong Answer

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]))
a=0
ans=0
for i in range(1,43):
  cnt=0
  DD=[]
  for j in D:
    if (j>>(42-i))&1==1:
      cnt+=1
      DD.append(j)
  if a==0 and cnt>=K:
    ans+=2**(42-i)
    a=1
    D=DD
  elif cnt>=K:
    ans+=2**(42-i)
print(ans)