#P5144. Centipede Partitioning: Maximizing Disgust
Centipede Partitioning: Maximizing Disgust
Centipede Partitioning: Maximizing Disgust
On a winding mountain road, WYH finds a centipede with N segments as thick as a middle finger. HKE instantly falls in love with the quirky creature despite its many toxic, wavy legs. However, MZL, an avid animal anatomist, intends to cut the centipede. To console HKE, MZL promises to only cut the centipede into M parts such that each part consists of one or several contiguous segments from the original centipede.
Each segment of the centipede carries a weight Wi (for 1 ≤ i ≤ N). The disgust value that HKE gets from a part composed of consecutive segments (Wi, Wi+1, …, Wj) is defined as
\( W_i \oplus W_{i+1} \oplus \cdots \oplus W_j \)
where \(\oplus\) denotes the bitwise XOR operation. The total disgust value is the sum of the disgust values for each of the M parts. Evil LJC wants HKE to feel as disgusted as possible. Your task is to help compute the maximum total disgust value HKE can get by choosing an optimal way to partition the centipede into exactly M contiguous parts.
Note: The XOR operator is denoted by xor
in Pascal and ^
(or xor
) in C++.
inputFormat
The first line contains two integers N and M (1 ≤ M ≤ N), representing the number of segments and the number of parts, respectively.
The second line contains N integers W1, W2, …, WN (0 ≤ Wi ≤ 109), which are the weights of the centipede's segments.
outputFormat
Output a single integer — the maximum total disgust value (i.e. the sum of the XOR values of each part) that can be achieved by partitioning the centipede into M contiguous parts.
sample
5 2
1 2 3 4 5
9