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
def getMinCost ( arr , n , k ) : NEW_LINE INDENT arr . sort ( reverse = True ) NEW_LINE min_cost = 0 ; NEW_LINE X = 0 ; NEW_LINE for i in range ( 0 , n , k ) : NEW_LINE INDENT for j in range ( i , n , 1 ) : NEW_LINE INDENT if ( j < i + k ) : NEW_LINE INDENT min_cost += arr [ j ] * ( X + 1 ) ; NEW_LINE DEDENT DEDENT X += 1 ; NEW_LINE DEDENT return min_cost ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 9 , 20 , 7 , 8 ] ; NEW_LINE K = 2 ; NEW_LINE N = len ( arr ) ; NEW_LINE print ( getMinCost ( arr , N , K ) ) ; NEW_LINE DEDENT