Monk's Choice of Numbers : HackerEarth Problem Solution

Monk loves cakes! He visits the Binary Bakery to buy some of his favorite cheesecakes. 
The owner of the bakery, Bob, is a clever man. He does not want Monk to finish all his cheesecakes. Hence, he plays a game.
The Monk is given N numbers and has to select K of these numbers. For each number that Monk chooses, he will get as many cheesecakes as the number of 1's in the Binary representation of the number i.e. the number of bits that are set.
Help Monk find the maximum number of cakes that he can have.


Input:
The first line of input contains T. T test cases follow.
First line of each test cases contains 2 space-separated integers N and K.
The next line contains N space-separated integers.

Output:
For each test cases, print the answer in a new line.

Constraints:
1 ≤ T ≤ 10
1 ≤ N ≤ 103
0 ≤ K ≤ N
0 ≤ Numbers ≤ 105

Sample Input

1
4 2
6 1 2 0

Sample Output 

3

Explanation

He chooses numbers 6 (110) and 1 (001) with 2 and 1 set bits respectively.

C++ Implementation:

#include <iostream>
#include <queue>
using namespace std;

int findones(int n)
{
 int count = 0;
 while(n)
 {
  if(n&1)
   count++;
  n>>=1;
 }
 return count;
}

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
     priority_queue<int> pq;
     int n,k;
     cin>>n>>k;
     while(n--)
     {
      int x;
      cin>>x;
      pq.push(findones(x));
     }
     int sum = 0;
     while(k--)
     {
      sum += pq.top();
      pq.pop();
     }
     cout<<sum<<endl;
    }
    return 0;
}