human
Write me a code to minimum cost to form a number X by adding up powers of 2 and the other steps required given the following:
Minimum cost to form a number X by adding up powers of 2 | Function to return the minimum cost.
Re - compute the array.
Add answers for set bits.
If bit is set.
Increase the counter.
Right shift the number.
Driver code"
Solution in Python:
assistant
#Here is code to minimum cost to form a number X by adding up powers of 2 and the other steps required.
 def MinimumCost ( a , n , x ) :
	for i in range ( 1 , n , 1 ) :
		a [ i ] = min ( a [ i ] , 2 * a [ i - 1 ] )
	ind = 0
	sum = 0
	while ( x ) :
		if ( x & 1 ) :
			sum += a [ ind ]
		ind += 1
		x = x >> 1
	return sum
if __name__ == ' _ _ main _ _ ' :
	a = [ 20 , 50 , 60 , 90 ]
	x = 7
	n = len ( a )
	print ( MinimumCost ( a , n , x ) )