Reduction Game

4.2

5 votes
Algorithms, Depth First Search, Graphs, Medium
Problem

You are given an array A consisting of N distinct integers. If the size of the array is greater than one, then you can perform the following operations any number of times:

  • Select two integers Ai and Aj such that CountOnes(AiAj)=1 and delete either Ai or Aj from the array. This operation costs C1.

  • Select two integers Ai and Aj such that CountOnes(AiAj)>1 and delete either Ai or Aj from the array. This operation costs C2.

Here, CountOnes(X) returns the number of 1s available in the binary representation of integer X. Determine the minimum cost to reduce the size of the array to 1.

Input format

  • First line: T denoting the number of test cases
  • For each test case:
    • First line: N
    • Second line: Two space-separated integers C1 and C2
    • Third line: N space-separated integers denoting the array A

Output format

For each test case, print a single integer in a separate line as described in the problem statement

Constraints

1T20

1N104

1C1,C2,Ai109

Subtasks

  • For 10 points: 1N10
  • For 90 points: Implement the original constraints
Time Limit: 2
Memory Limit: 256
Source Limit:
Explanation

We take 1 and 3 and delete 1 with cost 50. Next we take 3 and 2 and delete 2 with cost 50. Last, we take 3 and 4 and delete 4 with cost 100.

So total cost = 50+50+100 = 200

Editor Image

?