33OJ
登录 注册

#ABC479F. 狭窄通道 / Narrow Passage

狭窄通道 / Narrow Passage

Problem Statement

There are NN people standing on a number line. The people are numbered 1,2,…,N1, 2, \ldots, N, and initially person ii is at coordinate SiS_i. Here, the SiS_i are pairwise distinct.

You can repeatedly perform the following operation.

  • Choose one person. Let xx be the coordinate where that person currently is, and move them to x−1x - 1 or x+1x + 1. If there is a person at the destination, that person is simultaneously moved to coordinate xx.

Find the minimum number of operations required to reach a state where each person ii is at coordinate TiT_i. Here, the TiT_i are pairwise distinct.

Constraints

  • 1≤N≤2×1051 \leq N \leq 2 \times 10^5
  • 0≤Si,Ti≤1090 \leq S_i, T_i \leq 10^9
  • Si≠SjS_i \neq S_j and Ti≠TjT_i \neq T_j if i≠ji \neq j.
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

  • NN
  • S1S_1 S2S_2 …\ldots SNS_N
  • T1T_1 T2T_2 …\ldots TNT_N

Output

Output the answer.

3
4 2 5
3 4 1
6

Initially, persons 1,2,31, 2, 3 are at coordinates 4,2,54, 2, 5, respectively. By performing the operations as follows, persons 1,2,31, 2, 3 end up at coordinates 3,4,13, 4, 1, respectively, in six operations.

  • Move person 11 from coordinate 44 to 33. After this operation, persons 1,2,31, 2, 3 are at coordinates 3,2,53, 2, 5, respectively.
  • Move person 11 from coordinate 33 to 22. Since person 22 is at coordinate 22, person 22 simultaneously moves to coordinate 33. After this operation, persons 1,2,31, 2, 3 are at coordinates 2,3,52, 3, 5, respectively.
  • Move person 33 from coordinate 55 to 44. After this operation, persons 1,2,31, 2, 3 are at coordinates 2,3,42, 3, 4, respectively.
  • Move person 33 from coordinate 44 to 33. Since person 22 is at coordinate 33, person 22 simultaneously moves to coordinate 44. After this operation, persons 1,2,31, 2, 3 are at coordinates 2,4,32, 4, 3, respectively.
  • Move person 33 from coordinate 33 to 22. Since person 11 is at coordinate 22, person 11 simultaneously moves to coordinate 33. After this operation, persons 1,2,31, 2, 3 are at coordinates 3,4,23, 4, 2, respectively.
  • Move person 33 from coordinate 22 to 11. After this operation, persons 1,2,31, 2, 3 are at coordinates 3,4,13, 4, 1, respectively.
4
2 4 5 9
2 4 5 1
11
6
5 2 9 1 6 8
1 4 7 0 9 3
13