#include using namespace std; using ll = long long; #ifdef LOCAL #include "debug.hpp" #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__) #else #define debug(...) #endif ll N, K, P; vector A, B; bool isOK(ll key) { ll cnt = 0; for (ll i = 0; i < N; i++) { ll a = A[i], temp = 0; if (a <= key) { ll not_temp = upper_bound(B.begin(), B.end(), P - a - 1) - B.begin(); ll temp2 = upper_bound(B.begin(), B.end(), key - a) - B.begin(); temp = N - (not_temp - temp2); } else { ll not_temp = upper_bound(B.begin(), B.end(), P - a - 1) - B.begin(); temp = upper_bound(B.begin(), B.end(), key + P - a) - B.begin(); debug(not_temp, temp); temp -= not_temp; } cnt += temp; debug(key, i, temp); } debug(key, cnt); return (cnt >= K); } int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> N >> K >> P; A.resize(N); B.resize(N); for (ll i = 0; i < N; i++) cin >> A[i]; for (ll i = 0; i < N; i++) cin >> B[i]; sort(A.begin(), A.end()); sort(B.begin(), B.end()); debug(A); // m 以下はK個以上ある? ll ok = P, ng = -1; while (abs(ok - ng) > 1) { ll mid = (ok + ng) / 2; if (isOK(mid)) ok = mid; else ng = mid; } ll ans = ok; cout << ans << '\n'; return 0; }