#include #include #define rep(i,n) for(ll i=0;i < (n);i++) #define rep2(i, s, n) for (ll i = (s); i < (n); i++) #define rrep(i,n) for(ll i=n-1;i>=0;i--) #define ff first #define ss second #define pb push_back #define ALL(a) (a).begin(),(a).end() typedef long long ll; const ll INF=1000000000000000000ll; const ll MOD=1000000007ll; const int MAX=5100000; using namespace std; // aよりもbが大きいならばaをbで更新する // (更新されたならばtrueを返す) template bool chmax(T &a, const T& b) { if (a < b) { a = b; // aをbで更新 return true; } return false; } // aよりもbが小さいならばaをbで更新する // (更新されたならばtrueを返す) template bool chmin(T &a, const T& b) { if (a > b) { a = b; // aをbで更新 return true; } return false; } ll N, K, P; vector A, B; bool solve(ll now){ ll t1, t2, t3, cnt = 0; rep(i, N){ t1 = now - A[i]; t2 = P - A[i]; t3 = t1 + P; if(now==4){cout << t1 << " " << P << " " << t2 << endl;} auto itr1 = lower_bound(ALL(B), t1); auto itr2 = lower_bound(ALL(B), t2); auto itr3 = lower_bound(ALL(B), t3); cnt += itr1 - B.begin(); cnt += (itr3 - B.begin()) - (itr2 - B.begin()); if(cnt >= K){return true;} } return false; } int main() { cin.tie(0); ios::sync_with_stdio(false); ll tmp; cin >> N >> K >> P; rep(i, N){cin >> tmp; A.push_back(tmp);} rep(i, N){cin >> tmp; B.push_back(tmp);} sort(ALL(A)); sort(ALL(B)); ll OK = P, NG = -1; while(OK - NG > 1){ ll mid = (OK+NG)/2; if(solve(mid)){OK = mid;} else{NG = mid;} } cout << NG << endl; return 0; }