Can someone help me find the problem in my code


#1

here is my code. my time limit is exceeding while submitting.
I know we could use heapq from collections. but shouldn’t this raw implementation work too with same time complexity. can someone point out my mistake.
Thanks

class Solution:
    # @param A : integer
    # @param B : list of integers
    # @return an integer
    def nchoc(self, A, B):
        def heapify(i):
            if 2*i+1<len(B) and B[2*i+1]>B[i]:
                largest=2*i+1
            else:
                largest=i
            if 2*i+2<len(B) and B[2*i+2]>B[largest]:
                largest=2*i+2
            if largest!=i:
                B[largest],B[i]=B[i],B[largest]
                heapify(largest)
        e=(len(B)-2)//2
        while e>=0:
            heapify(e)
            e-=1
        s=0    
        for i in range(A):
            s+=B[0]
            B[0]=B[0]//2
            heapify(0)
        return s % (1000000007)