#P7994. Optimal Temperature Adjustment

    ID: 21178 Type: Default 1000ms 256MiB

Optimal Temperature Adjustment

Optimal Temperature Adjustment

Farmer John has N cows in a row, each in its own stall, and each cow has a preferred temperature. The i-th cow desires the temperature pi, but currently her stall is at temperature ti. To ensure all cows are comfortable, Farmer John installed a new air conditioning system capable of adjusting the temperature of a contiguous block of stalls by exactly 1 unit (either increasing or decreasing).

Given the initial and preferred temperatures, determine the minimum number of commands Farmer John must issue so that every stall reaches its cow's ideal temperature.

Hint: Let \(\Delta_i = p_i - t_i\). It can be shown that the answer is given by

\[ \text{answer} = |\Delta_1| + \sum_{i=2}^{N} \left|\Delta_i - \Delta_{i-1}\right| \]

inputFormat

The first line contains a single integer N (1 ≤ N ≤ 105), the number of cows. The second line contains N space-separated integers p1, p2, ..., pN - the preferred temperatures. The third line contains N space-separated integers t1, t2, ..., tN - the current temperatures.

outputFormat

Output a single integer, the minimum number of commands required to achieve the desired temperatures.

sample

3
2 2 2
1 1 1
1

</p>