Outlets
InterviewTime limit1sMemory limit128 MB
Given power strips that swap between standard A and B outlets, find the maximum number of standard-A outlets that can end up powering computers.
- Level
Medium5 of 10
- Topics
- Greedy, Sorting, Simulation, Implementation
- Solved
- No attempts yet
Problem
Alex has retired and now enjoys programming at home. Because he likes to code together with many people, he wants to set up as many computers at home as possible.
Where he lives there are two standards for outlets and plugs: standard A and standard B. The two standards are incompatible, so a standard-A plug fits only a standard-A outlet, and a standard-B plug fits only a standard-B outlet.
Alex's house has exactly one outlet, and it is standard A. Every computer sold uses a standard-A plug, so a single outlet can power only one computer. However, Alex owns power strips (multi-taps), which come in two kinds:
- A type-1 strip uses a standard-A plug and provides several standard-B outlets.
- A type-2 strip uses a standard-B plug and provides several standard-A outlets.
The strips can withstand any amount of current. So by plugging a type-1 strip into a standard-A outlet to obtain standard-B outlets, then plugging a type-2 strip into one of those standard-B outlets to obtain standard-A outlets again, and continuing this way, he can keep increasing the number of standard-A outlets.
Given how many strips of each kind Alex has and how many outlets each strip provides, find the maximum number of standard-A outlets that can hold a computer in the end. (An outlet holding a computer counts as one computer; that is, each standard-A outlet that ends up free corresponds to one computer.)
Input
The first line contains the number of type-1 strips and the number of type-2 strips . ()
The second line contains the number of standard-B outlets provided by each type-1 strip, separated by spaces. ()
The third line contains the number of standard-A outlets provided by each type-2 strip, separated by spaces. ()
If or is , the corresponding line may be empty.
Output
Print the maximum number of computers that can be used, on a single line.
Hint
At the start there is only one standard-A outlet, so only one computer can be plugged in. Plugging a type-1 strip into that outlet spends one standard-A outlet but yields several standard-B outlets, and plugging type-2 strips into those standard-B outlets yields several standard-A outlets.
In the first example, plug the type-1 strip with 3 standard-B outlets into the single standard-A outlet, then plug the two type-2 strips (with 3 and 2 standard-A outlets) into two of the resulting standard-B outlets. This produces standard-A outlets, so 5 computers can be used.