#C6848. Minimum Battery Consumption for Robot Tasks

    ID: 50653 Type: Default 1000ms 256MiB

Minimum Battery Consumption for Robot Tasks

Minimum Battery Consumption for Robot Tasks

You are given n tasks located on a one-dimensional rail line. Each task is positioned at a certain integer coordinate. A robot starts at a given position and must complete all tasks and return to its starting position. The robot consumes battery units equal to the distance traveled.

An optimal strategy is to first travel to one end of the task positions, then proceed to the other end, and finally return to the starting position. Mathematically, if \( p_{min} = \min(task\_positions) \) and \( p_{max} = \max(task\_positions) \), and the starting position is s, then the minimum battery consumption is given by:

[ |s - p_{min}| + |p_{max} - p_{min}| + |p_{max} - s| ]

Your task is to compute this minimum battery consumption.

inputFormat

The input is read from standard input (stdin) and consists of three lines:

  1. The first line contains an integer (n), the number of tasks.
  2. The second line contains (n) space-separated integers representing the positions of each task on the rail line.
  3. The third line contains an integer representing the starting position of the robot.

outputFormat

Output a single integer to standard output (stdout), which is the minimum battery consumption required for the robot to complete all tasks and return to its starting position.## sample

3
1 5 9
0
18