Using priority queue ( Max heap)


#1
int Solution::solve(vector<int> &a, int b) {
    
    priority_queue<int,vector<int> > pq;
    int i,j,n,num;
    n=a.size();
    for(i=0;i<n;i++)pq.push(a[i]);
    int ans=0;
    while(b--)
    {
        num=pq.top();
        pq.pop();
        ans+=num;
        if(num>1)
        pq.push(num-1);
    }
    return ans;
}