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: Runtime Error

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
b=[]
tmp=0
for i in range(len(a)):
    tmp=a[i]+tmp
    b.append(tmp)
c=[]
l=len(b)
if (k!=1):
    for i in range(k-1):    
        c.append(b[l-1-i])
    for i in range(k-1):
        for j in range(k-1):
            c.append(b[l-j-1]-b[i])
            if (b[l-j-1]-b[i])<b[-k+1]:
                break 

    c.sort(reverse=True)
    print(c)              
    print(c[k-1]) 

else:
    print(b[l-1])

Submission status: Runtime Error

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

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

b.sort()
b=b[::-1]
for sp in range(0,N+1-K):
    if [len(bin(b[i])[2:]) for i in range(sp,sp+K)] == [len(bin(b[sp])[2:])for _ in range(sp,sp+K)]:
        break
s=b[sp:sp+K]
if K==1:
    tmp=s[0]
else:
    tmp=s[0]&s[1]
    for i in range(2,K):
        tmp=tmp&s[i]

print(tmp)

Submission status: Runtime Error

Solution code:
import numpy as np
import collections as cp
N, K = map(int,input().split())
print("N,K",N,K)
a_arr = input().split()
tmp = []
for ele in a_arr:
    tmp.append(int(ele))
arr = tmp
sum_arr = []
bin_arr = []
len_arr = []
for i in range(N):
    for j in range(i,N):
        tmpnum = sum(arr[i:j+1])
        sum_arr.append(tmpnum)
        bin_arr.append(bin(tmpnum))
        len_arr.append(len(bin(tmpnum)))

print(bin_arr)
max_len = max(len_arr) - 2
ans = 0
tmp_arr = sum_arr
loop = 0
end_flag = 0
get_ev = 0
while end_flag==0:
    ans_arr = []
    non_arr = []
    number = 0
    two_pow = pow(2, max_len-1-loop)
    loop += 1
    for sum_tmp in tmp_arr:
        if sum_tmp >= two_pow:
            number +=1
            ans_arr.append(sum_tmp - two_pow)
        else:
            non_arr.append(sum_tmp)
    if number >=K:
        tmp_arr = ans_arr
        ans += two_pow
        get_ev = 1
    elif get_ev == 1:
        end_flag = 1
    else:
        tmp_arr = ans_arr.append(non_arr)
        if loop >= max_len-1:
            end_flag = 1

print(ans)

Submission status: Runtime Error

Solution code:
import itertools
import numpy as np
def main():
    n,k = map(int, input().split())
    a = list(map(int, input().split()))

    x = [sum(a[i:j+1]) for i in range(n) for j in range(i,n)]
    x.sort()
    x = x[::-1]
    ans = []
    for ls in itertools.combinations(range(k**2), k):
        y = x[ls[0]]
        for i in ls[1:]:
            y = y & x[i]
        ans.append(y)
    print(max(ans))
    # for i in range(k-1):
    #     ans = ans & x[i+1]
    # print(ans)

if __name__ == "__main__":
    main()

Submission status: Runtime Error

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

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

