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: Time Limit Exceeded

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
    t = 40
    tmp = pow(2, t)
    while t:
        count = 0
        tmp //= 2
        for b in B:
            if ((b & (tmp + ans)) == (tmp + ans)):
                count += 1
            if count >= K:
                ans += tmp
                break
        t -= 1

    print (ans)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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
    t = 40
    while t:
        count = 0
        tmp = pow(2, t -1)
        for b in B:
            if ((b & (tmp + ans)) == (tmp + ans)):
                count += 1
            if count >= K:
                ans += tmp
                break
        t -= 1

    print (ans)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
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
t = 40
while t:
    count = 0
    tmp = pow(2, t -1)
    for b in B:
        if ((b & (tmp + ans)) == (tmp + ans)):
            count += 1
    if count >= K:
        ans += tmp
    t -= 1

print (ans)

Submission status: Time Limit Exceeded

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: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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: Time Limit Exceeded

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])
ans = 0
for d in range(41,-1,-1):
    c = 0
    for e in T:
        if (ans+2**d)&e == ans+2**d:
            c += 1
    if c >= K:
        ans += 2**d
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n,k = map(int,input().split())
l = []
a = list(map(int,input().split()))
for i in range(len(a)):
  for j in range(len(a)-i):
    l.append(sum(a[j:j+i+1]))
m = len(bin(sum(a)))-2
x = 0
for i in range(m-1,-1,-1):
  c = 0
  cu = 2**i
  for j in l:
    if x+cu & j == x+cu:
      c += 1
    if c == k:
      x += cu
      break
print(x)

Submission status: Time Limit Exceeded

Solution code:
from itertools import accumulate,combinations
n,k = map(int,input().split())
a = list(map(int,input().split()))
acc = [0]+list(accumulate(a))
ls = []
for i,j in combinations(acc,2):
  ls.append(j-i)
x = max(ls).bit_length()
ans = 0
for i in range(x-1,-1,-1):
  cnt = 0
  for j in ls:
    if ans+2**i & j == ans+2**i:
      cnt += 1
  if cnt >= k:
    ans += 2**i
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
nk = input().split()
seisuuretu_2 = input().split()
nk1 = int(nk[1])
seisuuretu = [int(i) for i in seisuuretu_2]

list_max = set()
beautiful_list = []
for start in range(len(seisuuretu) + 1):
    for end in range(len(seisuuretu) + 1):
        if start < end:
            temp_pattern = bin(sum(seisuuretu[start:end]))[2:]
            beautiful_list.append(temp_pattern)
            list_max |= {len(temp_pattern),}

list_max = -max(list_max)

status = True
while status == True:
    temp = []
    count = 0
    len_all = 0
    for i in beautiful_list:
        try:
            if int(i[list_max]) == 1:
                count += 1
                temp.append(i)
        except:
            continue
    if count > nk1:
        list_max += 1
        beautiful_list = temp
    elif count == nk1:
        num_count = True
        for num in temp:
            if num_count == True:
                ronriseki = int(num, 2)
            else:
                ronriseki = ronriseki & int(num, 2)
            num_count = False
        print(ronriseki)
        sys.exit()
        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)
        sys.exit()
        status = False
    else:
        list_max += 1

Submission status: Time Limit Exceeded

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

seisuuretu = [int(i) for i in seisuuretu_2]

list_max = set()
beautiful_list = []
for start in range(len(seisuuretu) + 1):
    for end in range(len(seisuuretu) + 1):
        if start < end:
            temp_pattern = bin(sum(seisuuretu[start:end]))[2:]
            beautiful_list.append(temp_pattern)
            list_max |= {len(temp_pattern),}

list_max = -max(list_max)

status = True
while status == True:
    temp = []
    count = 0
    len_all = 0
    for i in beautiful_list:
        try:
            if int(i[list_max]) == 1:
                count += 1
                temp.append(i)
        except:
            continue
    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

Submission status: Time Limit Exceeded

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

for i in range(len(seisuuretu)):
    seisuuretu[i] = int(seisuuretu[i])

list_max = set()
beautiful_list = []
for start in range(len(seisuuretu) + 1):
    for end in range(len(seisuuretu) + 1):
        if start < end:
            temp_pattern = bin(sum(seisuuretu[start:end]))[2:]
            beautiful_list.append(temp_pattern)
            list_max |= {len(temp_pattern),}

list_max = -max(list_max)

