Two Machines
InterviewTime limit1sMemory limit512 MB
Given commissioning times and rates for two machines, find the most parts made in k minutes when only one engineer can commission at a time.
Problem
A factory has two machines. The goal is to make as many parts as possible during today's shift, which lasts minutes.
Both machines are mothballed at the start. The first machine needs minutes to be commissioned, after which it makes parts per minute. The second machine needs minutes to be commissioned, after which it makes parts per minute.
An engineer must be present to commission a machine, so the two machines cannot be commissioned at the same time. Commissioning one machine while the other makes parts is allowed, and both machines may make parts at the same time.
Find the maximum number of parts that can be made in minutes.
Input
The first line contains a single integer (), the length of the shift in minutes.
The second line contains non-negative integers and (), the commissioning time of the first machine and the number of parts it makes per minute.
The third line contains non-negative integers and (), the commissioning time of the second machine and the number of parts it makes per minute.
Output
Print one integer, the maximum number of parts that can be made during the shift.
The answer may exceed the 32-bit integer range, so use a 64-bit type such as long long in C++ or int64 in Pascal.
Hint
In the sample, it is better to commission the second machine first and make 45 parts in the remaining 15 minutes. Then commission the first machine and make 20 more parts on it in the remaining 5 minutes.
If the first machine is commissioned first and makes 40 parts in the remaining 10 minutes, then the second machine can make only 15 parts after it is commissioned. The total is 55, which is less than 65.