#P2234. [HNOI2002] 营业额统计

    ID: 1208 远端评测题 1000ms 128MiB 尝试: 3 已通过: 0 难度: 3 上传者: 标签>2002各省省选平衡树湖南概率论,统计

[HNOI2002] 营业额统计

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 nn (n32767n \leq 32767), representing the number of days from the company’s founding up to now.
Each of the next nn lines contains an integer aia_i (ai106|a_i| \leq 10^6), representing the turnover on day ii, 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 2312^{31}.

6
5
1
2
5
4
6

12

Hint

Result explanation: 5+15+21+55+45+65=5+4+1+0+1+1=125+|1-5|+|2-1|+|5-5|+|4-5|+|6-5|=5+4+1+0+1+1=12.

Translated by ChatGPT 5