You have money units of money, one chocolate costs price, and the shop gives you one free chocolate for every wrappers empty wrappers you hand in. Every chocolate you eat, bought or free, leaves you one wrapper, and you may keep exchanging as long as you hold at least wrappers of them.
Return the total number of chocolates you can eat.
Function Signature
def total_chocolates(money: int, price: int, wrappers: int) -> int:
Rules
-
You buy with money only, one whole chocolate at a time. Money left over after buying
money // price
chocolates goes unused.
-
Each exchange hands in exactly
wrappers
wrappers for one chocolate, and the wrapper of that chocolate can be used in later exchanges.
-
You cannot borrow chocolates or wrappers.
-
If
money < price
, you buy nothing and the answer is 0.
Constraints
-
1 <= money <= 10^6
-
1 <= price <= 10^6
-
2 <= wrappers <= 10^6
Examples
Input: money = 10, price = 1, wrappers = 2
Output: 19
You buy 10 chocolates and hold 10 wrappers. The exchanges go 10 wrappers for 5 chocolates (5 held), 4 for 2 (3 held), 2 for 1 (2 held) and 2 for 1 (1 held), so the total is 10 + 5 + 2 + 1 + 1 = 19.
Input: money = 15, price = 1, wrappers = 3
Output: 22
You buy 15 chocolates. The exchanges go 15 wrappers for 5 (5 held), 3 for 1 (3 held) and 3 for 1 (1 held), so the total is 15 + 5 + 1 + 1 = 22.
Input: money = 20, price = 3, wrappers = 5
Output: 7
You buy 6 chocolates and 2 units of money go unused. Five of the 6 wrappers buy one more chocolate, which leaves 2 wrappers, too few for another exchange.