dp = [[-1 for k in range(N*(N+1)//2+1)] for n in range(1000+1)]

def generate_substring(a):
    for i in range(N):
        for j in range(i, N):
            yield a[i:j+1]

def find_max_and(i, k):

    if dp[i][k] >= 0:
        return dp[i][k];
    
    if i == N_SUBSTR:
        res = 0xFFFF if k == 0 else 0
    elif k == 0:
        res = find_max_and(i + 1, k)
    else:
        a = find_max_and(i + 1, k)
        b = find_max_and(i + 1, k - 1) & scores[i]
        
        res = b if a == 0xFFFF else max(a, b)

    dp[i][k] = res
    return res

scores = list(map(sum, generate_substring(a)))
N_SUBSTR = len(scores)
print(find_max_and(0, K))

Submission status: Runtime Error

Solution code:
import itertools as tool

N, K = map(int, input().split())
a = [int(x) for x in input().split()]

beautifuls = []
for i in range(0, len(a)):
    for j in range(i+1, len(a)+1):
        beautiful = sum(a[i:j])
        beautifuls.append(beautiful)

beau_K = list(tool.combinations(beautifuls, K))
ands = []
for combi in beau_K:
    for i in range(K):
        if i == 0:
            tmp = combi[0]
        else:
            tmp = bin(tmp & combi[i])
    ands.append(eval(str(tmp)))
print(max(ands))

Submission status: Runtime Error

Solution code:
import math

def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

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

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

ruisekia=[]
keta=[0]*30
for i in range(N):
  temp=0
  for j in range(i,N):
    temp+=a[j]
    ruisekia.append(temp)
ruisekia.sort(reverse=True)

print(ruisekia)
for i in range(combinations_count(N,K)):
  for j in range(K):
    if ruisekia[i]==1:
      x=5

'''
for i in range(len(ruisekia)):
  tempo=ruisekia[i]
  while(tempo!=0):
    x=1
    count=0
    while(tempo>=x*2):
      x*=2
      count+=1
    keta[count]+=1
    tempo-=x
print(keta)
'''

Submission status: Runtime Error

Solution code:

N,K=map(int,input().split())
a=list(map(int,input().split()))
L=[]
for i in range(N):
    for j in range(i+1,N+1):
        L.append(sum(a[i:j]))

L.sort(reverse=True)
M=[]
#print(L)

for j in range(31):
    if 2**(31-j)<=L[0]:
        if 2**(31-j)<=L[K-1]:
            for l in range(N):
                if L[l]>=2**(31-j):
                    M.append(L[l])
            break
        else:
            for k in range(N):
                L[k]=L[k]%(2**(31-j))
            L.sort(reverse=True)
            
ans=0
#print(M)
#print(31-j)
for a in range(31-j):
    if len(M)<K:
        break
    M.sort(reverse=True)
    if M[K-1]>=2**(31-j-a):
        ans+=2**(31-j-a)
        for b in range(len(M)):
            if M[b]>=2**(31-j-a):
                M[b]%=2**(31-j-a)
            else:
                del M[b]
    
        
print(ans)

Submission status: Runtime Error

Solution code:
import random

def ra(p):
    ans=p[0]
    for i in range(K):
        ans=ans&p[i]
    return ans

N,K=list(map(int,input().split()))
a=list(map(int,input().split()))
s=[]
i=0
j=N
while(1):
    if(i+j<=N):
        s.append(sum(a[i:i+j]))
        i+=1
    else:
        i=0
        j-=1
    if(j==1 and i==N):    break
s.sort(reverse=True)

con=2**(len(bin(s[0]))-3)
t=[s[0]]
i=1
while(1):
    if(s[i]>=con):
        t.append(s[i])
        i+=1
    else:
        break

m=0
for i in range(100):
    h=ra(random.sample(t,K))
    if m < h:
        m=h
print(m)

Submission status: Runtime Error

Solution code:
N,K=map(int,input().split())
a=list(map(int,input().split()))
L=[]
for i in range(N):
    for j in range(i+1,N+1):
        L.append(sum(a[i:j]))

L.sort(reverse=True)
M=[]
#print(L)

for j in range(31):
    if 2**(31-j)<=L[0]:
        if 2**(31-j)<=L[K-1]:
            for l in range(N):
                if L[l]>=2**(31-j):
                    M.append(L[l])
            break
        else:
            for k in range(N):
                L[k]=L[k]%(2**(31-j))
            L.sort(reverse=True)
            
ans=0
#print(M)
#print(31-j)
for a in range(31-j):
    M.sort(reverse=True)
    if M[K-1]>=2**(31-j-a):
        ans+=2**(31-j-a)
        for b in range(len(M)):
            if M[b]>=2**(31-j-a):
                M[b]%=2**(31-j-a)
            else:
                del M[b]
    
        
print(ans)

Submission status: Runtime Error

Solution code:
from functools import reduce
N, K = input().split()
a = list(map(lambda x : int(x) ,input().split()))

#N, K = list(map(lambda x : int(x) , "8 4".split()))
#a = list(map(lambda x : int(x) , "9 1 8 2 7 5 6 4".split()))

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

def conbination(beauty, r, list, cnt, answers):
	if cnt == r:
		print(list)
		answers.append( reduce(lambda x, y: x & y, list))
		return
	
	for i in range(0, len(beauty)):
		cnt += 1
		list.append(beauty[i])
		
		conbination(beauty[i+1:], r, list, cnt, answers)

		cnt -= 1
		list.pop()

answers = []
beauty = list(map(lambda x: sum(x), substring))

conbination(beauty, K, [], 0, answers)

print(max(answers))

print(N, K)

Submission status: Runtime Error

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

sum_a = 0
for i in range(N):
    sum_a += a[i]

temp = 0
a.sort()
for i in range(K):
    temp = sum_a - a[i]
    result = sum_a & temp
print(result)

Submission status: Runtime Error

Solution code:
import numpy as np

N, K = [int(i) for i in input().split()]
a = np.cumsum([int(i) for i in input().split()])

l, r = -1, N - 1
numList = [a[-1]]
count = 1
while(count < K):
    if a[-1] - a[l+1] >= a[r-1]:
        l += 1
        numList.append(a[-1] - a[l])
    else:
        r -= 1
        numList.append(a[r])
    count += 1

ans = a[-1]
for num in numList:
    ans &= num

print(ans)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
from pprint import pprint
import itertools

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

rui = [0]
tmp = 0
for a in an:
    rui.append(tmp+a)
    tmp += a


sum_list = [] 

for l in range(1,N+1):
    for r in range(l,N+1):
        tmp = rui[r] - rui[l-1]
        sum_list.append(tmp)

bin_list = [[0 for _ in range(30)] for _ in range(len(sum_list))]

for i,s in enumerate( sum_list):
    tmps = str(bin(s))[2:]
    for j,b in enumerate( reversed(tmps)):
        bin_list[i][j] = int(b)

# pprint(bin_list)

sum_bin = []
for j in range(30):
    tmp_bin = 0
    for i in range(len(sum_list)):
        tmp_bin += bin_list[i][j]
    sum_bin.append(tmp_bin)

# pprint(sum_bin)

max_keta = -1
for i,s in enumerate( sum_bin):
    if s >= K:
        max_keta = i

if max_keta == -1:
    print(0)
    exit()

sub_sum_list = [a for a in sum_list if 2**(max_keta) <= a < 2**(max_keta+1)]

# pprint(sub_sum_list)

ans_list = []
for c in itertools.combinations(sub_sum_list, K):
    ans = c[0]
    for s in c[1:K]:
        ans &= s

    ans_list.append(ans)

if len(ans_list) == 0:
    print(0)
else:
    print(max(ans_list))

Submission status: Runtime Error

Solution code:
import numpy as np
n,k=map(int, input().split())
a=list(map(int, input().split()))
a2=[0]
for i in a:
    a2.append(a2[-1]+i)
c=dict()
for i in range(n+1):
    for j in range(i,n+1):
        if j>i:
            c[a2[j]-a2[i]]=c.get(a2[j]-a2[i],0)+1
keys=list(c.keys())
keys.sort()
keys2=[bin(i).split('b')[1] for i in keys]
maxb=len(kyes2[-1])
keys2=[i.zfill(maxb) for i in keys2]
bin_list=[]
for i in keys2:
    bin_list.append(list(map(int,list(i))))
bin_list=np.array(bin_list)
for i,j in enumerate(keys):
    bin_list[i]=bin_list[i]*c[j]
bin_list
ans=0
l=0
while l<maxb:
    if bin_list[:,l].sum()>=k:
        ans+=2**(maxb-l-1)
        bin_list=bin_list[bin_list[:,l]>0]
    l+=1
print(ans)

Submission status: Runtime Error

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

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

dp = [[-1 for k in range(N*(N+1)//2+1)] for n in range(1000+1)]

def generate_substring(a):
    for i in range(N):
        for j in range(i, N):
            yield a[i:j+1]

def find_max_and(i, k):

    if dp[i][k] >= 0:
        return dp[i][k];
    
    if i == N_SUBSTR:
        res = 0xFFFF
    elif k == 0:
        res = find_max_and(i + 1, k)
    else:
        a = find_max_and(i + 1, k)
        b = find_max_and(i + 1, k - 1) & scores[i]
        
        res = b if a == 0xFFFF else max(a, b)

    dp[i][k] = res
    return res

scores = list(map(sum, generate_substring(a)))
N_SUBSTR = len(scores)
print(find_max_and(0, K))

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
from pprint import pprint
import itertools

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

rui = [0]
tmp = 0
for a in an:
    rui.append(tmp+a)
    tmp += a


sum_list = [] 

for l in range(1,N+1):
    for r in range(l,N+1):
        tmp = rui[r] - rui[l-1]
        sum_list.append(tmp)

bin_list = [[0 for _ in range(30)] for _ in range(len(sum_list))]

for i,s in enumerate( sum_list):
    tmps = str(bin(s))[2:]
    for j,b in enumerate( reversed(tmps)):
        bin_list[i][j] = int(b)

# pprint(bin_list)

sum_bin = []
for j in range(30):
    tmp_bin = 0
    for i in range(len(sum_list)):
        tmp_bin += bin_list[i][j]
    sum_bin.append(tmp_bin)

# pprint(sum_bin)

max_keta = -1
for i,s in enumerate( sum_bin):
    if s >= K:
        max_keta = i

if max_keta == -1:
    print(0)
    exit()

sub_sum_list = [a for a in sum_list if 2**(max_keta) <= a < 2**(max_keta+1)]

# pprint(sub_sum_list)

ans_list = []
for c in itertools.combinations(sub_sum_list, K):
    ans = c[0]
    for s in c[1:K]:
        ans &= s

    ans_list.append(ans)

print(max(ans_list))

Submission status: Runtime Error

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

sum_a = 0
for i in range(N):
    sum_a += a[i]

temp = 0
for i in range(K-1):
    temp = sum_a - a.pop(min(a))
    result = sum_a & temp
print(result)

Submission status: Runtime Error

Solution code:
import sys
import copy
from collections import deque

input = sys.stdin.readline
n, k = map(int, input().split())
a_list = deque(list(map(int, input().split())))

bit_list = deque([int(sum(a_list))])
l_list = copy.deepcopy(a_list)
r_list = copy.deepcopy(a_list)
l, r = 0, 0
for _ in range(1, k):
    if l + l_list[0] < r + r_list[-1]:
        l += l_list.popleft()
        bit_list.append(int(sum(l_list)))
    else:
        r += r_list.pop()
        bit_list.append(int(sum(r_list)))
cnt = k + 1
ans = 0
while cnt < n:
    tmp = bit_list.popleft()
    for b in bit_list:
        tmp = int((bin(tmp & b)), 2)
    ans = max(ans, tmp)
    if l + l_list[0] < r + r_list[-1]:
        l += l_list.popleft()
        bit_list.append(int(sum(l_list)))
    else:
        r += r_list.pop()
        bit_list.append(int(sum(r_list)))
    cnt += 1
print(ans)

Submission status: Runtime Error

Solution code:
N, K = map(int, input().split())
a = list(map(int, input().split()))
b = []
start = 0
end = N-1
sum1 = 0
sum2 = 0
for i in range(K):
    if sum1 + sum2 <= min(sum1 + a[start], sum2 + a[end]):
        b.append(sum1 + sum2)
        if a[start] < a[end]:
            sum1 += a[start]
            start += 1
        else:
            sum2 += a[end]
            end -= 1
    elif sum1 + a[start] > sum2 + a[end]:
        sum2 += a[end]
        end -= 1
        b.append(sum2)
    else:
        sum1 += a[start]
        start += 1
        b.append(sum1)
print(sum(a)-b[-1])

Submission status: Runtime Error

Solution code:
#include <iostream>
#include <algorithm>
using namespace std;
 
int main() {
	long long n, m, t=0, ans=0;
	cin >> n >> m;
	long long A[n];
	for(int i=0; i<n; i++)
	{
		cin >> A[i];
		ans+=A[i];
	}
	if(n==1)
		cout << 0;
	else{	
	sort(A, A+n);
	long long q=0;
	for(int i=n-1; i>=1; i--)
	{
		if(A[i]!=A[i-1])
		{
			if(t>A[i]-A[i-1]-1)
				t-=A[i]-A[i-1]-1;
			else	
				q+=A[i]-A[i-1]-1-t;
				t=0;
		}
		else
			t++;
	}
	//cout << q;
//	if(A[0]!=A[1]){
	if(A[1]-(n-2)>0)
	{
		if(t>max(min(A[1]-(n-2)-2, A[0]-1), 0LL))
			t-=max(min(A[1]-(n-2)-2, A[0]-1), 0LL);
		else	
			q+=max(min(A[1]-(n-2)-2, A[0]-1), 0LL)-t;
			t=0;
	}
//	}
	//cout << ans<< " " << n <<" " <<q;
	ans-=n+q;
	cout << ans;
	}
/*	for(int i=n-1; i>=1; i--)
	{
		if((A[i]-A[i-1])==0){
			ans+=A[i]-1;
			A[i-1]--;
		}
		else
		{
			ans+=A[i-1];
		}
	}
	*/
	return 0;
}

Submission status: Runtime Error

Solution code:
import sys
import copy
from collections import deque

input = sys.stdin.readline
n, k = map(int, input().split())
a_list = deque(list(map(int, input().split())))

bit_list = deque([int(sum(a_list))])
l_list = copy.deepcopy(a_list)
r_list = copy.deepcopy(a_list)
l, r = 0, 0
for _ in range(1, k):
    if l + l_list[0] < r + r_list[-1]:
        l += l_list.popleft()
        bit_list.append(int(sum(l_list)))
    else:
        r += r_list.pop()
        bit_list.append(int(sum(r_list)))
cnt = k + 1
ans = 0
while cnt < n:
    tmp = bit_list.popleft()
    for b in bit_list:
        tmp = int((bin(tmp & b)), 2)
    ans = max(ans, tmp)
    bit_list.popleft()
    if l + l_list[0] < r + r_list[-1]:
        l += l_list.popleft()
        bit_list.append(int(sum(l_list)))
    else:
        r += r_list.pop()
        bit_list.append(int(sum(r_list)))
    cnt += 1
print(ans)

Submission status: Runtime Error

Solution code:
#include <iostream>
#include <algorithm>
using namespace std;
 
int main() {
	long long n, m, t=0, ans=0;
	cin >> n >> m;
	long long A[n];
	for(int i=0; i<n; i++)
	{
		cin >> A[i];
		ans+=A[i];
	}
	if(n==1)
		cout << 0;
	else{	
	sort(A, A+n);
	long long q=0;
	for(int i=n-1; i>=1; i--)
	{
		if(A[i]!=A[i-1])
			q+=A[i]-A[i-1]-1;
		else
			t++;
	}
	//cout << q;
//	if(A[0]!=A[1]){
	if(A[1]-(n-2)>0)
		q+=max(min(A[1]-(n-2)-2, A[0]-1), 0LL);
//	}
	//cout << ans<< " " << n <<" " <<q;
	ans-=n-t+q;
	cout << ans;
	}
/*	for(int i=n-1; i>=1; i--)
	{
		if((A[i]-A[i-1])==0){
			ans+=A[i]-1;
			A[i-1]--;
		}
		else
		{
			ans+=A[i-1];
		}
	}
	*/
	return 0;
}

Submission status: Runtime Error

Solution code:
import sys
import copy
from collections import deque

input = sys.stdin.readline
n, k = map(int, input().split())
a_list = deque(list(map(int, input().split())))

bit_list = deque([int(sum(a_list))])
l_list = copy.deepcopy(a_list)
r_list = copy.deepcopy(a_list)
l, r = 0, 0
for _ in range(1, k):
    if l + l_list[0] < r + r_list[-1]:
        l += l_list.popleft()
        bit_list.append(int(sum(l_list)))
    else:
        r += r_list.pop()
        bit_list.append(int(sum(r_list)))
cnt = k
while cnt < n:
    ans = bit_list.popleft()
    for b in bit_list:
        ans = int((bin(ans & b)), 2)
    if ans == 0:
        bit_list.popleft()
        if l + l_list[0] < r + r_list[-1]:
            l += l_list.popleft()
            bit_list.append(int(sum(l_list)))
        else:
            r += r_list.pop()
            bit_list.append(int(sum(r_list)))
        cnt += 1
    else:
        break
print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np

N, K = [int(i) for i in input().split()]
a = np.cumsum([int(i) for i in input().split()])

l, r = 0, N - 1
numList = []
count = 0
while(count < K):
    if a[l] < a[N-1] - a[r]:
        numList.append(a[N-1] - a[l])
        l += 1
    else:
        numList.append(a[r])
        r -= 1
    count += 1

ans = a[-1]
for num in numList:
    ans &= num

print(ans)

Submission status: Runtime Error

Solution code:
N,K=map(int,input().split())
a=list(map(int,input().split()))
L=[]
for i in range(N):
    for j in range(i+1,N+1):
        L.append(sum(a[i:j]))

L.sort(reverse=True)
M=[]
#print(L)

for j in range(31):
    if 2**(31-j)<=L[0]:
        if 2**(31-j)<=L[K-1]:
            for l in range(N):
                if L[l]>=2**(31-j):
                    M.append(L[l])
            break
        else:
            for k in range(N):
                L[k]=L[k]%(2**(31-j))
            L.sort(reverse=True)
            
ans=2**(31-j)
#print(M)

for a in range(1,31-j):
    for b in range(len(M)):
        M[b]%=(31-j-a)
    M.sort(reverse=True)
    if M[K-1]>=2**(31-j-a):
        ans+=2**(31-j-a)
        for c in range(len(M)):
            if M[c]<2**(31-j-a):
                del M[c]
print(ans)

Submission status: Runtime Error

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

tmp = [sum(a_array[x:]) for x in range(n) if x < 4]
for i in range(1, n):
    for j in range(i, n):
        sum_a = sum(a_array[j:])
        if sum_a < min(tmp) and len(tmp) == 4:
            break
        tmp.remove(min(tmp))
        tmp.append(sum_a)

tmp = sorted(tmp)
print(tmp[-k])

Submission status: Runtime Error

Solution code:
import sys
from collections import deque

input = sys.stdin.readline
n, k = map(int, input().split())
a_list = deque(list(map(int, input().split())))

bit_list = [int(sum(a_list))]
l_list = a_list.copy()
r_list = a_list.copy()
l, r = 0, 0
for _ in range(1, k):
    if l + l_list[0] < r + r_list[-1]:
        l += l_list.popleft()
        bit_list.append(int(sum(l_list)))
    else:
        r += r_list.pop()
        bit_list.append(int(sum(r_list)))
ans = int(bit_list[0])
for b in bit_list[1:]:
    ans = int((bin(ans & b)), 2)
print(ans)

Submission status: Runtime Error

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

ans = sum(a)
for i in range(k-1):
    if a[0] >= a[-1]:
        a.pop()
        ans = ans & sum(a)
    else:
        a.popleft()
        ans = ans & sum(a)
print(ans)

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10**7)
n,k=map(int,input().split())
a=[int(i) for i in input().split()]
S=[0 for i in range(n+1)]
for i in range(1,n+1):
    S[i]=S[i-1]+a[i-1]
List=[]
for i in range(n+1):
    for j in range(i):
        List.append(S[i]-S[j])
List.sort(reverse=True)
def program(N,K,L):
    #print(N,K,L)
    M=len(L)
    b=[bin(i)[2:][::-1] for i in L]
    X=[[] for i in range(35)]
    for i in b:
        for j in range(len(i)):
            if i[j]=="1":
                X[j].append(i)
    l=35
    for i in range(34,-1,-1):
        if len(X[i])>=K:
            l=i
            break
    if l==35:
        return 0
    #print(X[l])
    R=[]
    for i in X[l]:
        if i[:-1]=="":
            R.append(0)
            continue
        R.append(int(i[:-1][::-1],2))
    R.sort(reverse=True)
    return program(len(R),K,R)+2**l
print(program(n,k,List))

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10**7)
n,k=map(int,input().split())
a=[int(i) for i in input().split()]
S=[0 for i in range(n+1)]
for i in range(1,n+1):
    S[i]=S[i-1]+a[i-1]
List=[]
for i in range(n+1):
    for j in range(i):
        List.append(S[i]-S[j])
List.sort(reverse=True)
def program(N,K,L):
    #print(N,K,L)
    M=len(L)
    b=[bin(i)[2:][::-1] for i in L]
    X=[[] for i in range(35)]
    for i in b:
        for j in range(len(i)):
            if i[j]=="1":
                X[j].append(i)
    l=35
    for i in range(34,-1,-1):
        if len(X[i])>=K:
            l=i
            break
    if l==35:
        return 0
    #print(X[l])
    R=[]
    for i in X[l]:
        if i[:-1]=="":
            R.append(0)
            continue
        R.append(int(i[:-1][::-1],2))
    return program(len(R),K,R)+2**l
print(program(n,k,List))

Submission status: Runtime Error

Solution code:
N,K = map(int,input().split())
A = list(map(int,input().split()))
L = []
for i in range(N):
    for j in range(1,N+1):
        if i > j - 1:
            continue
        else:
            l = A[i:j]
            L.append(sum(l))
L = list(set(L))
def combinationListRecursive(data, r):
  if r == 0 or r > len(data):
    return []

  result = []
  _combinationListRecursive(data, r, 0, [], result)
  return result


def _combinationListRecursive(data, r, start, progress, result):
  if r == 0:
    result.append(progress)
    return

  for i in range(start, len(data)):
    _combinationListRecursive(data, r - 1, i + 1, progress + [data[i]], result)
result = combinationListRecursive(L,K)
ans_list = []
for list_c in result:
    for e in list_c:
        if e == list_c[0]:
            copy = e
            continue
        AND = copy & e
        if e == list_c[-1]:
            ans = AND
            ans_list.append(ans)
            break
        copy = AND
print(max(ans_list))

Submission status: Runtime Error

Solution code:
n,k=map(int,input().split())
a=[int(i) for i in input().split()]
S=[0 for i in range(n+1)]
for i in range(1,n+1):
    S[i]=S[i-1]+a[i-1]
List=[]
for i in range(n+1):
    for j in range(i):
        List.append(S[i]-S[j])
List.sort(reverse=True)
def program(N,K,L):
    #print(N,K,L)
    M=len(L)
    b=[bin(i)[2:][::-1] for i in L]
    X=[[] for i in range(35)]
    for i in b:
        for j in range(len(i)):
            if i[j]=="1":
                X[j].append(i)
    l=35
    for i in range(34,-1,-1):
        if len(X[i])>=K:
            l=i
            break
    if l==35:
        return 0
    #print(X[l])
    R=[]
    for i in X[l]:
        if i[:-1]=="":
            R.append(0)
            continue
        R.append(int("0b"+i[:-1][::-1],2))
    return program(len(R),K,R)+2**l
print(program(n,k,List))

Submission status: Runtime Error

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


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


beauty = sorted(beauty, reverse=True)
beauty_bin = [len(bin(b)[2:]) for b in beauty]

ans_list = []
for j in range(N-K):
    k = beauty[j:j+K]
    ans = k[0]
    for i in range(1, K):
        ans = ans & k[i]
    ans_list.append(ans)


print(max(ans_list))

Submission status: Runtime Error

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

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

b.sort(reverse=True)
s=b[:K]
tmp=s[0]&s[1]
for i in range(2,K):
    tmp=tmp&s[i]

print(tmp)

Submission status: Runtime Error

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


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


beauty = sorted(beauty, reverse=True)
beauty_bin = [len(bin(b)[2:]) for b in beauty]

if sum(beauty[:K]) == len(bin(beauty[0])[2:]) * K:
    k = beauty[:K]
    ans = k[0]
    for i in range(1, K):
        ans = ans & k[i]
    print(ans)
else:
    k1 = beauty[:K]
    ans1 = k1[0]
    for i in range(1, K):
        ans1 = ans1 & k1[i]
    for i in range(N):
        if len(bin(beauty[i])[2:]) != len(bin(beauty[0])[2:]):
            l = i
            break
    k2 = beauty[l:l+K]
    ans2 = k2[0]
    for i in range(1, K):
        ans2 = ans2 & k2[i]
    print(max(ans1, ans2))

Submission status: Runtime Error

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

a = np.array(a)

s = 0
e = 0

arr = []
for s in range(N):
    for e in range(s+1, N+1):
        arr.append(a[s:e])

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

su = 0
b = [bin(np.sum(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: Runtime Error

Solution code:
n,k=map(int,input().split())
*a,=map(int,input().split())
l=[]
for i in range(n):
    for j in range(i+1,n+1):
        l.append(sum(a[i:j]))
l=sorted(l)[-k:][::-1]
c=l[0]
for t in l[1:]:
    c&=t
print(t)

Submission status: Runtime Error

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

m = sum(a)

w = []
p = 0
q = 0
for i in range(n):
    p += a[i]
    q += a[n-1-i]
    w.append(p)
    w.append(q)
w.sort()

res = m
for i in range(k-1):
    res = res&(m-w[i])    
print(res)

Submission status: Runtime Error

Solution code:
N,K=map(int,input().split())
a=[int(i) for i in input().split()]
S=[0 for i in range(N+1)]
for i in range(1,N+1):
    S[i]=S[i-1]+a[i-1]
L=[]
for i in range(N+1):
    for j in range(i):
        L.append(S[i]-S[j])
X=[0 for i in range(35)]
for i in L:
    X[(len(bin(i))-2)]+=1
#print(X)
j=35
for i in range(34,-1,-1):
    if X[i]>=K:
        j=i
        break
R=[]
for i in L:
    if len(bin(i))==j+2:
        R.append(i)
R.sort(reverse=True)
ans=R[0]
#print(sorted(L,reverse=True))
#print(R,j)
for i in range(1,K):
    ans=ans&R[i]
print(ans)