Simple C++ sol using Max heap


#1

int Solution::solve(vector &A, int B) {
priority_queue heap;
for(int i = 0; i<A.size(); i++)
{
heap.push(A[i]);
}
int ans = 0;
for(int i = 0; i<B; i++)
{
int x = heap.top();
ans += x;
heap.pop();
x–;
if(x>0)
{
heap.push(x);
}
}
return ans;
}