Easiest CPP solution......using priority queue


#1

int Solution::solve(vector &A, int B) {
priority_queue pq(A.begin(),A.end());
int sum=0;
while(B–)
{
int x=pq.top();
pq.pop();
sum=sum+x;
x–;
pq.push(x);
}
return sum;
}