Roses
Time limit1sMemory limit128 MB
Given two bundle prices for roses, find the minimum cost to buy at least N roses using any combination of bundles from either shop.
- Level
Medium6 of 10
- Topics
- Math, Greedy, Brute force
- Solved
- No attempts yet
Problem
To celebrate Valentine's Day, Sanggeun wants to give his girlfriend yellow roses. There are two flower shops near his house, and both have prepared plenty of flowers for Valentine's Day, so roses will never run out. However, both shops sell roses only in bundles.
The first shop sells roses for won, and the second shop sells roses for won. , , , and are all positive integers. If buying more than roses is cheaper than buying exactly , he can buy more and give the leftover roses to the shop clerk.
Write a program that computes the minimum amount of money Sanggeun needs to buy at least roses.
Input
The first line contains , , , , and , separated by spaces. does not exceed , and , , , and do not exceed .
Output
Print the minimum amount of money needed to buy at least roses. The answer never exceeds .
Hint
In the first example, buying one bundle from the first shop (2 roses, 3 won) and two bundles from the second shop (20 roses, 28 won) yields 22 roses for 31 won.