๐ฌ Welcome to the Sweet Candy Shop! This shop has an amazing deal that will make any candy lover excited.
The Deal: For every two candies you buy, you get a third candy absolutely free! But there's a catch - the free candy must cost less than or equal to the cheaper of the two candies you purchased.
Your Mission: Given an array cost where cost[i] represents the price of the i-th candy, find the minimum total cost to buy all candies using this discount optimally.
Example Strategy: If candies cost [1, 2, 3, 4] and you buy candies costing 2 and 3, you can take the candy costing 1 for free (since 1 โค min(2,3) = 2), but not the candy costing 4.
Goal: Return the minimum cost to purchase all candies by strategically using the buy-2-get-1-free discount.
Input & Output
Constraints
- 1 โค cost.length โค 1000
- 1 โค cost[i] โค 100
- All candy costs are positive integers