#P7637. [BalticOI 2006] BITWISE EXPRESSIONS (Day 1)

[BalticOI 2006] BITWISE EXPRESSIONS (Day 1)

Description

For simplicity, the expression has a specific form: it consists of several subexpressions in parentheses, separated by the bitwise AND operator (denoted by &\&). Each subexpression consists of one or more variables, separated by the bitwise OR operator (denoted by |). With this convention, the expression can be fully specified by giving the number of subexpressions and the number of variables in each subexpression. Variables are numbered only according to their occurrences in the expression.

For example, if the number of subexpressions is 44, and the numbers of variables in each subexpression are 33, 11, 22, 22, then the expression is:

$$E=(v_1|v_2|v_3) \& (v_4) \& (v_5|v_6) \& (v_7|v_8)$$

Bitwise operators are defined in the usual way. For example, to compute 21&621 \& 6, we first write the numbers (operands) in binary: 1010110101 and 110110 (because 21=24+22+2021=2^4+2^2+2^0 and 6=22+216=2^2+2^1). Each binary digit of the result now depends on the corresponding digits of the operands: if both operands have 11, then the result digit is 11, otherwise it is 00. As shown on the left in the figure below, the result is 44. If we want to compute 21621| 6, the process is the same, except that if the corresponding digit is 11 in any operand, then the result is 11. Therefore, the result digit is 00 only when both operands have 00. As shown in the middle of the figure below, the result is 2323. Extending this to more than two operands is straightforward. The example on the right below shows that 30&11&7=230 \& 11 \& 7=2. TuLi

Input Format

The first line contains two positive integers NN and PP. The second line contains PP positive integers (K1,K2,,KPK_1,K_2, \cdots ,K_P), where KiK_i is the number of variables in the ii-th subexpression. Each KiK_i is at least 11, and their sum equals NN. The next NN lines each contain two integers AjA_j and BjB_j, specifying the value range of the jj-th variable in the expression by AjvjBjA_j \le v_j \le B_j.

Output Format

One line containing one integer: the maximum value the expression can take.

8 4
3 1 2 2
2 4
1 4
0 0
1 7
1 4
1 2
3 4
2 3
6

Hint

Constraints

For 100%100 \% of the testdata, 1N1001 \le N \le 100, 1PN1 \le P \le N, 0AjBj2×1090 \le A_j \le B_j \le 2×10^9.

Sample Explanation

The sample restricts the values of the eight variables in the expression as follows: 2v142 \le v_1 \le 4, 1v241 \le v_2 \le 4, v3=0v_3=0, 1v471 \le v_4 \le 7, 1v541 \le v_5 \le 4, 1v621 \le v_6 \le 2, 3v743 \le v_7 \le 4, and 2v832 \le v_8 \le 3. If written in binary, one of the best assignments gives the expression (100011000)&(111)&(100010)&(100011)(100|011|000) \& (111) \& (100|010) \& (100|011). From this we can see that all subexpressions can become equal to 77 except the third one.

Problem Notes

From Baltic Olympiad in Informatics 2006 Day 1:Bitwise.
Translated and organized by @求学的企鹅.

Translated by ChatGPT 5