33 lines
1.2 KiB
Python
33 lines
1.2 KiB
Python
'''
|
|
Bob has recently started commuting by bus. We know that a one bus ride ticket costs a dollars. Besides, Bob found out that he can buy a special ticket for m rides,
|
|
also he can buy it several times. It costs b dollars. Bob will need to use bus n times. Help Bob to calculate what is the minimum sum of money he will have to spend
|
|
to make n rides?
|
|
|
|
Example 1
|
|
Input:
|
|
n = 10; m = 3; a = 1; b = 2
|
|
Output:
|
|
7
|
|
Example 2
|
|
Input:
|
|
n = 10; m = 2; a = 1; b = 1
|
|
Output:
|
|
5
|
|
'''
|
|
import math
|
|
|
|
class Solution(object):
|
|
def solve(self, n: int, m: int, a: int, b: int) -> int: # n: number of rides, m: rides in special ticket, a: cost of single ticket, b: cost of special ticket
|
|
# Calculate the cost using only single tickets
|
|
cost_single = n * a
|
|
# Calculate the cost using special tickets
|
|
cost_special = (n // m) * b + (n % m) * a # Full special tickets + remaining rides with single tickets
|
|
|
|
cost_packs_only = math.ceil(n / m) * b # If we only use special tickets, we need to round up the number of packs
|
|
# Return the minimum of the two costs
|
|
return min(cost_single, cost_special, cost_packs_only)
|
|
|
|
|
|
s = Solution()
|
|
print(s.solve(10, 3, 5, 1)) # Output: 4
|
|
print(s.solve(10, 2, 1, 1)) # Output: 5 |