Saturday, 27 November 2021

Cheap Travel Problem !!!

 PROBLEM 15: Cheap Travel


Problem Reference : Codeforces 

Ann has recently started commuting by subway. We know that a one ride subway ticket costs a rubles. Besides, Ann found out that she can buy a special ticket for m rides (she can buy it several times). It costs b rubles. Ann did the math; she will need to use subway n times. Help Ann, tell her what is the minimum sum of money she will have to spend to make n rides?

Input

The single line contains four space-separated integers nmab (1 ≤ n, m, a, b ≤ 1000) — the number of rides Ann has planned, the number of rides covered by the m ride ticket, the price of a one ride ticket and the price of an m ride ticket.


Output

Print a single integer — the minimum sum in rubles that Ann will need to spend.


Solution:


  1. import math
  2. n, m, a, b = input().split()
  3. n1 = int(n)
  4. m1 = int(m)
  5. a1 = int(a)
  6. b1 = int(b)
  7. n2 = n1
  8. sum1 = 0
  9. n3 = n1
  10. m3 = m1
  11. b3 = b1
  12. a3 = a1
  13.  
  14. d = n1 / m1
  15. d1 = math.ceil(d)
  16. d2 = d1 * b1
  17.  
  18. d3 = n2 * a1
  19.  
  20. while n3 >= m1:
  21. n3 = n3 - m1
  22. sum1 = sum1 + b3
  23. while n3 > 0:
  24. sum1 = sum1 + a3
  25. n3 = n3 - 1
  26. print(min(d2,d3,sum1))


This above solution is in python language.



Examples
input
6 2 1 2
output
6
input
5 2 2 3
output
8

No comments:

Post a Comment

Rearrange an array with O(1) extra space

  PROBLEM 61:  Rearrange an array with O(1) extra space (For best view experience, view in windows version) Problem Reference : GeeksForGeek...