There are n people and k keys on a straight line. Every person wants to get to the office which is located on the line as well. To do that, he needs to reach some point with a key, take the key and then go to the office. Once a key is taken by somebody, it couldn't be taken by anybody else.
You are to determine the minimum time needed for all n people to get to the office with keys. Assume that people move a unit distance per 1 second. If two people reach a key at the same time, only one of them can take the key. A person can pass through a point with a key without taking it.
Input
The first line contains three integers n, k and p (1 ≤ n ≤ 1 000, n ≤ k ≤ 2 000, 1 ≤ p ≤ 109) — the number of people, the number of keys and the office location.
The second line contains n distinct integers a1, a2, ..., an (1 ≤ ai ≤ 109) — positions in which people are located initially. The positions are given in arbitrary order.
The third line contains k distinct integers b1, b2, ..., bk (1 ≤ bj ≤ 109) — positions of the keys. The positions are given in arbitrary order.
Note that there can't be more than one person or more than one key in the same point. A person and a key can be located in the same point.
Output
Print the minimum time (in seconds) needed for all n to reach the office with keys.
Examples
Input
2 4 50 20 100 60 10 40 80
Output
50
Input
1 2 10 11 15 7
Output
7
Note
In the first example the person located at point 20 should take the key located at point 40 and go with it to the office located at point 50. He spends 30 seconds. The person located at point 100 can take the key located at point 80 and go to the office with it. He spends 50 seconds. Thus, after 50 seconds everybody is in office with keys.
题意:在一条直线上, 给你n个人的位置,m把钥匙的位置,和一个办公室的位置;
让你求每个人只有拿到一把钥匙才能进入办公室,每个人进入办公室的时间等于他拿到钥匙的时间加上拿到钥匙后走到办公室的时间;没把要是只能拿一次;求全部人拿到要是的最短时间;
思路:一维坐标,求最短时间也就是最短距离,首先想到二分,重点是二分什么;
我们选择的值二分最短时间,那就要保证每个人都能进入办公室,并且人拿到钥匙的时间+拿到钥匙后走到办公室的使劲按要 <= 最短时间;看代码的注释把;
需要注意的是 L和R你需要开LL型的, 因为最大的时候 mid会爆int,
当check(mid)== false 的时候l = mid + 1, 然后l +r = 1e9 + 2e9就爆int了
ac码
#include
#include
#include
#include
#include
#include
#include
#include
#include