A shop is selling candies at a discount. For every two candies sold, the shop gives a third candy for free.

The customer can choose any candy to take away for free as long as the cost of the chosen candy is less than or equal to the minimum cost of the two candies bought.


	For example, if there are 4 candies with costs 1, 2, 3, and 4, and the customer buys candies with costs 2 and 3, they&nbsp;can take the candy with cost 1 for free, but not the candy with cost 4.


Given a 0-indexed integer array cost, where cost[i] denotes the cost of the ith candy, return the minimum cost of buying all the candies.

&nbsp;
Example 1:

Input: cost = [1,2,3]
Output: 5
Explanation: We buy the candies with costs 2 and 3, and take the candy with cost 1 for free.
The total cost of buying all candies is 2 + 3 = 5. This is the only way we can buy the candies.
Note that we cannot buy candies with costs 1 and 3, and then take the candy with cost 2 for free.
The cost of the free candy has to be less than or equal to the minimum cost of the purchased candies.


Example 2:

Input: cost = [6,5,7,9,2,2]
Output: 23
Explanation: The way in which we can get the minimum cost is described below:
- Buy candies with costs 9 and 7
- Take the candy with cost 6 for free
- We buy candies with costs 5 and 2
- Take the last remaining candy with cost 2 for free
Hence, the minimum cost to buy all candies is 9 + 7 + 5 + 2 = 23.


Example 3:

Input: cost = [5,5]
Output: 10
Explanation: Since there are only 2 candies, we buy both of them. There is not a third candy we can take for free.
Hence, the minimum cost to buy all candies is 5 + 5 = 10.


&nbsp;
Constraints:


	1 &lt;= cost.length &lt;= 100
	1 &lt;= cost[i] &lt;= 100


class Solution:
    def minimumCost(self, cost: List[int]) -> int:
        cost.sort(reverse=True)
        res, i, N = 0, 0, len(cost)
        while i < N:
            res += sum(cost[i : i + 2])
            i += 3
        return res
class Solution {
    /** Algorithm
     * 1. Sort the cost array.
     * 2. In a loop, start from the back and buy items n, n-1 to get n-2 for free.
     * 3. Decrement the position by 3 and continue. stop when you reach 1.
     * 4. From 1, add the remaining 1 or 2 items.
     *
     */
    public int minimumCost(int[] cost) {
        int minCost = 0;
        int index = cost.length -1;
        Arrays.sort(cost);
        // add items in pairs of 2, the 3rd one getting it for free.
        while (index > 1) {
            minCost += cost[index] + cost[index -1];
            index -= 3;
        }
        // add the remaining 1, 2 items, if any.
        while(index >= 0) {
            minCost += cost[index--];
        }
        return minCost;
    }
}
//Solution 01:
class Solution {
public:
    int minimumCost(vector<int>& cost) {
        int n = cost.size();
        int i = n-1, ans = 0;

        if(n <= 2){
            for(auto x: cost)
                ans += x;
            return ans;
        }

        sort(cost.begin(), cost.end());

        while(i>=1){
            ans += cost[i] + cost[i-1];
            if(i-1 == 0 || i-1 == 1) return ans;
            i = i-3;
        }
        ans += cost[0];

        return ans;
    }
};
var minimumCost = function(cost) {
    if (cost.length < 3) {
        return cost.reduce((prev, cur) => prev + cur);
    }

    cost.sort((a, b) => b - a);
    let count = 0;
    let sum = 0;

    for (const num of cost) {
        if (count === 2) {
            count = 0;
            continue;
        }
        sum += num;
        count++;
    }

    return sum;
};
Minimum Cost of Buying Candies With Discount