1487. BITWISE EXPRESSIONS

单点时限: 2.0 sec

内存限制: 256 MB

In signal processing, one is sometimes interested in the maximum value of a certain expression, containing bitwise AND and OR operators, when the variables are integers in certain ranges. You are to write a program that takes such an expression and the range of each variable as input and determines the maximum value that the expression can take.

For simplicity, the expression is of a specific form, namely a number of subexpressions in parentheses separated by the bitwise AND operator (denoted &).

Each subexpression consists of one or more variables separated by the bitwise OR operator (denoted |). Using this convention, it is possible to completely specify the expression by giving the number of subexpressions and, for each subexpression, the number of variables in the subexpression. The variables are simply numbered according to their occurrence in the expression.

An example will clarify this. If the number of subexpressions is 4 and the number of variables in each subexpression is 3, 1, 2, and 2, then the expression will be E = (v1 | v2 | v3) & (v4) & (v5 | v6) & (v7 | v8) The bitwise operators are defined in the common way. For example, to perform the operation 21 & 6, we first write down the binary form of the numbers (operands):10101 and 110 (since 21 = 2^4 + 2^2 + 2^0 and 6 = 2^2 + 2^1). Every binary digit in the result now depends on the corresponding digit in the operands: if it is 1 in both operands, the resulting digit will be one, otherwise it will be zero. As is illustrated below to the left,

the resulting value is 4. If instead we want to calculate 21 | 6, the procedure is the same except that the resulting digit will be one if the corresponding digit is one in any of the operands, and thus it will be zero only in the case that the digit is zero in both operands. As is illustrated below in the center, the result is 23. The generalization to more than two operands is straightforward. The rightmost example below illustrates that 30 & 11 & 7 = 2.

+-------------------------------+

|_______11110 |

|1010110101___01011 |

|&_00110_| 00110& 00111 |

|-------_-------------- |

|0010010111___00010 |

+-------------------------------+

输入格式

In the first line, two integers N and P are given, where N is the total number of variables (1 <= N <= 100) and P is the number of subexpressions (1 <= P <= N). In the next line, P integers (K1, K2, … ,KP) are given, where Ki is the number of variables in the i-th subexpression. The Ki are all greater than or equal to 1 and their sum equals N. Each of the following N lines contains two integers Aj and Bj (0 <= Aj <= Bj <= 2000000000), specifying the range of the j-th variable in the expression according to Aj <= vj <= Bj.

输出格式

The content should be one row with a single integer: the maximum value that the expression can take.

样例

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

1 人解决,3 人已尝试。

1 份提交通过,共有 25 份提交。

9.9 EMB 奖励。

创建: 16 年,9 月前.

修改: 6 年,8 月前.

最后提交: 3 年,5 月前.

来源: BOI 2006

题目标签