Wipro Coding Question 1

Minimum Cost to Buy N Apples

 

Here, in this page we will discuss the Wipro coding question asked in previous recruitment drive. We will  write an efficient algorithm for the given question to find the minimum cost to buy N apples and there solution in C++ and Java. 

Wipro coding question 1

Question 1

Josh went to the market to buy N apples. He found two shops, shop A and B, where apples were being sold in lots. He can buy any number of the complete lot(s) but not loose apples. He is confused with the price and wants you to figure out the minimum cost to buy exactly N apples. Write an algorithm for Josh to calculate the minimum cost to buy exactly N apples.

Input Format:

  • The first line of the input consists of an integer – N, representing the total number of apples that Josh wants to buy.
  • The second line consists of two space-separated positive integers – M1 and P1, representing the number of apples in a lot and the lot’s price at shop A, respectively.
  • The third line consists of two space-separated positive integers-M2 and P2, representing the number of apples in a lot and lot’s price at shop B, respectively.

Output Format:

Print a positive integer representing the minimum price at which Josh can buy the apples.

Sample Input:

19

3 10

4 15

Sample Output:

65