#P7853. Maximizing Binary Addition Flip Costs
Maximizing Binary Addition Flip Costs
Maximizing Binary Addition Flip Costs
You are given a binary number (B) (represented as a 01
string of length (n)) and a sequence (a) of length (m). You will perform (m) operations on (B). In the (i)th operation, you update (B) as follows:
[ B \gets B + 2^{a_i}, ]
and define its value (or cost) by
[ v_i = \operatorname{popcount}(B \oplus (B+2^{a_i})), ]
where (\operatorname{popcount}(x)) denotes the number of 1’s in the binary representation of (x) and (\oplus) denotes bitwise XOR. You are allowed to execute the operations in any order. Your tasks are to compute:
-
The maximum possible total cost, i.e. the maximum attainable value of (\displaystyle \sum_{i=1}^m v_i) after executing all operations in some order.
-
The maximum possible single–operation cost, i.e. the maximum attainable value of (\displaystyle \max_{1\le i\le m} v_i) over all operations when the operations are performed in an optimal order.
In other words, by reordering the operations arbitrarily you wish to maximize both the sum of all (v_i) and the maximum individual (v_i}.
inputFormat
The input consists of three lines. The first line contains two integers (n) and (m) separated by a space. The second line contains a binary string (B) of length (n). The third line contains (m) integers (a_1, a_2, \ldots, a_m) separated by spaces, where each (a_i) represents the exponent in the corresponding operation.
outputFormat
Output two space–separated integers on a single line: the maximum possible total cost (\left(\sum_{i=1}^m v_i\right)) and the maximum possible individual operation cost (\left(\max_{1\le i\le m} v_i\right)).
sample
1 3
0
0 0 0
4 2