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:
n, k = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]

B = [0]

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

C = []

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

ans = 0
flag = False
D = []

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

Submission status: Accepted

Solution code:
N,K = map(int,input().split())
A = list(map(int,input().split()))
cums = [0]
for a in A:
    cums.append(cums[-1] + a)

vs = []
for l in range(N):
    for r in range(l+1,N+1):
        vs.append(cums[r] - cums[l])
vs.sort(reverse=True)

for i in range(100,-1,-1):
    tmps = []
    for v in vs:
        if v&(1<<i):
            tmps.append(v)
    if len(tmps) >= K:
        vs = tmps

if len(vs) < K:
    print(0)
else:
    ans = vs[0]
    for v in vs[1:K]:
        ans &= v
    print(ans)

Submission status: Accepted

Solution code:
import sys
n,k = map(int, sys.stdin.readline().split())
a = list(map(int, sys.stdin.readline().split()))
b = []
for i in range(n):
  for j in range(n):
    b.append(sum(a[i:j+1]))
    if b[-1] == 0:
      b.pop()
b.sort()
res = 0
for i in range(50,-1,-1):
  x = 1<<i
  ans = []
  for j in range(len(b)):
    if x&b[j]:
      ans.append(j)
  if len(ans) >= k:
    res += 1<<i
    c = []
    for l in ans:
      c.append(b[l])
    b = c
print(res)

Submission status: Accepted

Solution code:
# B
N, K = map(int, input().split())
a_list = list(map(int, input().split()))
NN = N*(N+1) // 2

arr = [0] * NN
res = 0

# bruce
ind = 0
for i in range(N):
    r = 0
    for j in range(i, N):
        r += a_list[j]
        arr[ind] = r
        ind += 1
        
