Baltic Olympiad in Informatics: 2006 Day 1, Problem 1
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
and the number of
variables in each subexpression is
,
,
, and
, then the expression will be

The bitwise operators are defined in the common way. For example, to perform the
operation
, we first write down the binary form of the numbers (operands):
and
(since
and
). Every binary digit in the result
now depends on the corresponding digit in the operands: if it is
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
. If instead we want to calculate
, 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
. The generalization to
more than two operands is straightforward. The rightmost example below illustrates
that
.
Copy
11110
10101 10101 01011
& 00110 | 00110 & 00111
------- ------- -------
00100 10111 00010
Input
In the first line, two integers
and
are given, where
is the total number of variables
and
is the
number of subexpressions
. In the next line,
integers
are
given, where
is the number of variables in the
-th subexpression. The
are all
greater than or equal to
and their sum equals
. Each of the following
lines
contains two integers
and
, specifying the range of
the
-th variable in the expression according to
.
Output
The output should be one row with a single integer: the maximum value that the expression can take.
Example
Assume that we want to limit the values of the eight variables in the expression above according to
,
,
,
,
,
,
, and
.
Sample Input
Copy
8 4
3 1 2 2
2 4
1 4
0 0
1 7
1 4
1 2
3 4
2 3
If writing in binary notation one of the best assignments gives the expression
, from which we note that all subexpressions may become equal to
except the third.
Sample Output
Copy
6
Grading
In
of the test cases, the number of possible assignments is less than one million.
Comments