Temperature Adjustment
InterviewTime limit1sMemory limit1024 MB
Find the fewest presses so every beaker reaches its target when each press shifts beaker i by plus or minus X_i, or print -1 if no press count works.
- Level
Easy3 of 10
- Topics
- Math, Implementation
- Solved
- No attempts yet
Problem
A school lab has beakers numbered from to . The initial temperature of beaker is degrees Celsius.
Gihyeon must set every beaker to a target temperature. Beaker must reach degrees Celsius. Tired of adjusting each beaker by hand, Gihyeon built a temperature control device.
The device has one button. Each press changes the temperature of every beaker () by degrees, either up or down. Gihyeon chooses the direction for each beaker separately, but a beaker's temperature cannot stay the same. Each is fixed for its beaker and never changes.
Each press takes time, so Gihyeon wants to reach all target temperatures with the fewest presses. Given , the initial temperatures, the target temperatures, and the change values, find the minimum number of presses needed. Also determine when the device cannot bring every beaker to its target.
Input
The first line contains ().
The second line contains separated by spaces ().
The third line contains separated by spaces ().
The fourth line contains separated by spaces ().
Output
Print the minimum number of button presses needed to set every beaker to its target temperature.
If the device cannot reach the targets, print -1.