Mr Keks is a typical white-collar in Byteland.

He has a bookshelf in his office with some books on it, each book has an integer positive price.

Mr Keks defines the value of a shelf as the sum of books prices on it. 

Miraculously, Mr Keks was promoted and now he is moving into a new office.

He learned that in the new office he will have not a single bookshelf, but exactly $k$ bookshelves. He decided that the beauty of the $k$ shelves is the bitwise AND of the values of all the shelves.

He also decided that he won't spend time on reordering the books, so he will place several first books on the first shelf, several next books on the next shelf and so on. Of course, he will place at least one book on each shelf. This way he will put all his books on $k$ shelves in such a way that the beauty of the shelves is as large as possible. Compute this maximum possible beauty.


-----Input-----

The first line contains two integers $n$ and $k$ ($1 \leq k \leq n \leq 50$) — the number of books and the number of shelves in the new office.

The second line contains $n$ integers $a_1, a_2, \ldots a_n$, ($0 < a_i < 2^{50}$) — the prices of the books in the order they stand on the old shelf.


-----Output-----

Print the maximum possible beauty of $k$ shelves in the new office.


-----Examples-----
Input
10 4
9 14 28 1 7 13 15 29 2 31

Output
24

Input
7 3
3 14 15 92 65 35 89

Output
64



-----Note-----

In the first example you can split the books as follows:

$$(9 + 14 + 28 + 1 + 7) \& (13 + 15) \& (29 + 2) \& (31) = 24.$$

In the second example you can split the books as follows:

$$(3 + 14 + 15 + 92) \& (65) \& (35 + 89) = 64.$$

Difficulty: interview

Starter code:

Solutions:
#!/usr/bin/env python3

[n, k] = list(map(int, input().strip().split()))
ais = list(map(int, input().strip().split()))
iais = [0 for _ in range(n + 1)]
for i in range(n):
	iais[i + 1] = iais[i] + ais[i]

def calc(k, split):
	res = 0
	for i in range(k):
		res &= iais[split[i + 1]] - iais[split[i]]
	return res

def check_mask(mask):
	dp = [[False for j in range(n + 1)] for i in range(k + 1)]

	for j in range(1, n - k + 1 + 1):
		dp[1][j] = (iais[j] & mask == mask)
	if not any(dp[1]):
		return False

	for i in range(2, k + 1):
		for j in range(i, n - (k - i) + 1):
			dp[i][j] = any(dp[i - 1][r] and ((iais[j] - iais[r]) & mask == mask) for r in range(i - 1, j - 1 + 1))
		if not any(dp[i]):
			return False

	return dp[k][n]


mask = 0
for i in range(55, -1, -1):
	if check_mask(mask | (1 << i)):
		mask |= 1 << i

print (mask)



import time;
def getIntList():
    return list(map(int, input().split()));

def getTransIntList(n):
    first = getIntList();
    m = len(first);
    result = [[0] * n for _ in range(m)];
    for i in range(m):
        result[i][0] = first[i];
    for j in range(1, n):
        curr = getIntList();
        for i in range(m):
            result[i][j] = curr[i];
    return result;
n, k = getIntList();
a = getIntList();
#sums[i][j] - сумма a по индексам от i до j не включая j
sums = [[0]*(n+1) for _ in range(n+1)];
for i in range(n):
    for j in range(i+1, n+1):
        sums[i][j]=sums[i][j-1]+a[j-1];
class SearchProblem:
    def __init__(self, a, n, k, tiLim):
        self.a=a;
        self.n=n;
        self.k=k;
        self.maxResult=0;
        self.tiLim=time.time()+tiLim;
    def search(self, currResult, currIndex, currLines):
        #Время вышло - заканчиваем.
        if time.time()>self.tiLim:
            return;
        if currLines>0 and currResult<=self.maxResult:
            return;
        if currLines==self.k-1:
            lastSum = sums[currIndex][self.n];
            currResult = currResult & lastSum;
            if currResult > self.maxResult:
                self.maxResult = currResult;
        for nextIndex in range(currIndex+1, self.n+1):
            currSum=sums[currIndex][nextIndex];
            if currLines==0:
                nextResult=currSum
            else:
                nextResult=currResult & currSum;
            self.search(nextResult, nextIndex, currLines+1);
        flag=True;
        if time.time() > self.tiLim:
            flag=False;
        return self.maxResult, flag;
