/
Time Limit: 2 sec / Memory Limit: 1024 MiB
配点 : 600 点
問題文
長さ 2 以上の正整数列のスコアをその数列の最大値と最小値の差で定義します。
長さ N の正整数列 A が与えられるので、A を長さ 2 以上であるような 1 つ以上の連続部分列に分割したときに各連続部分列のスコアの総和としてあり得る値の最小値を求めてください。
より形式的には、ある正整数 K に対して長さが 2 以上であるような K 個の正整数列 B_1,B_2,\dots,B_K をこの順に連結させると A と同じ数列になるときに \sum_{k=1}^{K}\left(\max(B_k)-\min(B_k)\right) としてあり得る値の最小値を求めてください。
制約
- 2 \leq N \leq 2 \times 10^5
- 1 \leq A_i \leq 10^9
- 入力される値はすべて整数
入力
入力は以下の形式で標準入力から与えられる。
N A_1 A_2 \ldots A_N
出力
答えを 1 行で出力せよ。
入力例 1
5 1 3 6 8 9
出力例 1
5
A を (1,3) と (6,8,9) に分割すると、スコアはそれぞれ 2,3 となり、総和は 5 です。
各連続部分列のスコアの総和を 4 以下にすることはできないため、答えは 5 です。
入力例 2
6 1 4 2 8 7 9
出力例 2
5
入力例 3
7 10 480 10 480 480 10 480
出力例 3
470
Score : 600 points
Problem Statement
The score of a sequence of positive integers of length at least 2 is defined as the difference between the maximum and minimum values of the sequence.
You are given a sequence A of N positive integers. Find the minimum possible total score of the contiguous subsequences when A is divided into one or more contiguous subsequences, each of length at least 2.
More formally, find the minimum possible value of \sum_{k=1}^{K}\left(\max(B_k)-\min(B_k)\right) over all positive integers K and sequences of positive integers B_1,B_2,\dots,B_K, each of length at least 2, such that concatenating B_1,B_2,\dots,B_K in this order yields A.
Constraints
- 2 \leq N \leq 2 \times 10^5
- 1 \leq A_i \leq 10^9
- All input values are integers.
Input
The input is given from Standard Input in the following format:
N A_1 A_2 \ldots A_N
Output
Print the answer in a single line.
Sample Input 1
5 1 3 6 8 9
Sample Output 1
5
If A is divided into (1,3) and (6,8,9), their scores are 2 and 3, respectively, for a total of 5.
It is impossible to make the total score of the contiguous subsequences 4 or less, so the answer is 5.
Sample Input 2
6 1 4 2 8 7 9
Sample Output 2
5
Sample Input 3
7 10 480 10 480 480 10 480
Sample Output 3
470