# Minimum and Maximum Cost to buy N Candies

Posted: 9 Dec, 2020

Difficulty: Hard

#### 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.
```

##### Input format :

```
The first line contains an integer 'T' which denotes the number of test cases or queries to be run. Then, the 'T' test cases follow.
The first line of each test case or query contains two space-separated integers 'N' and ‘K’ representing the number of candies available and the number of candies you get free for a single purchase respectively.
The second line of each test case contains 'N' single space-separated integers, representing the costs of the candies.
```

##### Output format:

```
For each test case, print two space-separated integers 'A' and 'B' where 'A' is the minimum amount and 'B' is the maximum amount in which Ram can buy all the candies.
```

##### Note:

```
You do not need to print anything, it has already been taken care of. Just implement the given function.
```

##### 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
```

Approach 1

Let us apply the way that for minimum cost, Ram buys the candy with minimum cost and then takes top 'K' expensive candies for free.

Similarly, for maximum cost, Ram buys the candy with maximum cost and then takes the top 'K' cheapest candies for free.

- For Minimum:
- Sort the cost array in increasing order.

Now, until the candies are available:

→ Start picking candies from the left i.e. the cheapest one.

→ Add the cost of the picked candy in a variable say, 'MINCOST'.

→ Drop 'K' candies from the right as you get them for free.

Return 'MINCOST'.

- Sort the cost array in increasing order.
- For Maximum:
- Sort the cost array in decreasing order.

Now, until the candies are available:

→ Start picking candies from the left i.e. the one with maximum cost.

→ Add the cost of the picked candy in a variable say, 'MAXCOST'.

→ Drop 'K' candies from the right as you get them for free.

Return 'MAXCOST'.

- Sort the cost array in decreasing order.

Approach 2

The idea is to use a priority queue.

**Firstly, we need to calculate M i.e. the number of candies that are to be actually bought, M can be calculated simply as:****Ceil[ N/(K+1) ] as Ram gets K+1 candies at one purchase where 1 candy is bought by Ram and K candies are free.**

For Example: For N = 8 and K = 2, when Ram buys one candy he gets 2 candies for free, so, in one buy he gets 3 (2+1) candies, so, total purchases required to buy all the 8 candies are Ceil[ 8 / 3] = 3.

- The idea is basically that for the minimum cost, take M cheapest candies and for the maximum cost take M costliest candies.
- For the minimum cost, create a min priority queue containing the costs of all the candies and then pick top M elements.
- Similarly for maximum cost, create a max priority queue containing the costs of all the candies and then pick top M elements.

SIMILAR PROBLEMS

# Lexicographic Permutation Rank

Posted: 13 Jul, 2021

Difficulty: Moderate

# Zero Pair Sum

Posted: 22 Jul, 2021

Difficulty: Moderate

# Implement a Queue

Posted: 27 Jul, 2021

Difficulty: Easy

# Remove K Corner Elements

Posted: 31 Jul, 2021

Difficulty: Easy

# Connecting Ropes

Posted: 12 Nov, 2021

Difficulty: Hard