#P7994. [USACO21DEC] Air Cownditioning B
[USACO21DEC] Air Cownditioning B
Problem Description
Farmer John’s cows are very picky about the temperature inside their barn. Some cows like it cooler, while others like it warmer.
Farmer John’s barn contains a row of stalls, numbered , with one cow in each stall. The -th cow wants the temperature in her stall to be , but the current temperature in her stall is . To make sure every cow feels comfortable, Farmer John installed a new air-conditioning system. The way this system is controlled is quite special: he can send a command to the system telling it to increase or decrease the temperature of a continuous group of stalls by 1 unit—for example, “increase the temperature of stalls by 1 unit.” A continuous group of stalls can be as short as a single stall.
Please help Farmer John find the minimum number of commands he must send to the new air-conditioning system so that every stall’s temperature matches the ideal temperature of the cow in that stall.
Input Format
The first line contains . The next line contains non-negative integers , separated by spaces. The last line contains non-negative integers .
Output Format
Output one integer: the minimum number of commands Farmer John needs to use.
5
1 5 3 3 4
1 2 2 2 1
5
Hint
【Sample Explanation】
One optimal set of commands Farmer John can use is:
Initial temperatures : 1 2 2 2 1
Increase stalls 2..5 by 1 : 1 3 3 3 2
Increase stalls 2..5 by 1 : 1 4 4 4 3
Increase stalls 2..5 by 1 : 1 5 5 5 4
Decrease stalls 3..4 by 1 : 1 5 4 4 4
Decrease stalls 3..4 by 1 : 1 5 3 3 4
【Constraints】
- Test cases 2-5 satisfy .
- Test cases 6-8 satisfy .
- Test cases 9-10 satisfy .
- In test cases 1-6 and 9, temperature values do not exceed .
- In test cases 7-8 and 10, temperature values do not exceed .
Translated by ChatGPT 5