#ABC471C. 饼干与贪心高桥 / Cookies and Greedy Takahashi
饼干与贪心高桥 / Cookies and Greedy Takahashi
Problem Statement
There are cookies at positions on a number line. The coordinate of the -th cookie is .
Takahashi is initially at coordinate on the number line, and repeats the following action until he has picked up all cookies.
- Action: Move to the coordinate of the nearest cookie from his current position (if there are multiple such cookies, the one with the smallest coordinate), and pick up that cookie.
Find the total distance Takahashi travels until he picks up all the cookies.
Input
The input is given from Standard Input in the following format:
Output
Output the answer.
Constraints
- The are distinct.
- All input values are integers.
4
-1 -4 2 -11
23
Takahashi acts as follows.
- He moves from coordinate to coordinate and picks up the cookie. The distance traveled is .
- He moves from coordinate to coordinate and picks up the cookie. The distance traveled is .
- He moves from coordinate to coordinate and picks up the cookie. The distance traveled is .
- He moves from coordinate to coordinate and picks up the cookie. The distance traveled is .
Thus, the total distance traveled is .
In the second action, the distances to the cookie at coordinate and the cookie at coordinate are both , and Takahashi moves to , the smaller coordinate.
10
1 2 3 4 5 -1 -2 -3 -4 -6
17
- Source: AtCoder ABC 471 C
相关
在下列比赛中: