human
Minimize Sum of an Array by at most K reductions | Function to obtain the minimum possible sum from the array by K reductions.
.
Insert elements into the MaxHeap.
Remove the maximum.
Insert maximum / 2.
Stores the sum of remaining elements.
Driver code"
Solution in Python:
 ... Give me a code to minimize Sum of an Array by at most K reductions and the other steps required?
assistant
#Here is code to minimize Sum of an Array by at most K reductions and the other steps required.
 def minSum ( a , n , k ) :
' 	 	 Implements ▁ the ▁ MaxHeap ' ' '
	q = [ ]
	for i in range ( n ) :
		q . append ( a [ i ] )
	q = sorted ( q )
	while ( len ( q ) > 0 and k > 0 ) :
		top = q [ - 1 ] // 2
		del q [ - 1 ]
		q . append ( top )
		k -= 1
		q = sorted ( q )
	sum = 0
	while ( len ( q ) > 0 ) :
		sum += q [ - 1 ]
		del q [ - 1 ]
	return sum
if __name__ == ' _ _ main _ _ ' :
	n = 4
	k = 3
	a = [ 20 , 7 , 5 , 4 ]
	print ( minSum ( a , n , k ) )