AT_abc479_f [ABC479F] Narrow Passage
Description
There are $ N $ people standing on a number line. The people are numbered $ 1, 2, \ldots, N $ , and initially person $ i $ is at coordinate $ S_i $ . Here, the $ S_i $ are pairwise distinct.
You can repeatedly perform the following operation.
- Choose one person. Let $ x $ be the coordinate where that person currently is, and move them to $ x - 1 $ or $ x + 1 $ . If there is a person at the destination, that person is simultaneously moved to coordinate $ x $ .
Find the minimum number of operations required to reach a state where each person $ i $ is at coordinate $ T_i $ . Here, the $ T_i $ are pairwise distinct.
Input Format
The input is given from Standard Input in the following format:
> $ N $ $ S_1 $ $ S_2 $ $ \ldots $ $ S_N $ $ T_1 $ $ T_2 $ $ \ldots $ $ T_N $
Output Format
Output the answer.
Explanation/Hint
### Sample Explanation 1
Initially, persons $ 1, 2, 3 $ are at coordinates $ 4, 2, 5 $ , respectively. By performing the operations as follows, persons $ 1, 2, 3 $ end up at coordinates $ 3, 4, 1 $ , respectively, in six operations.
- Move person $ 1 $ from coordinate $ 4 $ to $ 3 $ . After this operation, persons $ 1, 2, 3 $ are at coordinates $ 3, 2, 5 $ , respectively.
- Move person $ 1 $ from coordinate $ 3 $ to $ 2 $ . Since person $ 2 $ is at coordinate $ 2 $ , person $ 2 $ simultaneously moves to coordinate $ 3 $ . After this operation, persons $ 1, 2, 3 $ are at coordinates $ 2, 3, 5 $ , respectively.
- Move person $ 3 $ from coordinate $ 5 $ to $ 4 $ . After this operation, persons $ 1, 2, 3 $ are at coordinates $ 2, 3, 4 $ , respectively.
- Move person $ 3 $ from coordinate $ 4 $ to $ 3 $ . Since person $ 2 $ is at coordinate $ 3 $ , person $ 2 $ simultaneously moves to coordinate $ 4 $ . After this operation, persons $ 1, 2, 3 $ are at coordinates $ 2, 4, 3 $ , respectively.
- Move person $ 3 $ from coordinate $ 3 $ to $ 2 $ . Since person $ 1 $ is at coordinate $ 2 $ , person $ 1 $ simultaneously moves to coordinate $ 3 $ . After this operation, persons $ 1, 2, 3 $ are at coordinates $ 3, 4, 2 $ , respectively.
- Move person $ 3 $ from coordinate $ 2 $ to $ 1 $ . After this operation, persons $ 1, 2, 3 $ are at coordinates $ 3, 4, 1 $ , respectively.
### Constraints
- $ 1 \leq N \leq 2 \times 10^5 $
- $ 0 \leq S_i, T_i \leq 10^9 $
- $ S_i \neq S_j $ and $ T_i \neq T_j $ if $ i \neq j $ .
- All input values are integers.