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