Compatibility Queries
Practice
3.7 (6 votes)
1 D
Algorithms
Dynamic programming
Bitmask
Dynamic programming and bit masking
Problem
80% Success 1663 Attempts 30 Points 2s Time Limit 256MB Memory 1024 KB Max Code

Any integer \(A\) is said to be compatible with  another integer \(B\) if \(A|B=A+B\) where \(A|B\) is the Bitwise OR of \(A\) and \(B\).

You are given an array \(A\) of \(N\) integers. You are also given \(Q\) queries where each query has a single integer \(X\). For each query find the sum of all the array elements which are compatible with \(X\).

Input format

  • The first line contains a single integer \(N\).
  • The second line, contains \(N\) space-separated integers, the array elements. 
  • The third line contains a single integer \(Q\).
  • The next \(Q\) lines contain a single integer each, the value of \(X\).

Output format

Print the answer for each query in a separate line — the sum of all the array elements which are compatible with \(X\).

Constraints

\(1 \leq N \leq 10^6 \\ 1 \leq A_i \leq 10^6 \\ 1 \leq Q \leq 10^6 \\ 1 \leq X \leq 10^6\)
 

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:30
3 votes
Tags:
Dynamic ProgrammingDynamic Programming and Bit MaskingIntroduction to Dynamic Programming 1AlgorithmsBitmask1-D
Points:30
68 votes
Tags:
Bit ManipulationBit manipulationDynamic ProgrammingMediumOpen
Points:30
2 votes
Tags:
Breadth-first searchAlgorithmsDynamic Programming and Bit MaskingBrute-force searchStringDynamic ProgrammingBitmask