#P2339. Minimal Homework Submission Time

    ID: 15613 Type: Default 1000ms 256MiB

Minimal Homework Submission Time

Minimal Homework Submission Time

Bethsy has \(C\) subjects with homework that need to be submitted. All the teachers' classrooms are located along a corridor of length \(H\). The teachers accept homework only after class, and submitting the homework takes no time. Bethsy starts from position 0 and she is given the positions of each teacher's classroom as well as the position of the exit of the corridor.

Each unit of distance takes 1 second to travel. Your task is to compute the minimum time (in seconds) required for Bethsy to visit all the classrooms (in any order) and then reach the exit.

Note: Since the classrooms lie along a line, the optimal strategy is to cover the interval from the leftmost classroom \(L\) to the rightmost classroom \(R\) and then exit from the end that minimizes the total travel distance.

The answer can be computed as:

[ \text{time} = \min \Bigl(|0-L| + (R-L) + |E-R|,; |0-R| + (R-L) + |E-L|\Bigr) ]

where \(E\) is the exit position.

inputFormat

The input consists of three lines:

  1. The first line contains two integers \(C\) and \(H\) \((1 \leq C \leq 1000,\; 1 \leq H \leq 1000)\) representing the number of subjects and the length of the corridor.
  2. The second line contains \(C\) integers indicating the positions of the teachers' classrooms along the corridor. Each position is an integer between 0 and \(H\) (inclusive).
  3. The third line contains one integer \(E\) representing the exit position of the corridor (0 \(\leq E \leq H\)).

outputFormat

Output a single integer representing the minimum time (in seconds) needed for Bethsy to submit all her homework and reach the exit.

sample

3 10
2 5 8
10
10

</p>