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 Go (1.6) (judge verdict: Wrong Answer)

package main

import (
	"bufio"
	"fmt"
	"log"
	"os"
	"sort"
	"strconv"
)

func main() {
	log.SetFlags(log.Lshortfile)
	N := nextInt()
	K := nextInt()
	a := nextInts(N)
	b := make([]int, N)
	b[0] = a[0]
	for i := 1; i < N; i++ {
		b[i] = a[i] + b[i-1]
	}
	// log.Println(b)
	c := make([]int, 0)
	idx := 0
	for i := 0; i < N; i++ {
		for j := i; j < N; j++ {
			var v int
			if i > 0 {
				v = b[j] - b[i-1]
			} else {
				v = b[j]
			}
			c = append(c, v)
			idx++
		}
	}
	sort.Ints(c)
	min := c[len(c)-1-K]
	minsize := len(strconv.FormatInt(int64(min), 2))
	// log.Printf("%d %b %d\n", min, min, len(strconv.FormatInt(int64(min), 2)))
	d := make([]string, len(c))
	for i, v := range c {
		s := strconv.FormatInt(int64(v), 2)
		b := []byte(s)
		if len(b) > minsize {
			b = b[len(b)-minsize:]
		}
		d[i] = string(b)
	}
	// sort.Strings(d)
	// log.Println(d)
	e := make([]int, len(d))
	for i, v := range d {
		j, err := strconv.ParseInt(v, 2, 64)
		if err != nil {
			log.Fatal(err)
		}
		// log.Println(v, j)
		e[i] = int(j)
	}
	sort.Ints(e)
	// log.Println(c)
	rtn := uint64(e[len(e)-1])
	for i := 0; i < K; i++ {
		rtn &= uint64(e[len(e)-1-i])
		// log.Println(rtn)
	}
	fmt.Println(rtn)
	// for i, v := range c {
	// 	log.Printf("%d %d %b\n", i, v, v)
	// }
}
func Max(a ...int) int {
	r := a[0]
	for i := 0; i < len(a); i++ {
		if r < a[i] {
			r = a[i]
		}
	}
	return r
}
func Min(a ...int) int {
	r := a[0]
	for i := 0; i < len(a); i++ {
		if r > a[i] {
			r = a[i]
		}
	}
	return r
}
func Sum(a []int) (r int) {
	for i := range a {
		r += a[i]
	}
	return r
}
func Abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}

type Pair struct {
	a, b int
}

type Pairs []Pair

func (p Pairs) Len() int {
	return len(p)
}
func (p Pairs) Swap(i, j int) {
	p[i], p[j] = p[j], p[i]
}

func (p Pairs) Less(i, j int) bool {
	return p[i].b < p[j].b
}

var nextReader func() string

func init() {
	nextReader = NewScanner()
}

func NewScanner() func() string {
	r := bufio.NewScanner(os.Stdin)
	r.Buffer(make([]byte, 1024), int(1e+11))
	r.Split(bufio.ScanWords)
	return func() string {
		r.Scan()
		return r.Text()
	}
}
func nextString() string {
	return nextReader()
}
func nextInt64() int64 {
	v, _ := strconv.ParseInt(nextReader(), 10, 64)
	return v
}
func nextInt() int {
	v, _ := strconv.Atoi(nextReader())
	return v
}
func nextInts(n int) []int {
	r := make([]int, n)
	for i := 0; i < n; i++ {
		r[i] = nextInt()
	}
	return r
}
func nextFloat64() float64 {
	f, _ := strconv.ParseFloat(nextReader(), 64)
	return f
}