5.1 Postman Pat
(RankList for this Question)
Score: 30pts
Time Limit: 1.00 sec
Pat is a postman who wants to deliver letters to \(n\) households. Initially, Pat is at position \(P\). You can imagine this as an infinitely long line. Now the \(n\) houses are located in positions \(x_1\), \(x_2\), … , \(x_n\). In 1 second, Pat can move one direction to left or one direction to the right i.e. if he was at position \(x\) he can move either to \(x + 1\) or \(x – 1\). Pat can deliver a letter to a house at position \(x_i\) only when he reaches that position.
Can you help Pat find the minimum possible time it will take for him to deliver all the letters. You have to answer \(t\) queries.

Constraints
\(1 \leq t \leq 500\)
\(1 \leq n \leq 100\)
\(1 \leq x_i \leq 500\)
\(1 \leq P \leq 500\)

Input Format
\(t\)
\(n_1 P_1\)
\(x_11 x_12 x_13 … x_1n_1\)
.
.
.
\(n_t P_t\)
\(x_t1 x_t2 x_t3 … x_tn_t\)

Output Format
For each testcase, print an integer – minimum time to deliver all the letters.

Example 1
Input:
1
6 10
2 8 6 1 13 15

Output:
19

Explanation:
Heres what we do –
- Go to 13, time increases by (13 – 10)
- Go to 15, time increase by (15 – 13)
- Go to 8, time increases by (15 – 8)
- Go to 6, time increases by (8 – 6)
- Go to 2, time increases by (6 – 2)
- Go to 1, time increases by (2 – 1)
Total time taken is 19 seconds.

Example 2
Input:
1
1 10
10

Output:
0

Explanation:
Similar explanation

Example 3
Input:
1
4 10
2 4 6 8

Output:
8

Explanation:
Similar explanation

Log In to solve the Question