#P7637. Composite Bitwise Expression Evaluation

    ID: 20830 Type: Default 1000ms 256MiB

Composite Bitwise Expression Evaluation

Composite Bitwise Expression Evaluation

You are given a specially structured expression comprising several subexpressions. Each subexpression is enclosed in parentheses and the subexpressions are separated by a bitwise AND operator (denoted as \(&\)). Within each subexpression, one or more variables are combined using the bitwise OR operator (denoted as \(|\)).

The expression is completely specified by two pieces of data:

  • The number of subexpressions, \(n\).
  • An array of \(n\) positive integers where the \(i\)-th integer indicates the number of variables in the \(i\)-th subexpression.

The variables are numbered sequentially based on their appearance in the expression. For example, if \(n = 4\) and the number of variables in each subexpression is given as \(3, 1, 2, 2\) respectively, then the expression is:

[ E = (v_1 \mid v_2 \mid v_3) ;&; (v_4) ;&; (v_5 \mid v_6) ;&; (v_7 \mid v_8) ]

The bitwise operators are defined in the usual way. For instance:

  • To compute \(21 \;\&\; 6\), convert the numbers to binary \(10101\) and \(110\) respectively, then the result is computed by taking the AND of each corresponding bit.
  • To compute \(21 \mid 6\), take the OR of the corresponding bits of the numbers.

This concept easily generalizes to expressions with more than two operands. For example, \(30 \;\&\; 11 \;\&\; 7 = 2\).

Your task is to evaluate the given composite expression as follows:

  1. For each subexpression, compute the bitwise OR of its variables.
  2. Compute the overall value by taking the bitwise AND of all the resulting values from step 1.

You will be provided with:

  • An integer \(n\): the number of subexpressions.
  • A list of \(n\) positive integers specifying the count of variables in each subexpression.
  • A list of variable values. The variables are provided in order of appearance.
  • </p>

    inputFormat

    The input consists of three lines:

    1. The first line contains a single integer \(n\) \( (1 \le n \le 10^5)\), the number of subexpressions.
    2. The second line contains \(n\) space-separated positive integers, where the \(i\)-th integer represents the number of variables in the \(i\)-th subexpression. The sum of these integers does not exceed \(10^5\).
    3. The third line contains the corresponding variable values as space-separated integers. Each variable value is in the range \(0 \le v \le 10^9\). They are provided in the order they appear in the expression.

    outputFormat

    Print a single integer representing the value of the expression computed as the bitwise AND of the bitwise OR's of each subexpression.

    sample

    4
    3 1 2 2
    1 2 4 5 3 8 12 7
    1