Registration
Limits: 2 sec., 256 MiB
This statement was translated automatically from Ukrainian.
Having successfully arrived at their destination in a Zaporozhets limousine, our heroes went to registration. Team registration is carried out in an infinitely long corridor, which, for simplicity, we will consider as the \(OX\) axis with positive coordinates.
There are \(n\) organizers conducting the registration process and \(m\) teams that need to be registered for the competition. The \(i\)-th organizer stands at coordinate \(a_i\), and the \(i\)-th team is at coordinate \(b_i\). In one second, each of the organizers can move one unit of distance to the left or to the right. A team is considered registered for the competition if it has been visited by at least one organizer. The registration process happens instantly. Several organizers are allowed to be in the same place at the same time.
Calculate the minimum time in which all teams can be registered for the competition.
Input
The first line contains two integers \(n\) and \(m\) — the number of organizers and teams, respectively.
The second line contains \(n\) numbers \(a_i\) — the coordinates of the \(i\)-th organizer.
The third line contains \(m\) numbers \(b_i\) — the coordinates of the \(i\)-th team.
Output
In a single line, output an integer — the minimum time for the organizers to register all teams.
Constraints
\(1 \le n, m \le 5 \cdot 10^4\),
\(1 \le a_i, b_i \le 10^9\),
for 12 tests the following constraints hold:
\(n = 2\),
\(1 \le m \le 10^3\).
Samples
| Input (stdin) | Output (stdout) |
|---|---|
| 2 3 4 1 1 7 3 | 3 |
| Input (stdin) | Output (stdout) |
|---|---|
| 2 4 3 8 1 4 2 12 | 4 |
Notes
In the first example, the 1st organizer will move to coordinate 7 in 3 seconds and register the second team, and the 2nd organizer will register the first team at the 0-th second, and the third team at the 2nd second. Thus, it takes 3 seconds to register all teams.
In the second example, it is advantageous for the 1st organizer to first register the second team, then the third and the first, which will take 4 seconds. At the same time, the 2nd organizer will register the fourth team in 4 seconds. Thus, it takes 4 seconds to register all teams.
Submit a solution
| Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
|---|
| Element Type | Created | Who | Problem | Compiler | Result | Time (sec.) | Memory (MiB) | # | Actions |
|---|