P2234 [HNOI2002] Turnover Statistics
Description
Tiger was recently promoted to the Sales Department manager. His first task from the company is to compile statistics and analyze the company’s turnover since its founding.
Tiger took out the company’s ledger, which records the turnover of each day since the company was founded. Analyzing turnover is a rather complex task. Because of holidays, big discounts, or other situations, turnover may fluctuate. A certain degree of fluctuation is acceptable, but sometimes the turnover suddenly becomes very high or very low, which indicates a problem in the company’s operations. In economics and management, a minimum fluctuation value is defined to measure this: the larger the minimum fluctuation value, the less stable the turnover is.
To analyze whether the overall turnover from the company’s founding to now is stable, you only need to sum the minimum fluctuation value of each day. Your task is to write a program to help Tiger compute this value.
We define the minimum fluctuation value of a day as $\min\{|\text{the turnover of some previous day} - \text{the turnover of the current day}|\}$.
In particular, the minimum fluctuation value of the first day is the turnover of the first day.
Input Format
The first line contains a positive integer $n$ ($n \leq 32767$), representing the number of days from the company’s founding up to now.
Each of the next $n$ lines contains an integer $a_i$ ($|a_i| \leq 10^6$), representing the turnover on day $i$, which may be negative.
Output Format
Output a single integer: the sum of the minimum fluctuation values of each day. It is guaranteed that the result is less than $2^{31}$.
Explanation/Hint
Result explanation: $5+|1-5|+|2-1|+|5-5|+|4-5|+|6-5|=5+4+1+0+1+1=12$.
Translated by ChatGPT 5