status = True
while status == True:
    temp = []
    count = 0
    len_all = 0
    for i in beautiful_list:
        try:
            if int(i[list_max]) == 1:
                count += 1
                temp.append(i)
        except:
            continue
    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

Submission status: Time Limit Exceeded

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

for i in range(len(seisuuretu)):
    seisuuretu[i] = int(seisuuretu[i])

list_max = []
beautiful_list = []
for start in range(len(seisuuretu) + 1):
    for end in range(len(seisuuretu) + 1):
        if start < end:
            temp_pattern = bin(sum(seisuuretu[start:end]))[2:]
            beautiful_list.append(temp_pattern)
            list_max.append(len(temp_pattern))

list_max = -max(list_max)

status = True
while status == True:
    temp = []
    count = 0
    len_all = 0
    for i in beautiful_list:
        try:
            if int(i[list_max]) == 1:
                count += 1
                temp.append(i)
        except:
            continue
    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

Submission status: Time Limit Exceeded

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

for i in range(len(seisuuretu)):
    seisuuretu[i] = int(seisuuretu[i])

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

set_max = set()
beautiful_set = set()
beautiful_list = []
for pattern in all_seisuuretu:
    temp_pattern = bin(sum(pattern))[2:]
    beautiful_set |= {temp_pattern}
    set_max |= {len(temp_pattern)}

beautiful_list = list(beautiful_set)
list_max = -max(set_max)

status = True
while status == True:
    temp = []
    count = 0
    len_all = 0
    for i in beautiful_list:
        try:
            if int(i[list_max]) == 1:
                count += 1
                temp.append(i)
        except:
            continue
    if count > int(nk[1]):
        beautiful_list = temp
        list_max += 1
    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

Submission status: Time Limit Exceeded

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

for i in range(len(seisuuretu)):
    seisuuretu[i] = int(seisuuretu[i])

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)

status = True
while status == True:
    temp = []
    count = 0
    len_all = 0
    for i in beautiful_list:
        try:
            if int(i[list_max]) == 1:
                count += 1
                temp.append(i)
        except:
            continue
    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

Submission status: Time Limit Exceeded

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

for i in range(len(seisuuretu)):
    seisuuretu[i] = int(seisuuretu[i])

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)

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 / len(beautiful_list):
        for i in beautiful_list:
            try:
                if int(i[list_max]) == 1:
                    count += 1
                    temp.append(i)
            except:
                continue
        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: Time Limit Exceeded

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

for i in range(len(seisuuretu)):
    seisuuretu[i] = int(seisuuretu[i])

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)

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:
            try:
                if int(i[list_max]) == 1:
                    count += 1
                    temp.append(i)
            except:
                continue
        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: Time Limit Exceeded

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

for i in range(len(seisuuretu)):
    seisuuretu[i] = int(seisuuretu[i])

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)

status = True
while status == True:
    temp = []
    count = 0
    for i in beautiful_list:
        try:
            if int(i[list_max]) == 1:
                count += 1
                temp.append(i)
        except:
            continue
    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

Submission status: Time Limit Exceeded

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

for i in range(len(seisuuretu)):
    seisuuretu[i] = int(seisuuretu[i])

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

beautiful_list = []
for pattern in all_seisuuretu:
    beautiful_list.append(bin(sum(pattern))[2:])

list_max = []
for i in beautiful_list:
    list_max.append(len(i))
list_max = -max(list_max)

status = True
while status == True:
    temp = []
    count = 0
    for i in beautiful_list:
        try:
            if int(i[list_max]) == 1:
                count += 1
                temp.append(i)
        except:
            continue
    if count > int(nk[1]):
        list_max += 1
        beautiful_list = temp
    elif count == int(nk[1]):
        beautiful_list = temp
        for num in range(len(beautiful_list)):
            if num == 0:
                ronriseki = int(beautiful_list[num], 2)
            else:
                ronriseki = ronriseki & int(beautiful_list[num], 2)
        print(ronriseki)
        status = False
    elif list_max == -1:
        for num in range(len(beautiful_list)):
            if num == 0:
                ronriseki = int(beautiful_list[num], 2)
            else:
                ronriseki = ronriseki & int(beautiful_list[num], 2)
        print(ronriseki)
        status = False
    else:
        list_max += 1

Submission status: Time Limit Exceeded

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

for i in range(len(seisuuretu)):
    seisuuretu[i] = int(seisuuretu[i])

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

