No.3066 Collecting Coins Speedrun 1
タグ : / 解いたユーザー数 138
作問者 : 👑
aplysiaSheep
👑
hamamu
Problem Statement
There are $N$ coins on a number line. The $i$-th coin is at coordinate $c_i$. The coordinates of any $2$ coins are distinct from each other.
You are initially at coordinate $0$. You may move a distance of $1$ in the positive or negative direction any number of times.
You can obtain a coin if and only if you are at the same coordinate as that coin.
Your goal is to collect all the coins and finally return to coordinate $0$. Find the minimum total distance traveled required to achieve the goal.
Constraints
- All inputs are integers
- $1 \leq N \leq 10^5$
- $-10^8 \leq c_1 < c_2 < \dots < c_N \leq 10^8$
Input
The input is given from standard input in the following format.
$N$ $c_1$ $c_2$ $\dots$ $c_N$
Output
Output the answer.
Sample
Sample 1
Input
3 -1 1 3
Output
8
By collecting the coins in the order of the $1$-st, $3$-rd, and $2$-nd, you move through the coordinates as $0 \rightarrow -1 \rightarrow 3 \rightarrow 1 \rightarrow 0$, so the total distance traveled is $1 + 4 + 2 + 1 = 8$.
Note that you must return to coordinate $0$ after collecting all the coins.
Sample 2
Input
9 -31415296 -26457513 -24494897 -17320508 -14142135 0 22360679 27182818 31622776
Output
126076144
提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。