#P7823. 「RdOI R3」闹钟

「RdOI R3」闹钟

Description

You have an alarm clock. The timer settings of the alarm clock can be represented by two variables a1,a2a_1, a_2, both starting with an initial value of 00. Adjusting a variable from xx to yy costs xy|x-y|.

Next, you are given nn numbers k1,k2,,knk_1, k_2, \cdots, k_n. On day ii, you need to adjust the variables so that at least one of the two variables equals kik_i. Find the minimum total cost.

Input Format

The first line contains an integer nn.
The second line contains nn integers k1,k2,,knk_1, k_2, \cdots, k_n.

Output Format

Output one integer in one line, representing the minimum cost.

6
1 1 4 5 1 4
7
5
4 1 9 2 3
12

Hint

Sample Explanation

Sample explanation 1:
Day 1: a={1,0}a=\{1,0\}.
Day 2: a={1,0}a=\{1,0\}.
Day 3: a={4,0}a=\{4,0\}.
Day 4: a={5,0}a=\{5,0\}.
Day 5: a={5,1}a=\{5,1\}.
Day 6: a={4,1}a=\{4,1\}.

Sample explanation 2:
Day 1: a={4,0}a=\{4,0\}.
Day 2: a={4,1}a=\{4,1\}.
Day 3: a={9,1}a=\{9,1\}.
Day 4: a={9,2}a=\{9,2\}.
Day 5: a={9,3}a=\{9,3\}.


Constraints

Test ID nn\le Special property
11 1010 None
22 10310^3 ki100k_i\le 100
343\sim 4 None
575\sim 7 10510^5 ki100k_i\le 100
8108\sim 10 None

In addition, to catch some incorrect solutions, this problem includes several hack test points. The testdata of the hack test points satisfy n10n\le 10 and ki100k_i\le 100. If your program does not pass at least one hack test point, then the score for this problem will be 00.

For 100%100\% of the testdata, 1n1051\le n\le 10^5 and 1ki1091\le k_i\le 10^9.

Translated by ChatGPT 5