Increase/Decrease/Copy
Contest: Educational Codeforces Round 166 (Rated for Div. 2)
Difficulty rating: 1100
Tags: greedy, implementation

You are given two integer arrays: array $$$a$$$ of length $$$n$$$ and array $$$b$$$ of length $$$n+1$$$.

You can perform the following operations any number of times in any order:

- choose any element of the array $$$a$$$ and increase it by $$$1$$$;
- choose any element of the array $$$a$$$ and decrease it by $$$1$$$;
- choose any element of the array $$$a$$$, copy it and append the copy to the end of the array $$$a$$$.

Your task is to calculate the minimum number of aforementioned operations (possibly zero) required to transform the array $$$a$$$ into the array $$$b$$$. It can be shown that under the constraints of the problem, it is always possible.

Input format:
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases.

Each test case consists of three lines:

- the first line contains a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$);
- the second line contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le 10^9$$$);
- the third line contains $$$n + 1$$$ integers $$$b_1, b_2, \dots, b_{n + 1}$$$ ($$$1 \le b_i \le 10^9$$$).

Additional constraint on the input: the sum of $$$n$$$ over all test cases doesn't exceed $$$2 \cdot 10^5$$$.

Output format:
For each test case, print a single integer — the minimum number of operations (possibly zero) required to transform the array $$$a$$$ into the array $$$b$$$.

Constraints:
Time limit: 2.0 s
Memory limit: 256.0 MB

Note:
In the first example, you can transform $$$a$$$ into $$$b$$$ as follows: $$$[2] \rightarrow [2, 2] \rightarrow [1, 2] \rightarrow [1, 3]$$$.

Example input:
3
1
2
1 3
2
3 3
3 3 3
4
4 2 1 2
2 1 5 2 3

Example output:
3
1
8

Editorial:
Let's fix the index of the element to be copied (denote it as $$$i$$$). For all other elements of the array, the number of required operations is $$$|a_j-b_j|$$$ for all $$$j \ne i$$$.
Consider the case when $$$a_i \le b_i$$$ (similar to the case when $$$a_i \ge b_i$$$). There are three possible relative location of the desired element $$$b_{n + 1}$$$:
• if $$$b_{n + 1} < a_i$$$, you can proceed as follows: copy $$$i$$$-th element, increase $$$a_{n + 1}$$$ to $$$b_{n + 1}$$$ and increase $$$a_i$$$ to $$$b_i$$$, then the number of operations is equal to $$$1 + (b_{n + 1} - a_i) + (b_i - a_i)$$$;
• if $$$a_i \le b_{n + 1} \le b_i$$$, you can proceed as follows: increase $$$a_i$$$ to $$$b_{n + 1}$$$, copy it, and keep increasing to $$$b_i$$$, then the number of operations is equal to $$$(b_{n + 1} - a_i) + 1 + (b_i - b_{n + 1}) = (b_i - a_i) + 1$$$;
• if $$$b_i < b_{n + 1}$$$, you can proceed as follows: increase $$$a_i$$$ to $$$b_i$$$, copy $$$i$$$-th element, and increase $$$a_{n + 1}$$$ to $$$b_{n + 1}$$$, then the number of operations is equal to $$$(b_i - a_i) + 1 + (b_{n + 1} - b_i)$$$.
As you can see, regardless of the case, $$$|b_i-a_i|$$$ is also added to the answer. So the answer looks like this: $$$\sum\limits_{j=1}^{n} |b_j-a_j|$$$ plus some extra operations to get $$$b_{n + 1}$$$. That extra value is equal to the minimum value of $$$f(i)$$$ over all indices $$$i$$$, where $$$f(i)$$$ is equal to $$$1 + |b_{n + 1} - a_i|$$$ or $$$1$$$ or $$$1 + |b_{n + 1} - b_i|$$$ depending on the cases described above.