709B.md (1801B)
1 Source: https://codeforces.com/problemset/problem/709/B 2 3 B. Checkpoints 4 5 time limit per test 6 7 1 second 8 9 memory limit per test 10 11 256 megabytes 12 13 input 14 15 standard input 16 17 output 18 19 standard output 20 21 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. 22 23 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. 24 25 Input 26 27 The first line of the input contains two integers *n* and *a* (1 ≤ *n* ≤ 100 000, - 1 000 000 ≤ *a* ≤ 1 000 000) — the number of checkpoints and Vasya's starting position respectively. 28 29 The second line contains *n* integers *x*1, *x*2, ..., *x**n* ( - 1 000 000 ≤ *x**i* ≤ 1 000 000) — coordinates of the checkpoints. 30 31 Output 32 33 Print one integer — the minimum distance Vasya has to travel in order to visit at least *n* - 1 checkpoint. 34 35 Examples 36 37 Input 38 39 ``` 40 3 10 41 1 7 12 42 ``` 43 44 Output 45 46 ``` 47 7 48 ``` 49 50 Input 51 52 ``` 53 2 0 54 11 -10 55 ``` 56 57 Output 58 59 ``` 60 10 61 ``` 62 63 Input 64 65 ``` 66 5 0 67 0 0 1000 0 0 68 ``` 69 70 Output 71 72 ``` 73 0 74 ``` 75 76 Note 77 78 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. 79 80 In the second sample it's enough to visit only one checkpoint so Vasya should just walk to the point - 10.