Air Cownditioning
InterviewTime limit2sMemory limit1024 MB
Given preferred and current temperatures per stall, find the minimum number of range-increment or range-decrement commands to reach the target.
- Level
Medium5 of 10
- Topics
- Array, Greedy, Math, Prefix sum
- Solved
- No attempts yet
Problem
Farmer John's cows are very particular about the room temperature in their barn. Some cows like the temperature to be on the cooler side, while others prefer more warmth.
Farmer John's barn contains a sequence of stalls, numbered , each containing a single cow. The -th cow prefers the temperature of her stall to be , and right now the temperature in her stall is . In order to make sure every cow is comfortable, Farmer John installs a new air conditioning system that is controlled in a somewhat interesting way. He can send commands to the system telling it to either raise or lower the temperature in a consecutive series of stalls by 1 unit, for example "raise the temperature in stalls by 1 unit". The series of stalls could be as short as just a single stall.
Please help Farmer John determine the minimum number of commands he needs to send his new air conditioning system so that every cow's stall is at the ideal temperature for its resident cow.
Input
The first line of input contains . The next line contains the non-negative integers , separated by spaces. The final line contains the non-negative integers .
Output
Please write a single integer as output containing the minimum number of commands Farmer John needs to use.
Constraints
- Temperature values are at most .