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

#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
#include <set>
using namespace std;
#define ll long long

int main () {
  ll n, K;
  cin >> n >> K;
  ll a[n];
  ll sum[n + 1];
  sum[0] = 0;
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
    sum[i + 1] = sum[i] + a[i];
  }
  vector<ll> ans;
  for (ll i = 0; i < n + 1; i++) {
    for (ll j = i + 1; j < n + 1; j++) {
      ans.push_back(sum[j] - sum[i]);  
    }  
  }
  ll sub = 1;
  ll itr = 0;
  ll dp[40];
  for (int i = 0; i < 40; i++) {
    dp[i] = 0;  
  }
  vector<map<ll, ll> > box;
  box.resize(40);
  while (itr < 41) {
    for (ll i = 0; i < ans.size(); i++) {
      if ((sub & ans[i]) != 0) {
        dp[itr]++;
        box[itr][i] = 1;  
      }    
    }  
    sub = sub * 2;
    itr++;  
  }
  sub = 1;
  for (int i = 0; i < 40; i++) {
    /*
    cout << sub << " " << dp[i] << endl;
    for (auto x = box[i].begin(); x != box[i].end(); ++x) {
      cout << *x << " ";  
    }
    cout << endl;
    */
    sub = sub * 2;  
  }
  ll answer = 0;
  ll ssss = 0;
  bool ok = false;

  for (ll i = 0; i < 40; i++) {
    sub = sub / 2;
    if (ok == false) {
      if (box[39 - i].size() >= K) {
        answer = answer + sub;
        ok = true;
        itr = 39 - i;
      }
    }
    else {
      ssss = 0;
      if (box[39 - i].size() >= K) {
        for (auto j = box[39 - i].begin(); j != box[39 - i].end(); ++j) {
          if (box[itr][j->first] == 1) {
              ssss++;      
          } 
          else {
            box[39 - i][j->first] = 0;    
          }
        }  
        if (ssss >= K) {
          answer = answer + sub;  
          itr = 39 - i;
        }
        else {
          // 何もしない.    
        }
      }     
    }
  }
  cout << answer << endl;
}