beautiful_list = []
for pattern in all_seisuuretu:
    beautiful_list.append(sum(pattern))

for i in range(len(beautiful_list)):
    beautiful_list[i] = bin(beautiful_list[i])[2:]

list_max = []
for i in beautiful_list:
    list_max.append(len(i))
list_max = max(list_max)
list_max = -list_max

status = True
while status == True:
    temp = []
    count = 0
    for i in beautiful_list:
        try:
            if int(i[list_max]) == 1:
                count += 1
                temp.append(i)
        except:
            continue
    if count > int(nk[1]):
        list_max += 1
        beautiful_list = temp
    elif count == int(nk[1]):
        beautiful_list = temp
        for num in range(len(beautiful_list)):
            if num == 0:
                ronriseki = int(beautiful_list[num], 2)
            else:
                ronriseki = ronriseki & int(beautiful_list[num], 2)
        print(ronriseki)
        status = False
    elif list_max == -1:
        for num in range(len(beautiful_list)):
            if num == 0:
                ronriseki = int(beautiful_list[num], 2)
            else:
                ronriseki = ronriseki & int(beautiful_list[num], 2)
        print(ronriseki)
        status = False
    else:
        list_max += 1

Submission status: Time Limit Exceeded

Solution code:
import itertools

nk = input().split()
seisuuretu = input().split()

for i in range(len(seisuuretu)):
    seisuuretu[i] = int(seisuuretu[i])

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

beautiful_list = []

for pattern in all_seisuuretu:
    beautiful_list.append(sum(pattern))

select_index = []

for index in itertools.permutations(range(len(beautiful_list)), r=int(nk[1])):
    select_index.append(index)

result = []

for temp_index in select_index:
    count = 0
    for one_index in temp_index:
        if count == 0:
            temp_and = beautiful_list[one_index]
        else:
            temp_and = temp_and & beautiful_list[one_index]
        count += 1
    result.append(temp_and)

print(max(result))

Submission status: Time Limit Exceeded

Solution code:
import sys
import numpy as np
import itertools

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N, K = lr()
A = np.array([0] + lr())
A_cum = A.cumsum()
beauties = []
for i, j in itertools.combinations(range(N+1), 2):
    beauties.append(A_cum[j] - A_cum[i])

beauties.sort(reverse=True)
j = 0
def check(x):
    return sum([x&y == x for y in beauties]) >= K

answer = 0
for i in range(40, -1, -1):
    if check(answer + (1 << i)):
        answer += 1 << i

print(answer)
# 00

Submission status: Time Limit Exceeded

Solution code:
def main():
    from itertools import accumulate

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

    acc = (0,) + tuple(accumulate((a)))

    bits = [0] * 40

    seg_idx = 0
    for r in range(1, n + 1):
        for l in range(r):
            b = acc[r] - acc[l]
            for j in range(40):
                if (b >> j) & 1:
                    bits[j] |= (1 << seg_idx)
            seg_idx += 1

    ret = 0
    t = (1 << seg_idx) - 1
    for j in range(39, -1, -1):
        s = t & bits[j]
        if bin(s).count('1') >= k:
            ret |= (1 << j)
            t = s
    print(ret)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    from itertools import accumulate

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

    acc = (0,) + tuple(accumulate(a))

    bs = tuple(set() for _ in range(40))

    pow2 = [1 << j for j in range(40)]

    m = 0
    for l in range(n):
        for r in range(l + 1, n + 1):
            beauty = acc[r] - acc[l]
            for j in range(40):
                if beauty & pow2[j] > 0:
                    bs[j].add(m)
            m += 1

    ret = 0
    t = set(range(m))
    for j in range(39, -1, -1):
        s = t & bs[j]
        if len(s) >= k:
            ret += pow2[j]
            t = s
    print(ret)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
from itertools import accumulate

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

acc = (0,) + tuple(accumulate(a))

bs = tuple(set() for _ in range(40))

m = 0
for l in range(n):
    for r in range(l + 1, n + 1):
        beauty = acc[r] - acc[l]
        for j in range(40):
            if beauty & (1 << j) > 0:
                bs[j].add(m)
        m += 1

ret = 0
t = set(range(m))
for j in range(39, -1, -1):
    s = t & bs[j]
    if len(s) >= k:
        ret += 1 << j
        t = s
print(ret)

Submission status: Time Limit Exceeded

