C++ solution using MaxHeap (Time Complexity: O(Alogn))


#1
int Solution::nchoc(int A, vector<int> &B) {
    long long sum = 0;
    priority_queue<int> maxheap;
    for(int i=0;i<B.size();i++)
    {
        maxheap.push(B[i]);
    }
    for(int i=0;i<A;i++)
    {
        long long top = maxheap.top();
        sum = sum + top;
        maxheap.pop();
        maxheap.push(floor(top/2));
    }
    sum = sum % (long long)(pow(10,9)+7);
    return sum;
}

#2

This is an “nlogn” solution not “Alogn”. You are pushing all elements to a priority queue which takes “nlogn” time.