Compatibility Queries

3.7

6 votes
1-D, Algorithms, Dynamic Programming, Bitmask, Dynamic Programming and Bit Masking
Problem

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

1N1061Ai1061Q1061X106
 

Sample Input
3
1 2 3
3
1
2
3
Sample Output
2
1
0
Time Limit: 2
Memory Limit: 256
Source Limit:
Explanation

The array is [1,2,3]. There are 3 queries.

  • In the first query, only 2 from the array is compatible with 1 so the answer for the first query is 2.
  • In the second query, only 1 from the array is compatible with 2 so the answer for the second query is 1.
  • In the third query, no element from the array is compatible with 3 so the answer for the third query is 0.
Editor Image

?