测评会员优惠活动进行中 · 开通 VIP,有效期内测评不限次 VIP 优惠中 · 测评不限次 立即查看

A10492. Checkpoints

编程题 普及/提高-

题目描述

Vasya takes part in the orienteering competition. There are $n$ checkpoints located along the line at coordinates $x_{1},x_{2},...,x_{n}$ . Vasya starts at the point with coordinate $a$ . His goal is to visit at least $n-1$ checkpoint in order to finish the competition. Participant are allowed to visit checkpoints in arbitrary order.

Vasya wants to pick such checkpoints and the order of visiting them that the total distance travelled is minimized. He asks you to calculate this minimum possible value.

输入格式

The first line of the input contains two integers $n$ and $a$ ( $1<=n<=100000$ , $-1000000<=a<=1000000$ ) — the number of checkpoints and Vasya's starting position respectively.

The second line contains $n$ integers $x_{1},x_{2},...,x_{n}$ ( $-1000000<=x_{i}<=1000000$ ) — coordinates of the checkpoints.

输出格式

Print one integer — the minimum distance Vasya has to travel in order to visit at least $n-1$ checkpoint.

输入输出样例

输入 #1
3 10
1 7 12
输出 #1
7
输入 #2
2 0
11 -10
输出 #2
10
输入 #3
5 0
0 0 1000 0 0
输出 #3
0

说明/提示

In the first sample Vasya has to visit at least two checkpoints. The optimal way to achieve this is the walk to the third checkpoints (distance is $12-10=2$ ) and then proceed to the second one (distance is $12-7=5$ ). The total distance is equal to $2+5=7$ .

In the second sample it's enough to visit only one checkpoint so Vasya should just walk to the point $-10$ .
上一题 去做题 下一题