#define _GLIBCXX_DEBUG #include #include #include using namespace std; using ll = long long; using ull = unsigned long long; ull N, K; unsigned P; ull Solve(int x, vector A, vector B){ unsigned a, b, c; a = (B[0] <= x) ? distance(A.begin(), upper_bound(A.begin(), A.end(), x - B[0])) : 0; b = distance(A.begin(), lower_bound(A.begin(), A.end(), P - B[0])); c = distance(A.begin(), upper_bound(A.begin(), A.end(), P + x - B[0])); ull ret = a + c - b; for (unsigned i = 1; i < N; i++){ while(a > 0 && A[a-1] + B[i] > x ) a--; while(b > 0 && A[b-1] + B[i] >= P) b--; while(c > 0 && A[c-1] + B[i] > x + P) c--; ret += a + c - b; } return ret; } int main(){ scanf("%llu %llu %u", &N, &K, &P); vector A(N); for(int i = 0; i < N; ++i) scanf("%u", &A[i]); sort(A.begin(), A.end()); vector B(N); for(int i = 0; i < N; ++i) scanf("%u", &B[i]); sort(B.begin(), B.end()); int ok = P-1, ng = -1, mid; while(ok - ng > 1){ mid = ok + ng >>1; if(Solve(mid, A, B) >= K) ok = mid; else ng = mid; } printf("%d\n", ok); return 0; }