#include "bits/stdc++.h"
using namespace std;
#define int long long
void solve()
{
int n, k, pos;
cin >> n >> k >> pos;
vector<int> a(n), b(k);
for (int &x : a)
cin >> x;
for (int &x : b)
cin >> x;
sort(a.begin(), a.end());
sort(b.begin(), b.end());
auto check = [&](int mx)
{
vector<int> vis(k);
// left
for(int i = 0; i < n; ++i)
{
if(a[i] >= pos) break;
int ans = -1;
for(int j = 0; j < k; ++j)
{
if(vis[j]) continue;
int dis = max(0LL, a[i] - b[j]);
if(b[j] > pos) dis = b[j] - pos;
if(2 * dis + pos - a[i] <= mx)
{
ans = j;
break;
}
}
if(ans == -1) return false;
vis[ans] = 1;
}
// right
for(int i = n - 1; i >= 0; --i)
{
if(a[i] < pos) break;
int ans = -1;
for(int j = k - 1; j >= 0; --j)
{
if(vis[j]) continue;
int dis = max(0LL, b[j] - a[i]);
if(b[j] < pos) dis = pos - b[j];
if(2 * dis + a[i] - pos <= mx)
{
ans = j;
break;
}
}
if(ans == -1) return false;
vis[ans] = 1;
}
return true;
};
int lo = 0, hi = 2e9, md, ans = -1;
while(lo <= hi)
{
md = (lo + hi) >> 1;
if(check(md))
hi = md - 1, ans = md;
else
lo = md + 1;
}
assert(ans != -1);
cout << ans << '\n';
}
int32_t main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int tc = 1;
// cin >> tc;
for (int i = 1; i <= tc; ++i)
solve();
}
702A - Maximum Increase | 1656D - K-good |
1426A - Floor Number | 876A - Trip For Meal |
1326B - Maximums | 1635C - Differential Sorting |
961A - Tetris | 1635B - Avoid Local Maximums |
20A - BerOS file system | 1637A - Sorting Parts |
509A - Maximum in Table | 1647C - Madoka and Childish Pranks |
689B - Mike and Shortcuts | 379B - New Year Present |
1498A - GCD Sum | 1277C - As Simple as One and Two |
1301A - Three Strings | 460A - Vasya and Socks |
1624C - Division by Two and Permutation | 1288A - Deadline |
1617A - Forbidden Subsequence | 914A - Perfect Squares |
873D - Merge Sort | 1251A - Broken Keyboard |
463B - Caisa and Pylons | 584A - Olesya and Rodion |
799A - Carrot Cakes | 1569B - Chess Tournament |
1047B - Cover Points | 1381B - Unmerge |