Maximal intersection
Time: 1 s · Memory: 256 MB
- Sorting
Statement
You are given segments on a number line. Remove exactly one of them so that the length of the common intersection of the remaining segments is as large as possible. An empty intersection or a single point has length .
Input format
The first line contains (). The next lines contain and ().
Output format
A single number — the maximum length of the intersection.
Examples
Input
4 1 3 2 6 0 4 3 3
Output
1
Input
2 3 10 1 5
Output
7
Note
The intersection of segments is with length . When segment is removed, you need and of the others. If you sort the left ends in decreasing and the right ends in increasing order, the two largest and the two smallest are enough: if the removed segment gave the extreme value, take the second one. Another way is prefix and suffix maxima/minima.
Recomputing after removing each segment is — too slow. In the first example you should remove , not or : the answer is .
Source: Codeforces, problem 1029C.
