#P9365. Maximize Expression Value with Bitwise Operators

    ID: 22518 Type: Default 1000ms 256MiB

Maximize Expression Value with Bitwise Operators

Maximize Expression Value with Bitwise Operators

SolarPea is up to his old tricks again – this time, he wants to blow up PolarSea’s blog by constructing an arithmetic expression that produces a huge number. In the spirit of power towers, consider the following intimidating formula in ( \LaTeX ) format:

[ 2 ^ {2 ^ {2 ^ {2 ^ {2 ^ {2 ^ {2 ^ {2 ^ {2 ^ {2}}}}}}}}} ]

In this problem, you are given a multiset of powers of two, (a_1, a_2, \ldots, a_n), and three types of bitwise operators: AND, OR, and XOR. You are also given three integers (x), (y) and (z) representing the number of bitwise AND, OR, and XOR operators available, respectively. It is guaranteed that (n = x + y + z).

You can arrange the numbers in any order. Form an expression defined as follows: let (x_0 = 0) and for (i = 1,2,\ldots,n) define

[ x_i = x_{i-1} ; \mathrm{op}_i ; b_i, ]

where (b = (b_1, b_2, \ldots, b_n)) is a permutation of the original numbers (a) and each (\mathrm{op}_i) is one of the three bitwise operators. Your goal is to construct an expression so that the final result (x_n) is as large as possible. If there are multiple valid expressions, output any one of them.

Note that the operators are applied sequentially (from left to right) regardless of usual precedence rules. For instance, if you output an expression of the form

[ 0 ; op_1 ; b_1 ; op_2 ; b_2 ; \cdots ; op_n ; b_n, ]

you must use exactly (x) AND operators, (y) OR operators and (z) XOR operators.

Can you help SolarPea maximize (x_n) and construct such an expression?

inputFormat

The first line contains an integer (T) (the number of test cases). Each test case is described as follows:

The first line of each test case contains three non‐negative integers (x), (y), and (z) (with (n = x+y+z)). The second line contains (n) space‐separated integers (a_1, a_2, \ldots, a_n), each a power of two.

outputFormat

For each test case, output two lines. The first line contains the maximum possible value of (x_n). The second line should contain a valid expression that yields that value. The expression must start with 0 and then, for each operator, print a space followed by the operator (use & for AND, | for OR, and ^ for XOR), another space, and then the number. For example, an expression might look like:

0 & 2 ^ 4 | 8

If there are multiple solutions, output any one of them.

sample

1
1 1 0
2 4
6

0 & 2 | 4

</p>