Minimum time required to schedule K processes | Function to execute k processes that can be gained in minimum amount of time ; Stores all the array elements ; Push all the elements to the priority queue ; Stores the required result ; Loop while the queue is not empty and K is positive ; Store the top element from the pq ; Add it to the answer ; Divide it by 2 and push it back to the pq ; Print the answer ; Driver Code
def executeProcesses ( A , N , K ) : NEW_LINE INDENT pq = [ ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT pq . append ( A [ i ] ) NEW_LINE DEDENT ans = 0 NEW_LINE pq . sort ( ) NEW_LINE while ( len ( pq ) > 0 and K > 0 ) : NEW_LINE INDENT top = pq . pop ( ) NEW_LINE ans += 1 NEW_LINE K -= top NEW_LINE top //= 2 NEW_LINE pq . append ( top ) NEW_LINE pq . sort ( ) NEW_LINE DEDENT print ( ans ) NEW_LINE DEDENT A = [ 3 , 1 , 7 , 4 , 2 ] NEW_LINE K = 15 NEW_LINE N = len ( A ) NEW_LINE executeProcesses ( A , N , K ) NEW_LINE