Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com

Minimum and Maximum Cost to buy N Candies

Hard
0/120
Average time to solve is 10m
profile
Contributed by
26 upvotes
Asked in companies
Paytm (One97 Communications Limited)Tech MahindraGroww

Problem statement

Ram went to a specialty candy store in Ninjaland which has 'N' candies with different costs.

The Candy shop gives a special offer to its customers. A customer can buy a single candy from the store and get at most 'K' different candies for free. Now, Ram is interested in knowing the maximum and the minimum amount he needs to spend for buying all the candies available in the store.

Note: In both cases, Ram must utilize the offer i.e. if 'K' or more candies are available, he must take 'K' candies for every candy purchase. If less than K candies are available, he must take all candies for a candy purchase.

For Example :

For 'N' =  5 and 'K' = 2

Let the cost of different candies in the store be: [9 8 2 6 4]

For the minimum amount: 
Ram can buy a candy with cost 2 and take candies with costs 9 and 8 for free. 
Then, he can buy a candy with cost 4 and take candy with cost 7 for free. 
Thus, the minimum cost will be 6 i.e. 2 + 4. 

For the maximum amount: 
Ram can buy a candy with cost 9 and take candies with costs 2 and 6 for free. 
Then, he can buy candy at cost 8 and take candy at cost 4 for free. 
Thus, the minimum cost will be 17 i.e. 9 + 8.

Thus, Minimum = 6 and Maximum = 17.
Detailed explanation ( Input/output format, Notes, Images )
Constraints:
1 <= 'T' <= 5
1 <= 'N' <= 10^5
0 <= 'K' < N
1 <= 'COST' <= (10^9) 

Where 'T' is the number of test cases, 'N' is the number of candies, 'K' is a type of candies and 'COST' is the cost of candies.

Time limit: 1 sec
Sample Input 1:
1
4 2
3 2 1 4
Sample Output 1 :
3 7
Explanation :
For the minimum amount: 
Ram can buy candy with cost 1 and take candies with costs 3 and 4 for free. 
Then, he can buy candy with cost 2.
Thus, the minimum cost will be 3 i.e. 1 + 2. 

For the maximum amount: 
Ram can buy candy with cost 4 and take candies with costs 1 and 2 for free. 
Then, he can buy candy with cost 3. 
Thus, the minimum cost will be 7 i.e. 4 + 3.
Sample Input 2:
2
5 2
9 8 2 6 4
3 0
1 5 4
Sample Output 2 :
6 17
10 10
Full screen
Console