Milk and Honey
InterviewTime limit1sMemory limit1024 MB
Assign each field to cows or bees to maximize total happiness, where each field's output value declines linearly with each added unit.
- Level
Medium4 of 10
- Topics
- Greedy, Math, Implementation
- Solved
- No attempts yet
Problem
In the land of milk and honey, Juku is in charge of cows and bees. Bees sting cows and make them unhappy, and cows eat all the flowers bees use to make honey, so cows and bees must be kept on separate fields. Each field can be used for cows or for bees, but not both.
Cows and bees are free to obtain, so a field used for cows is filled with the maximum cows it can support, and a field used for bees is filled with bees.
Each cow produces one unit of milk and each bee produces one unit of honey. Milk and honey give different amounts of happiness when consumed, and customers value rare goods more highly. So, within a single field, the first unit of milk produces units of happiness, the second only , the third , and so on (a value never drops below ). The same rule holds for honey with and .
Juku wants to decide, for each field, whether to raise cows or bees so that the total happiness is maximized. There are far too many cases to work out by hand, so compute the maximum for Juku.
Input
The first line contains two integers: (), the happiness of the first unit of milk, and (), the amount by which the happiness value drops for each additional unit of milk on a field.
The second line contains two integers and (), giving the same information for honey.
The third line contains (), the number of fields. Each of the next lines contains () and (), the number of cows and the number of bees that field can support.
Output
Print a single line containing the maximum total happiness achievable.