#P10911. [蓝桥杯 2024 国 B] 数位翻转

[蓝桥杯 2024 国 B] 数位翻转

Problem Description

Xiaoming created a function f(x)f(x) to reverse the binary digits of xx (with no leading 00). For example, f(11)=13f(11) = 13, because 11=(1011)211 = (1011)_2, and after reversing it left to right, it becomes 13=(1101)213 = (1101)_2. Another example: f(3)=3f(3) = 3, f(0)=0f(0) = 0, and f(2)=f(4)=f(8)=1f(2) = f(4) = f(8) = 1, and so on.

Xiaoming randomly generated an integer array {a1,a2,,an}\{a_1, a_2, \cdots, a_n\} of length nn. He wants to know: in this array, if you choose at most mm disjoint intervals and reverse the binary digits of the numbers inside these intervals (change aia_i to f(ai)f(a_i)), what is the maximum possible sum of the entire array?

Input Format

The input has 22 lines.

The first line contains two positive integers n,mn, m.

The second line contains nn integers a1,a2,,ana_1, a_2, \cdots, a_n separated by spaces.

Output Format

Output 11 line containing one integer, which is the answer.

5 3
11 12 13 14 15
67
6 2
23 8 11 19 16 35
134

Hint

[Sample Explanation 1]

Flip only a1a_1, and the sum is 13+12+13+14+15=6713 + 12 + 13 + 14 + 15 = 67.

[Sample Explanation 2]

Flip intervals [a3,a4][a_3, a_4] and [a6][a_6], and the sum is 23+8+13+25+16+49=13423 + 8 + 13 + 25 + 16 + 49 = 134.

[Constraints]

For 20%20\% of the testdata, it is guaranteed that n,m20n, m \le 20.
For 100%100\% of the testdata, it is guaranteed that n,m1000n, m \le 1000, and 0ai1090 \le a_i \le 10^9.

Translated by ChatGPT 5