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

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include<bits/stdc++.h>
using namespace std;
 
#define rep(i,m,n) for(int i = (m); i < (int)(n); i++)
#define rrep(i,m,n) for(int i=(int)(m); i>=(int)(n); i--)
#define vsort(v) sort(v.begin(), v.end());
#define rvsort(v) sort(v.begin(), v.end(),greater<int>());
#define YES cout<<"YES"<< endl
#define NO cout<<"NO"<<endl
#define Yes cout<<"Yes"<<endl
#define No cout<<"No"<<endl  
#define yes cout<<"yes"<<endl
#define no cout<<"no"<<endl
#define ret return
#define lb(v,n) lower_bound(v.begin(),v.end(),n)
#define ub(v,n) upper_bound(v.begin(),v.end(),n)
#define mae(v) max_element(v.begin(),v.end())
#define mie(v) min_element(v.begin(),v.end())
#define INF 0x7FFFFFFF
#define mod 1000000007
#define fir first
#define sec second
typedef long long ll;
////////////////////////////////////////////////////////////
int main(){
	int n,K;
	cin >> n >> K;
	vector<ll> a(n);
	rep(i,0,n) cin >> a[i];

	ll s[1001][1001] = {};

	
	vector<ll> num;
	rep(i,0,n){
		s[0][i] = a[i];
		num.push_back(a[i]);
	}


	rep(i,1,n){
		rep(j,0,n-i){
			s[i][j] = s[i-1][j] + a[j+i];
			num.push_back(s[i][j]);
		}
	}

	/*
		 rep(i,0,n){
		 rep(j,0,n) cout << s[i][j] << " ";
		 cout << endl;
		 }
		 */

	rvsort(num);


	ll che[50];
	che[0] = 1;
	rep(i,1,50) che[i] = che[i-1] * 2; 

	int start = 50;

	while(1){
		int cnt[50] = {};
		rep(i,0,num.size()){
			rep(j,0,start){
				if( (num[i] & che[j]) == che[j] ) cnt[j]++;
			}
		}
		//rep(i,0,start) cout << che[i] << ":" << cnt[i] << endl;
		ll check = -1;
		for(int i = start-1; i >= 0 ; i-- ){
			if(cnt[i] >= K ){
				check = che[i];
				start = i;
				break;
			}
		}

		if( check == -1){
			break;
		}
		//cout << "check:" << check << endl;
		vector<ll> t;
		rep(i,0,num.size()){
			if( (num[i] & check) == check ){
				t.push_back(num[i]);
			}
		}

		if( t.size() == K ) break;
		else num = t;
	}
	
	ll ans = num[0];
	rep(i,1,K) ans = ans & num[i];
	cout << ans << endl;

	ret 0;
}