# get Greedy
for m in range(40, -1, -1):
    cnt = 0
    for i in range(NN):
        if (arr[i] // (2**m)) % 2 == 1:
            cnt += 1
    if cnt >= K:
        res += 2**m
        for i in range(NN):
            if (arr[i] // (2**m)) % 2 == 0:
                arr[i] = 0

print(res)

Submission status: Accepted

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

asum = [0]
for i in range(n):
    asum.append(asum[i] + aa[i])

def getsum(l, r):
    return asum[r + 1] - asum[l]

active = []
for l in range(n):
    for r in range(l, n):
        active.append(getsum(l, r))

ans = 0
for i in range(40, -1, -1):
    ans *= 2
    bit = 1 << i
    cnt = 0
    nxt = []
    for num in active:
        if num & bit:
            cnt += 1
            nxt.append(num)
    if cnt >= k:
        ans += 1
        active = nxt

print(ans)

Submission status: Accepted

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

a = np.array(a)

s = 0
e = 0

arr = []
aa = np.append(0,np.cumsum(a))
for s in range(len(aa)):
    for e in range(s+1, len(aa)):
        arr.append(aa[e] - aa[s])

max_len = len(bin(np.sum(a))[2:])

su = 0
b = [bin(aa)[2:] for aa in arr]
b = ['0' * (max_len - len(bb)) + bb for bb in b]
while True:
    c = [bb for bb in b if bb[0] == '1']
    if len(c) >= K:
        su += 2 ** (len(c[0]) - 1)
        b = [cc[1:] for cc in c]
    else:
        b = [bb[1:] for bb in b]
    if len(b[0]) == 0:
        break
print(su)

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**13
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    n,k = LI()
    a = LI()
    ii = [2**i for i in range(41)]
    u = [a[0]]
    for c in a[1:]:
        u.append(c+u[-1])
    for i in range(n-1):
        for j in range(i+1,n):
            u.append(u[j]-u[i])

    r = 0
    for t in ii[::-1]:
        b = [c for c in u if c >= t]
        if len(b) >= k:
            r += t
            u = [c - t for c in b]
        else:
            u = [c - (c & t) for c in u]

    return r


print(main())

Submission status: Accepted

Solution code:

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

    accA = [0] + list(accumulate(A))
    Bin = []

    for i in range(N + 1):
        for j in range(i + 1, N + 1):
            par_sum = accA[j] - accA[i]
            b = bin(par_sum)
            b = b[2:].zfill(40)
            Bin.append(b)

    ans = 0
    for n in range(40):
        sifted_B = [b for b in Bin if b[n] == "1"]
        if len(sifted_B) >= K:
            ans += 2 ** (39 - n)
            Bin = sifted_B

    print(ans)

    
main()

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
x=[0,a[0]]
for i in range(1,n):
    x.append(x[-1]+a[i])
y=[]
for i in range(n+1):
    for j in range(i+1,n+1):
        y.append(x[j]-x[i])
y.sort()
y=y[::-1]
m=len(bin(y[0]))
y=["0"*(m-len(bin(y[i])))+bin(y[i])[2::] for i in range(len(y))]
res=["0" for _ in range(m-2)]
for x in range(m-2):
    t=0
    z=[]
    for i in range(len(y)):
        if y[i][x]=="1":
            t+=1
            z.append(y[i])
    if t>=k:
        res[x]="1"
        y=z[::]
print(int("".join(res),2))

Submission status: Accepted

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

L = []
for i in range(N):
    v = A[i]
    L.append(v)
    for j in range(i+1, N):
        v += A[j]
        L.append(v)
ans = 0
for i in range(64):
    b = 1 << (63-i)
    c = 0
    R = []
    for j in range(len(L)):
        if b & L[j]:
            R.append(L[j])
    if len(R) >= K:
        L = R
        ans |= b
print(ans)

Submission status: Accepted

Solution code:
from bisect import bisect_left
def main():
    n, k = map(int, raw_input().split())
    a = map(int, raw_input().split())
    s = [0] * (n + 1)
    for i, x in enumerate(a):
        s[i+1] = s[i] + x
    b = [s[i] - s[j] for i in xrange(1, n + 1) for j in xrange(i)]
    b.sort()
    l, r = 0, len(b)
    y = z = 0
    for i in xrange(39, -1, -1):
        x = z + (1 << i)
        p = bisect_left(b, x, l, r)
        if r - p >= k:
            z = x
            y += 1 << i
            l = p
        else:
            b = [((1 << i) - 1) & b[j] for j in xrange(l, r)]
            b.sort()
            l, r = 0, len(b)
            z = 0
    print y
main()

Submission status: Accepted

Solution code:
import sys
stdin = sys.stdin

sys.setrecursionlimit(10**5)

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

from itertools import accumulate

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

a_cum = [0] + list(accumulate(a))
sm_lst = []
for i in range(n+1):
    for j in range(i+1,n+1):
        sm_lst.append(a_cum[j] - a_cum[i])
        
pop = sm_lst
ans = 0
for bit in range(40,-1,-1):
    yes=[]
    no=[]
    
    for s in pop:
        if s & (1<<bit):
            yes.append(s)
        else:
            no.append(s)
            
    if len(yes) >= k:
        ans += (1<<bit)
        pop = yes
        

print(ans)

Submission status: Runtime Error

Solution code:
def dwacon5th_prelims_b():
    n, k = (int(x) for x in input().split())
    A = [int(x) for x in input().split()]
    B = []

    for i in range(n):
        cum = 0
        for a in A[i:]:
            cum += a
            B.append(cum)
    B.sort(reverse=True)

    bit = [0]*30
    cand = B
    for i in range(30, -1, -1):
        nx = []
        for b in cand:
            if (b >> i) & 1: nx.append(b)
        if len(nx) >= k:
            cand = nx
            bit[i] = 1

    ans = int(''.join([str(x) for x in reversed(bit)]), base=2)
    print(ans)

if __name__ == '__main__':
   dwacon5th_prelims_b()

Submission status: Runtime Error

Solution code:
from typing import List, Dict

INF = 10**9+1


def read_int() -> int:
    return int(input().strip())


def read_ints() -> List[int]:
    return list(map(int, input().strip().split(' ')))

def remove_if_gt(a, b):
    if a >= b:
        return a-b
    return a

def solve() -> int:
    N, K = read_ints()
    A = read_ints()
    S = []
    for i in range(N):
        prefix = 0
        for j in range(i, N):
            prefix += A[j]
            S.append(prefix)
    S.sort(reverse=True)
    highest_bit = 40
    answer = 0
    while highest_bit >= 0:
        while 1<<highest_bit > S[0]:
            highest_bit -= 1
            if highest_bit < 0:
                return answer
        highest_bit_val = 1<<highest_bit
        count = 0
        for i in range(N):
            if S[i]&highest_bit_val != 0:
                count += 1
            else:
                break
        if count < K:
            S = [remove_if_gt(s, highest_bit_val) for s in S]
        else:
            answer += highest_bit_val
            S = [remove_if_gt(s, highest_bit_val) for s in S if s >= highest_bit_val]
        S.sort(reverse=True)
    return answer


if __name__ == '__main__':
    print(solve())

Submission status: Runtime Error

Solution code:
from typing import List, Dict

INF = 10**9+1


def read_int() -> int:
    return int(input().strip())


def read_ints() -> List[int]:
    return list(map(int, input().strip().split(' ')))

def remove_if_gt(a, b):
    if a >= b:
        return a-b
    return a

def solve() -> int:
    N, K = read_ints()
    A = read_ints()
    S = []
    for i in range(N):
        prefix = 0
        for j in range(i, N):
            prefix += A[j]
            S.append(prefix)
    S.sort(reverse=True)
    highest_bit = 0
    while 1<<(highest_bit+1) <= S[0]:
        highest_bit += 1
    answer = 0
    while highest_bit >= 0:
        highest_bit_val = 1<<highest_bit
        count = 0
        for i in range(N):
            if S[i]&highest_bit_val != 0:
                count += 1
            else:
                break
        if count < K:
            S = [remove_if_gt(s, highest_bit_val) for s in S]
        else:
            answer += highest_bit_val
            S = [remove_if_gt(s, highest_bit_val) for s in S if s >= highest_bit_val]
        S.sort(reverse=True)
        highest_bit -= 1
    return answer


if __name__ == '__main__':
    print(solve())

Submission status: Runtime Error

Solution code:
ma = lambda :map(int,input().split())
lma = lambda :list(map(int,input().split()))
tma = lambda :tuple(map(int,input().split()))
ni = lambda:int(input())
yn = lambda fl:print("Yes") if fl else print("No")
import collections
import math
import itertools
import heapq as hq

n,k = ma()
A = lma()
bu = []
for i in range(n):
    tmp=0
    for j in range(i,n):
        tmp+=A[j]
        bu.append(tmp)
mb = bin(max(bu))
ans = 0
for i in range(len(mb)-3,-1,-1):
    tmp=[]
    for b in bu:
        if (b>>i &1):
            tmp.append(b)
    if len(tmp2)>=k:
        ans+=2**i
        bu=tmp
    else:
        pass
print(ans)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
stand_bit = [set() for i in range(42)]
acc = [0 for i in range(n+1)]
acc[0] = 0
for i in range(1, n+1):
    acc[i] += a[i-1] + acc[i-1]
set_ans = set()
for i in range(n):
    for j in range(i, n):
        set_ans.add(acc[j+1] - acc[i])
        s = acc[j+1] - acc[i]
        index = 41
        while s :
            if s & 1:
                stand_bit[index].add(acc[j+1] - acc[i])
            s = s >> 1
            index -= 1
for i in range(42):
    if len(stand_bit[i]) >= k:
        set_ans &= stand_bit[i]
ans_list = list(set_ans)
ans = ans_list[0]
for i in ans_list:
    ans &= i
print(ans)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
stand_bit = [set() for i in range(42)]
acc = [0 for i in range(n+1)]
acc[0] = 0
for i in range(1, n+1):
    acc[i] += a[i-1] + acc[i-1]
set_ans = set()
for i in range(n):
    for j in range(i, n):
        set_ans.add(acc[j+1] - acc[i])
        s = acc[j+1] - acc[i]
        index = 41
        while s :
            if s & 1:
                stand_bit[index].add(acc[j+1] - acc[i])
            s = s >> 1
            index -= 1
/*
for i in range(42):
    if len(set_ans & stand_bit[i]) >= k:
        set_ans &= stand_bit[i]
ans_list = list(set_ans)
ans = ans_list[0]
for i in ans_list:
    ans &= i
print(ans)
*/

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
stand_bit = [set() for i in range(42)]
acc = [0 for i in range(n+1)]
acc[0] = 0
for i in range(1, n+1):
    acc[i] += a[i-1] + acc[i-1]
set_ans = set()
for i in range(n):
    for j in range(i, n):
        set_ans.add(acc[j+1] - acc[i])
        s = "{:b}".format(acc[j+1] - acc[i])
        s = s.zfill(42)
        for num, bit in enumerate(s):
            if bit == "1":
                stand_bit[num].add(acc[j+1] - acc[i])
for i in range(64):
    if len(set_ans & stand_bit[i]) >= k:
        set_ans &= stand_bit[i]
ans_list = list(set_ans)
ans = ans_list[0]
for i in ans_list:
    ans &= i
print(ans)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
stand_bit = [set() for i in range(35)]
acc = [0 for i in range(n+1)]
acc[0] = 0
for i in range(1, n+1):
    acc[i] += a[i-1] + acc[i-1]
set_ans = set()
for i in range(n):
    for j in range(i, n):
        set_ans.add(acc[j+1] - acc[i])
        s = "{:b}".format(acc[j+1] - acc[i])
        s = s.zfill(35)
        for num, bit in enumerate(s):
            if bit == "1":
                stand_bit[num].add(acc[j+1] - acc[i])
for i in range(64):
    if len(set_ans & stand_bit[i]) >= k:
        set_ans &= stand_bit[i]
ans_list = list(set_ans)
ans = ans_list[0]
for i in ans_list:
    ans &= i
print(ans)

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
stand_bit = [set() for i in range(32)]
acc = [0 for i in range(n+1)]
acc[0] = 0
for i in range(1, n+1):
    acc[i] += a[i-1] + acc[i-1]
set_ans = set()
for i in range(n):
    for j in range(i, n):
        set_ans.add(acc[j+1] - acc[i])
        s = "{:b}".format(acc[j+1] - acc[i])
        s = s.zfill(32)
        for num, bit in enumerate(s):
            if bit == "1":
                stand_bit[num].add(acc[j+1] - acc[i])
for i in range(32):
    if len(set_ans & stand_bit[i]) >= k:
        set_ans &= stand_bit[i]
ans_list = list(set_ans)
ans = ans_list[0]
for i in ans_list:
    ans &= i
print(ans)

Submission status: Runtime Error

Solution code:
from itertools import accumulate

def main():
    int: n, k, i, j, ans, num, cnt, b
    list: a, btfl
    
    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: Runtime Error

Solution code:
from itertools import accumulate

def main():
    def int n, k, i, j, ans, num, cnt, b
    def list a, btfl
    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: Runtime Error

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]
beau=[]
for i in range(N+1):
    for j in range(i):
        beau.append(cum[i]-cum[j])
beau.sort()
bit=[0]*30
for x in beau:
    j=0
    while((x>>j)>0):
        bit[j]+=x>>j&1
        j+=1
ans=0
for i in range(len(beau)-K+1):
    res=1023
    for j in range(i,i+K):
        res&=beau[j]
    ans=max(ans,res)
print(ans)

Submission status: Runtime Error

Solution code:
def bit(b_k):
  point = 0
  count = 0
  while(True):
    for i in range(0:3):
      for j in range(i + 1, 4):
        while(count < b_k):
          if a_sum[i:j] >= 2 ** point:
            count += 1
      else:
        break
    else:
      break
      
n, k = map(int, input().split(" "))
a = list(map(int, input().split(" ")))
a_sum = []
for i in range(1, n + 1):
  a_sum.append(sum(a[0:i]))
print(a_sum)
n_count = 1
temp = 0
while(True):
  for i in range(0:3):
    for j in range(i + 1, 4):
      if a_sum[i:j] >= 2*n_count:

Submission status: Runtime Error

Solution code:
n, k = map(int, input().split())
a = list(map(int, input().split()))
array = [sum(a)] * k
s = 0
e = n - 1
for i in range(1, k):
    if a[s] > a[e]:
        array[i] -= a[e]
        e -= 1
    else:
        array[i] -= a[s]
        s -= 1
ans = array[0]
for i in range(k):
    ans &= array[i]
print(ans)

Submission status: Runtime Error

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

S = []
for l in range(0, N):
  temp = 0
  for r in range(l, N):
    temp += A[r]
    S.append(temp)

max_s = max(S)
n = max_s.bit_length()
S_temp = [S.copy(), []]
s_idx = 0
for i in range(n-1, -1, -1):
  c = 0
  for s in S_temp[s_idx]:
    if (s >> i) & 1:
      S_temp[s_idx-1].append(s)
      c += 1
  if c == K:
    ans = S_temp[s_idx-1][0]
    for _s in S_temp[s_idx-1]:
      ans &= _s
    print(ans)
    quit()
  if c > K:
    S_temp[s_idx] = []
    s_idx = not s_idx
  else:
    S_temp[s_idx-1] = []

ans = S_temp[s_idx][0]
for _s in S_temp[s_idx][:K]:
  ans &= _s

print(ans)

Submission status: Runtime Error

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

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

sum_list = []
for left in range(n):
    lrsum = 0
    for right in range(left,n):
        lrsum += a[right]
        sum_list.append(lrsum)

sum_list.sort(reverse=True)

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

import itertools
for i in range(k+1,len(sum_list)):
    if sum_list[i] < ans:
        print(ans)
        exit()

    for v in itertools.combinations(sum_list[i], k):
        for i,vv in enumerate(v):
            if i == 0: tmp = vv
            else: tmp = tmp & vv
        ans = max(ans,tmp)

print(ans)

Submission status: Runtime Error

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

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

sum_list = []
for left in range(n):
    lrsum = 0
    for right in range(left,n):
        lrsum += a[right]
        sum_list.append(lrsum)

sum_list.sort(reverse=True)

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

    ans &= sum_list[i]

import itertools
for i in range(k+1,len(sum_list)):
    if sum_list[i] < ans:
        print(ans)
        exit()

    for v in itertools.combinations(sum_list[i], k):
        for i,vv in enumerate(v):
            if i == 0: tmp = vv
            else: tmp = tmp & vv
        ans = max(ans,tmp)

print(ans)

Submission status: Runtime Error

Solution code:
import sys
from collections import *
import heapq
import math
# from math import gcd
import bisect
import copy
from itertools import permutations,accumulate,combinations,product
# def input():
#     return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
mod=pow(10,9)+7
al=[chr(ord('a') + i) for i in range(26)]
direction=[[1,0],[0,1],[-1,0],[0,-1]]

n,k=map(int,input().split())
a=list(map(int,input().split()))
rlst=ruiseki(a)
# print(rlst)
lst=[]
for i in range(n+1):
    for j in range(i+1,n+1):
        # print(i,j)
        lst.append(rlst[j]-rlst[i])
# print(lst)
tmp=math.log2(rlst[-1])
now=int(tmp)

while now>=0:
    anslst=[]
    for i in range(len(lst)):
        if 1&(lst[i]>>now)==1:
            anslst.append(lst[i])
    if len(anslst)>=k:
        lst=anslst
    now-=1
# print(lst)
ans=lst[0]
for i in range(k):
    ans=ans&lst[i]
print(ans)

Submission status: Runtime Error

Solution code:
import sys
from collections import *
import heapq
import math

import bisect
import copy
from itertools import permutations,accumulate,combinations,product
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
mod=pow(10,9)+7
al=[chr(ord('a') + i) for i in range(26)]
direction=[[1,0],[0,1],[-1,0],[0,-1]]

n,k=map(int,input().split())
a=list(map(int,input().split()))
rlst=ruiseki(a)
# print(rlst)
lst=[]
for i in range(n+1):
    for j in range(i+1,n+1):
        lst.append(rlst[j]-rlst[i])
# print(lst)
tmp=math.log2(rlst[-1])
now=int(tmp)

while now>=0:
    anslst=[]
    for i in range(len(lst)):
        if 1&(lst[i]>>now)==1:
            anslst.append(lst[i])
    if len(anslst)>=k:
        lst=anslst
    now-=1
# print(lst)
ans=lst[0]
for i in range(k):
    ans=ans&lst[i]
print(ans)

Submission status: Runtime Error

Solution code:
import sys
from collections import *
import heapq
import math
from math import gcd
import bisect
import copy
from itertools import permutations,accumulate,combinations,product
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
mod=pow(10,9)+7
al=[chr(ord('a') + i) for i in range(26)]
direction=[[1,0],[0,1],[-1,0],[0,-1]]

n,k=map(int,input().split())
a=list(map(int,input().split()))
rlst=ruiseki(a)
# print(rlst)
lst=[]
for i in range(n+1):
    for j in range(i+1,n+1):
        lst.append(rlst[j]-rlst[i])
# print(lst)
tmp=math.log2(rlst[-1])
now=int(tmp)

while now>=0:
    anslst=[]
    for i in range(len(lst)):
        if 1&(lst[i]>>now)==1:
            anslst.append(lst[i])
    if len(anslst)>=k:
        lst=anslst
    now-=1
# print(lst)
ans=lst[0]
for i in range(k):
    ans=ans&lst[i]
print(ans)

Submission status: Runtime Error

Solution code:
import copy
N, K = map(int,input().split())
a = list(map(int,input().split()))
# まず累積和で美しさを全列挙
S = [0]*N
S[0] = a[0]
for k in range(1,N):
    S[k] = S[k-1] + a[k]
S = [0] + S
T = []
for k in range(N):
    for l in range(k+1,N+1):
        T.append(S[l]-S[k])
U = []
C = [0]*32
for e in T:
    t = []
    for d in range(32,-1,-1):
        if e >= 2**d:
            t.append(1)
            C[d] += 1
            e -= 2**d
        else:
            t.append(0)
    U.append(t[::-1])
d = -1
for k in range(31,-1,-1):
    if C[k] >= K:
        d = k
        break
if d == -1:
    print(0)
    exit(0)
ans = 2**d
V = []
for e in U:
    if e[d] == 1:
        V.append(e[:d+1][::-1])

W = copy.deepcopy(V)
for k in range(1,d+1):
    n = []
    for e in W:
        if e[k] == 1:
            n.append(e)
    if len(n) >= K:
        ans += 2**(d-k)
        W = copy.deepcopy(n)
    else:
        break

print(ans)

Submission status: Runtime Error

Solution code:
import copy
import bisect
import math

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

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

ans = 0
s.sort()

while s[-1] != 0:
    n = int(math.log2(s[-1]))
    i = bisect.bisect_left(s, 2**n)
    update = False
    while len(s) - i < K - 1:
        n -= 1
        i = bisect.bisect_left(s, 2**n)
        update = True
    if not update:
        if len(s) - i == K - 1:
            temp = s[-1]
            for i in range(1, K):
                temp &= s[-i-1]
            print(ans + temp)
            exit()
        else:
            ans += 2**n
            s = [x & (2**n-1) for x in s[i:]]
    else:
        s = [x & (2**n-1) for x in s]
    s.sort()
print(ans)

Submission status: Runtime Error

Solution code:
nk = input().split()
seisuuretu = input().split()

#組み合わせのインデックス
all_seisuuretu = []
for start in range(len(seisuuretu) + 1):
    for end in range(len(seisuuretu) + 1):
        if start < end:
            all_seisuuretu.append(seisuuretu[start:end])

#美しさと最大の文字数
list_max = []
beautiful_list = []
for pattern in all_seisuuretu:
    temp_pattern = bin(sum(pattern))[2:]
    beautiful_list.append(temp_pattern)
    list_max.append(len(temp_pattern))
list_max = -max(list_max)

#文字数を揃える
for num in range(len(beautiful_list)):
    while len(beautiful_list[num]) < list_max:
        beautiful_list[num] = "0" + beautiful_list[num]

#int型に変換
for i in range(len(seisuuretu)):
    seisuuretu[i] = int(seisuuretu[i])

#インデックスを選択する
status = True
while status == True:
    temp = []
    count = 0
    len_all = 0
    for len_count in beautiful_list:
        len_all += len(len_count)
    if int(nk[1]) <= len_all:
        for i in beautiful_list:
            if int(i[list_max]) == 1:
                count += 1
                temp.append(i)
        if count > int(nk[1]):
            list_max += 1
            beautiful_list = temp
        elif count == int(nk[1]):
            beautiful_list = temp
            num_count = True
            for num in beautiful_list:
                if num_count == True:
                    ronriseki = int(num, 2)
                else:
                    ronriseki = ronriseki & int(num, 2)
                num_count = False
            print(ronriseki)
            status = False
        elif list_max == -1:
            num_count = True
            for num in beautiful_list:
                if num_count == True:
                    ronriseki = int(num, 2)
                else:
                    ronriseki = ronriseki & int(num, 2)
                num_count = False
            print(ronriseki)
            status = False
        else:
            list_max += 1
    else:
        list_max += 1

Submission status: Runtime Error

Solution code:
from itertools import *
from collections import *
from bisect import *


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

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


nlst=[]

for i in range(len(ruiA)-1):
    for m in range(i+1,len(ruiA)):
        nlst.append(ruiA[m]-ruiA[i])
nlst.sort(reverse=True)

lenbin=31
for i in range(len(nlst)):
    a=bin(nlst[i])[2:]
    nlst[i]=[0 for i in range(lenbin-len(a))]+list(map(int,list(a)))
    #print(nlst[i])
lenbinst=0
print(unko)
#print(nlst)
use=set([i for i in range(len(nlst))])
while lenbinst<lenbin:
    idx=-1
    count=0
    dellst=set()
    for m in use:
        if nlst[m][lenbinst]==1:
            count+=1
    if count>=K:
        if len(nlst)!=count:
            for m in use:
                if nlst[m][lenbinst]!=1:
                    dellst.add(m)
    use=use-dellst
    lenbinst+=1
valuelst=[0 for i in range(lenbin)]
nlst=[nlst[i] for i in use]

for n in nlst:
    for i,a in enumerate(list(reversed(n))):
        if a:
            valuelst[i]+=1

for i in range(lenbin):
    if valuelst[i]>=K:
        newlst=[i for i in nlst if i[idx]==1]
        if len(newlst)>=K:
            nlst=newlst

valuelst=[0 for i in range(lenbin)]
for n in nlst:
    for i,a in enumerate(list(reversed(n))):
        if a:
            valuelst[i]+=1
value=0
for i in range(lenbin):
    if valuelst[i]>=K:
        value+=2**i
print(value)

Submission status: Runtime Error

Solution code:
from itertools import *
from collections import *
from bisect import *


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

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


nlst=[]

for i in range(len(ruiA)-1):
    for m in range(i+1,len(ruiA)):
        nlst.append(ruiA[m]-ruiA[i])
nlst.sort(reverse=True)

lenbin=31
for i in range(len(nlst)):
    a=bin(nlst[i])[2:]
    nlst[i]=[0 for i in range(lenbin-len(a))]+list(map(int,list(a)))
    #print(nlst[i])
lenbinst=0

#print(nlst)
use=set([i for i in range(len(nlst))])
while lenbinst<lenbin:
    idx=-1
    count=0
    dellst=set()
    for m in use:
        if nlst[m][lenbinst]==1:
            count+=1
    if count>=K:
        if len(nlst)!=count:
            for m in use:
                if nlst[m][lenbinst]!=1:
                    dellst.add(m)
    use=use-dellst
    lenbinst+=1
valuelst=[0 for i in range(lenbin)]
nlst=[nlst[i] for i in use]

for n in nlst:
    for i,a in enumerate(list(reversed(n))):
        if a:
            valuelst[i]+=1

for i in range(lenbin):
    if valuelst[i]>=K:
        newlst=[i for i in nlst if i[idx]==1]
        if len(newlst)>=K:
            nlst=newlst

valuelst=[0 for i in range(lenbin)]
for n in nlst:
    for i,a in enumerate(list(reversed(n))):
        if a:
            valuelst[i]+=1
value=0
for i in range(lenbin):
    if valuelst[i]>=K:
        value+=2**i
print(value)