Minimum cost of choosing the array element | Function that find the minimum cost of selecting array element ; Sorting the given array in increasing order ; To store the prefix sum of arr [ ] ; Update the pref [ ] to find the cost selecting array element by selecting at most M element ; Print the pref [ ] for the result ; Driver Code
def minimumCost ( arr , N , M ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE pref = [ ] NEW_LINE pref . append ( arr [ 0 ] ) NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT pref . append ( arr [ i ] + pref [ i - 1 ] ) NEW_LINE DEDENT for i in range ( M , N ) : NEW_LINE INDENT pref [ i ] += pref [ i - M ] NEW_LINE DEDENT for i in range ( N ) : NEW_LINE INDENT print ( pref [ i ] , end = ' ▁ ' ) NEW_LINE DEDENT DEDENT arr = [ 6 , 19 , 3 , 4 , 4 , 2 , 6 , 7 , 8 ] NEW_LINE M = 2 NEW_LINE N = len ( arr ) NEW_LINE minimumCost ( arr , N , M ) ; NEW_LINE