Minimum addition

3.9

67 votes
Basic Programming, Bit Manipulation
Problem

You are given an array of positive numbers. You are also given  queries. Each query contains two integers . For each query, determine the sum of all values that must be added to each number in range to  such that bitwise AND of all numbers in the range is greater than 0.

You are required to minimize the total value that must be added. The value you must add to numbers is not necessarily equal for all numbers.

Note: Each query is independent of each other, that is, for each query, you must treat the range over the initial input array.

Input format

  • The first line contains an integer  denoting the number of array elements.
  • The next line denotes  array elements.
  • The next line contains a number  denoting the number of queries.
  • Next  lines contain two integers  and .

Output format

For each query, print the minimum sum of values to be added in a new line.

Constraints

 

Sample Input
5
4 3 2 4 1
4
1 4
2 3
4 4
4 5
Sample Output
3
0
0
1
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation
  • For the first query we can add 1 to the 1st, 3rd and, 4th numbers.
  • For the second and third query, we don't need to add as the Bitwise AND is already > 0.
  • For the last query, we can add 1 to 4th number.

 

Editor Image

?