P27287 [AtCoder] Gacha
提高-
题目描述

Problem Description

On a number line, there are $ N $ gachas and $ N $ coins. The coordinate of the $ i $th gacha is $ A_i $, and the coordinate of the $ j $th coin is $ B_j $. Note that all these coordinates are positive integers. You are currently at coordinate $ 0 $ and can move freely on the number line.

  • You can pick up a coin when you move to its coordinate.
  • You can spin a gacha when you move to its coordinate by consuming one coin you picked up.

You want to spin all $ N $ gachas exactly once. Find the minimum distance required to achieve this goal.

Input Format

The input is given from the standard input in the following format:

$ N $ $ A_1 $ $ A_2 $ $\ldots$ $ A_N $ $ B_1 $ $ B_2 $ $\ldots$ $ B_N $

Output Format

Output the minimum distance required in one line.

Sample Input and Output

Sample Input #1

4
1 6 7 12
3 5 10 11

Sample Output #1

21

Sample Input #2

2
1 2
1 1000000000

Sample Output #2

1999999998

Notes/Hints

Constraints

  • $1 \leq N \leq 10^5$
  • $1 \leq A_i \leq 10^9$
  • $ A_i < A_{i+1} $ $(1 \leq i \leq N-1)$
  • $1 \leq B_j \leq 10^9$
  • $ B_j < B_{j+1} $ $(1 \leq j \leq N-1)$
  • All inputs are integers.

Sample Explanation 1

In this input example, you can achieve the minimum distance by following these actions:
1. Move to coordinate $ 3 $ and pick up the coin.
2. Move to coordinate $ 1 $ and spin the gacha.
3. Move to coordinate $ 5 $ and pick up the coin.
4. Move to coordinate $ 6 $ and spin the gacha.
5. Move to coordinate $ 10 $ and pick up the coin.
6. Move to coordinate $ 11 $ and pick up the coin.
7. Move to coordinate $ 12 $ and spin the gacha.
8. Move to coordinate $ 7 $ and spin the gacha.

Sample Explanation 2

It is possible that a gacha and a coin are at the same coordinate.

输入格式

见题面 Input 部分

输出格式

见题面 Output 部分

样例输入
4 1 6 7 12 3 5 10 11
样例输出
21