#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; long long n, k, p; vector a, b; bool solve(long long x) { long long sum = 0; for (int i = 0; i < n; i++) { long long l1 = x - a[i]; auto itr = upper_bound(b.begin(), b.end(), l1); sum += int(itr - b.begin()); long long l2 = p - a[i]; long long r1 = p + x - a[i]; auto itr1 = lower_bound(b.begin(), b.end(), l2); auto itr2 = upper_bound(b.begin(), b.end(), r1); sum += int(itr2 - itr1); } if (sum < k) { return true; } else { return false; } } int main() { cin >> n >> k >> p; long long a1, b1; for (int i = 0; i < n; i++) { cin >> a1; a.emplace_back(a1); } for (int i = 0; i < n; i++) { cin >> b1; b.emplace_back(b1); } sort(a.begin(), a.end()); sort(b.begin(), b.end()); long long l = 0, r = p; while (r - l > 1) { long long mid = (l + r) / 2; if (solve(mid)) { l = mid; } else { r = mid; } } cout << r << endl; }