Skip to main content

Minimum Shopping Cost with Vouchers

easy
Software engineer

Alex is an influencer who loves shopping online and sharing his latest hauls with his followers. He's planning a big unboxing video featuring $n$ products from an e-commerce site. Each product has an original price prices[i], and Alex has vouchersCount discount vouchers to spend across his purchases.

Each voucher halves the current price of one item (using integer division), and you are allowed to use multiple vouchers for a single product. For example, if $k$ vouchers are applied to a product priced at $p$, its final cost becomes $\lfloor p / 2^k \rfloor$.

Return the minimum total cost for purchasing all items after optimally applying all (or some) vouchers.

Example 1

Input

prices = [10, 20, 30], vouchersCount = 2

Output

35

Explanation

To minimize the total cost, we should always apply a voucher to the current most expensive item. 
1. Initial prices: [10, 20, 30]. The most expensive is 30. Applying 1 voucher: 30 / 2 = 15. Prices: [10, 20, 15].
2. Current prices: [10, 20, 15]. The most expensive is 20. Applying 1 voucher: 20 / 2 = 10. Prices: [10, 10, 15].
Total cost: 10 + 10 + 15 = 35.

Example 2

Input

prices = [100, 200], vouchersCount = 5

Output

50

Explanation

Applying vouchers greedily to the highest price: 200->100, 100->50, 100->50, 50->25, 50->25. Final prices: [25, 25].

Example 3

Input

prices = [15, 25, 35], vouchersCount = 0

Output

75

Constraints

  • 1 <= prices.length <= 10⁵
  • 0 <= vouchersCount <= 10⁹
  • 1 <= prices[i] <= 10⁹
  • Each voucher application uses integer division (floor).

OA

ArrayGreedyHeap (Priority Queue)Simulation
Language
Code editor loads in the browser.

Output

Input

[10,20,30]
2

Expected

35

Your output

Run to see your output.