human
What is a code to minimize cost to split an array into K subsets such that the cost of each element is its product with its position in the subset and the other steps required given the following:
Minimize cost to split an array into K subsets such that the cost of each element is its product with its position in the subset | Function to find the minimum cost to split array into K subsets.
Sort the array in descending order.
Stores minimum cost to split the array into K subsets.
Stores position of elements of a subset.
Iterate over the range [ 1 , N ].
Calculate the cost to select X - th element of every subset.
Update min_cost.
Update X.
Driver code.
Function call"
Solution in Python:
assistant
#Here is code to minimize cost to split an array into K subsets such that the cost of each element is its product with its position in the subset and the other steps required.
 def getMinCost ( arr , n , k ) :
	arr . sort ( reverse = True )
	min_cost = 0 ;
	X = 0 ;
	for i in range ( 0 , n , k ) :
		for j in range ( i , n , 1 ) :
			if ( j < i + k ) :
				min_cost += arr [ j ] * ( X + 1 ) ;
		X += 1 ;
	return min_cost ;
if __name__ == ' _ _ main _ _ ' :
	arr = [ 9 , 20 , 7 , 8 ] ;
	K = 2 ;
	N = len ( arr ) ;
	print ( getMinCost ( arr , N , K ) ) ;