OR Score of a Sequence

Split the array into K contiguous non-empty groups and maximize the sum of each group's bitwise OR.

Medium6Dynamic programmingBit manipulationPrefix sumNo attempts yetTime limit2sMemory limit512 MB

Problem

You are given a sequence AA of size NN and an integer KK. Split the sequence into KK contiguous non-empty groups. Every element of the sequence must belong to exactly one group.

Each group is written as two integers LL and RR, meaning that the LL-th number through the RR-th number belong to that group. The score of a group is the bitwise OR of every element in it.

The OR score of the sequence is the sum of the scores of all groups.

Write a program that splits the sequence into KK contiguous non-empty groups and finds the maximum OR score of the sequence.

Input

The first line contains NN and KK. (1N50001 \le N \le 5\,000, 1KN1 \le K \le N)

The second line contains the elements of the sequence A1,A2,,ANA_1, A_2, \dots, A_N, separated by spaces. (0Ai2300 \le A_i \le 2^{30})

Output

Print the maximum OR score of the sequence.

Hint

In the first example, splitting into (1,2)(1, 2) and (2)(2) scores (1 OR 2)+2=3+2=5(1 \text{ OR } 2) + 2 = 3 + 2 = 5.

In the second example, splitting into (1,2)(1, 2), (3)(3), (4)(4) scores (1 OR 2)+3+4=3+3+4=10(1 \text{ OR } 2) + 3 + 4 = 3 + 3 + 4 = 10.

In the third example, the only possible split is (1)(1) and (2)(2).