Solution code:
import sys
input=sys.stdin.readline
n,k=map(int,input().split())
lists=list(map(int,input().split()))
GG=[]
for i in range(len(lists)+1):
    for j in range(i,len(lists)):
        
        G=sum(lists[i:j+1])
        GG.append(G)
        
GG=sorted(GG,reverse=True)      
maxi=format(GG[0],"b")
from collections import deque
d=deque()
e=deque()
for j in range(len(GG)):
    d.append(format(GG[j],"b").zfill(len(maxi)))
answer=0
index=0

while index!=len(maxi):
    counter=0
    for some in d:
        if some[index]=="1":
            counter+=1
            e.append(some)
    if counter>=k:
        answer+=2**(len(maxi)-1-index)
        d=e
        e=deque()
    elif counter<k:
        d=d
        e=deque()
        
    index+=1
    counter=0


print(answer)

Submission status: Time Limit Exceeded

Solution code:
n,k=map(int,input().split())
lists=list(map(int,input().split()))
GG=[]
for i in range(len(lists)+1):
    for j in range(i,len(lists)):
        
        G=sum(lists[i:j+1])
        GG.append(G)
        
GG=sorted(GG,reverse=True)      
maxi=format(GG[0],"b")
from collections import deque
d=deque()
e=deque()
for j in range(len(GG)):
    d.append(format(GG[j],"b").zfill(len(maxi)))
answer=0
index=0

while index!=len(maxi):
    counter=0
    for some in d:
        if some[index]=="1":
            counter+=1
            e.append(some)
    if counter>=k:
        answer+=2**(len(maxi)-1-index)
        d=e
        e=deque()
    elif counter<k:
        d=d
        e=deque()
        
    index+=1
    counter=0


print(answer)

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations
N,K=list(map(int,input().split()))
a = list(map(int,input().split()))
ans = []
for i in range(N):
    for j in range(i+1,N+1):
        ans.append(sum(a[i:j]))
ans2 = []
for i in combinations(ans,K):
    c = i[0]
    for j in range(1,len(i)):
        c = c&i[j]
    ans2.append(c)
print(max(ans2))

Submission status: Time Limit Exceeded

Solution code:
N,K = map(int,input().split())
A = list(map(int,input().split()))
S = []
for i in range(N):
    t = i
    s = 0
    for j in range(t,N):
        s += A[j]
        S.append(s)
n = len(S)
x = 0
for i in range(40):
    m = 40 - i
    t = m - 1
    k = 0
    for j in range(n):
        if (x+2**t)&S[j] == x+2**t:
            k += 1
    if k >= K:
        x += 2**t
print(x)

Submission status: Time Limit Exceeded

Solution code:
N, K = map(int, input().split())
A = [0] + [int(a) for a in input().split()]
for i in range(N):
    A[i+1] += A[i]
S = []
for i in range(1, N+1):
    for j in range(i):
        S.append(A[i] - A[j])
l, r = 0, 1 << 50
while r - l > 1:
    m = (l+r) // 2
    if len([1 for i in range(len(S)) if S[i] & m == m]) >= K:
        l = m
    else:
        r = m
print(l)

Submission status: Time Limit Exceeded

Solution code:
def main():
  import numpy as np
  import sys
  readline = sys.stdin.readline
  readlines = sys.stdin.readlines
  N,K = map(int,readline().split())
  A = np.array(readline().split(),np.int64)
  AA = np.cumsum(A)
  L = AA
  for i in range(N-1):
    AA = AA - A[i]
    L = np.concatenate([L,AA[i+1:]])
  ans = 0
  remains = []
  for i in range(40):
    bit = 1 << (39 - i)
    l = []
    cnt = 0
    for j in range(len(L)):
      if bit & L[j] == bit:
        cnt += 1
        l.append(L[j])
    if cnt >= K:
      ans += bit
      L = l
  print(ans)
main()

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
import sys
readline = sys.stdin.readline
readlines = sys.stdin.readlines
N,K = map(int,readline().split())
A = np.array(readline().split(),np.int64)
AA = np.cumsum(A)
#print(AA)
L = AA
for i in range(N-1):
  AA = AA - A[i]
  L = np.concatenate([L,AA[i+1:]])

ans = 0
remains = []
for i in range(40):
  bit = 1 << (39 - i)
  l = []
  cnt = 0
  for j in range(len(L)):
    if bit & L[j] == bit:
      cnt += 1
      l.append(L[j])
  if cnt >= K:
    ans += bit
    L = l
print(ans)