#upLim[i][j] - оценка сверху на красоту разбиения книг с номерами с j до конца по i полкам
upLim=[[0]*(n+1) for _ in range(k+1)];
for i in range(1, k+1):
    if i==1:
        for j in range(0, n):
            upLim[i][j]=sums[j][n];
    else:
        for j in range(n-i, -1, -1):
            upLim[i][j]=0;
            for j1 in range(j+1, n):
                curr=min(sums[j][j1], upLim[i-1][j1]);
                upLim[i][j]=max(upLim[i][j], curr);
def solve():
    #Сначала ищем начальное решение deepfirst
    problem=SearchProblem(a, n, k, 0.1);
    if k==1:
        return sum(a);
    maxResult, solved = problem.search(0, 0, 0);
    if solved:
        #print("deep first succeed")
        return maxResult;
    results = [[set() for _ in range(n + 1)] for _ in range(k + 1)];
    # А теперь ищем полное решение динамическим программированием
    for i in range(1, n + 1):
        for firstIndexSum in range(0, i):
            # print(firstIndexSum, i);
            currSum = sums[firstIndexSum][i];
            if firstIndexSum == 0:
                if currSum>maxResult:
                    results[1][i].add(currSum);
                if k == 2:
                    lastSum = sums[i][n];
                    currResult = currSum & lastSum;
                    if currResult > maxResult:
                        maxResult = currResult;
                        for lines1 in range(k):
                            for j in range(n):
                                results[lines1][j] = {r for r in results[lines1][j] if r >= maxResult}
            else:
                for lines in range(k):
                    for prevSum in results[lines][firstIndexSum]:
                        # print(prevSum,"&",currSum,"=",prevSum&currSum)
                        fullSum = prevSum & currSum;
                        currLines = lines + 1;
                        if currLines == k - 1:
                            lastSum = sums[i][n];
                            currResult = fullSum & lastSum;
                            if currResult > maxResult:
                                maxResult = currResult;
                                for lines1 in range(k):
                                    for j in range(n):
                                        results[lines1][j] = {r for r in results[lines1][j] if r >= maxResult}
                        if upLim[k-currLines][i]<=maxResult:
                            continue;
                        if fullSum>maxResult:
                            results[currLines][i].add(fullSum);
                        # print(i, results[i])

    return maxResult;
print(solve());

[n, k] = [int(x) for x in input().split()]
sum = [int(x) for x in input().split()]
for i in range(1, n):
    sum[i] += sum[i - 1]

def check(mask, all):
    dp = [[False for j in range(n)] for i in range(k)]
    for i in range(n):
        dp[0][i] = ((sum[i] & all & mask) == mask)
    for i in range(1, k):
        for j in range(n):
            dp[i][j] = any(dp[i - 1][p] and ((sum[j] - sum[p]) & all & mask) == mask for p in range(0, j))
    return dp[k - 1][n - 1]
    

ans = 0
for i in range(60, -1, -1):
    if(check(ans | (1 << i), ~((1 << i) - 1))):
       ans |= 1 << i
print(ans)



3.7

import sys


n, k = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]

cur = 0

def is_submask(n, mask):
    return (n & mask) == mask

def f(mask):
    s = [[False for j in range(n)] for i in range(n)]
    for l in range(n):
        cur = 0
        for r in range(l, n):
            cur += a[r]
            s[l][r] = is_submask(cur, mask)
    dp = [[False for j in range(n)] for i in range(k)]
    dp[0] = s[0][:]
    for k1 in range(1, k):
        for r in range(n):
            for l in range(1, r + 1):
                    dp[k1][r] |= dp[k1 - 1][l - 1] & s[l][r]
    return dp[k - 1][n - 1]

cur = 0
for i in range(56, -1, -1):
    if f(cur + 2 ** i):
        cur += 2 ** i
print(cur)