P7823 "RdOI R3" Alarm Clock

Description

You have an alarm clock. The timer settings of the alarm clock can be represented by two variables $a_1, a_2$, both starting with an initial value of $0$. Adjusting a variable from $x$ to $y$ costs $|x-y|$. Next, you are given $n$ numbers $k_1, k_2, \cdots, k_n$. On day $i$, you need to adjust the variables so that **at least** one of the two variables equals $k_i$. Find the minimum total cost.

Input Format

The first line contains an integer $n$. The second line contains $n$ integers $k_1, k_2, \cdots, k_n$.

Output Format

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

Explanation/Hint

### Sample Explanation Sample explanation 1: Day 1: $a=\{1,0\}$. Day 2: $a=\{1,0\}$. Day 3: $a=\{4,0\}$. Day 4: $a=\{5,0\}$. Day 5: $a=\{5,1\}$. Day 6: $a=\{4,1\}$. Sample explanation 2: Day 1: $a=\{4,0\}$. Day 2: $a=\{4,1\}$. Day 3: $a=\{9,1\}$. Day 4: $a=\{9,2\}$. Day 5: $a=\{9,3\}$. --- ### Constraints |Test ID|$n\le$|Special property| |:-:|:-:|:-:| |$1$|$10$|None| |$2$|$10^3$|$k_i\le 100$| |$3\sim 4$|$10^3$|None| |$5\sim 7$|$10^5$|$k_i\le 100$| |$8\sim 10$|$10^5$|None| In addition, to catch some incorrect solutions, this problem includes several hack test points. The testdata of the hack test points satisfy $n\le 10$ and $k_i\le 100$. If your program does not pass at least one hack test point, then the score for this problem will be $0$. For $100\%$ of the testdata, $1\le n\le 10^5$ and $1\le k_i\le 10^9$. Translated by ChatGPT 5