#include #include #include #include #include #include #include #include #include #include #include #include #include #define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl; #define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl; template inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } using namespace std; typedef long long ll; int main(){ ios::sync_with_stdio(false); cin.tie(0); cout << setprecision(10) << fixed; ll n, k, p; cin >> n >> k >> p; vector a(n), b(n); for(int i = 0; i < n; i++) cin >> a[i]; for(int i = 0; i < n; i++) cin >> b[i]; sort(a.begin(), a.end()); sort(b.begin(), b.end()); auto sect_elements = [&](int l, int r)->int{ if(r < l) return 0; if(r < 0) return 0; chmax(l, 0); int ans = upper_bound(b.begin(), b.end(), r)-lower_bound(b.begin(), b.end(), l); // cout << l << ',' << r << ' ' << ans << endl; return ans; }; auto calc = [&](int c){ ll ans = 0; for(int i = 0; i < n; i++){ ans += sect_elements(-a[i], c-a[i]); ans += sect_elements(p-a[i], p+c-a[i]); } return ans; }; if(calc(0) >= k){ cout << 0 << endl; return 0; } int l = 0, r = p-1; while(r-l > 1){ int c = (l+r)/2; if(calc(c) < k) l = c; else r = c; } cout << r << endl; }