I worked through the Flowers challenge on hackerrank.com and would like to get feedback on solution and algorithm implementation. It was suggested by hackerrank to review greedy algorithm to solve this challenge. As far as iI understand this is an implementation of greedy algorithm.
You and your K−1\$K−1\$ friends want to buy N\$N\$ flowers. Flower number i\$i\$ has cost ci cost \$c_i\$. Unfortunately the seller does not want just one customer to buy buy a lot of flowers, so he tries to change the price of flowers for customers customers who have already bought some flowers. More precisely, if a customer customer has already bought x\$x\$ flowers, he should pay (x+1)∗ci\$(x+1) \times c_i\$ dollars to to buy flower number i\$i\$.
You and your K−1 friends want to buy all N flowers in such a way that you spend the least amount of money. You can buy the flowers in any order.
You and your \$K−1\$ friends want to buy all \$N\$ flowers in such a way that you spend the least amount of money. You can buy the flowers in any order.
Input:
##Input:
The first line of input contains two integers N\$N\$ and K \$K\$ (K≤N\$K≤N\$). The next line line contains N\$N\$ space separated positive integers c1,c2,…,cN\$c_1,c_2,\dots,c_N\$.
Output:
Output:
Print the minimum amount of money you (and your friends) have to pay in in order to buy all N\$N\$ flowers.