#include using namespace std; typedef long long ll; #define deb(...) logger(#__VA_ARGS__, __VA_ARGS__) template void logger(string vars, Args&&... values) { cout << vars << " = ["; string delim = ""; (..., (cout << delim << values, delim = ", ")); cout << "]" << " "; } ll n, k, p; vectorx, y, dp; bool solve(ll mid) { ll ans = 0; for (ll i = 0; i < n; i++) { ans += dp[p + mid - x[i]] - dp[p - x[i] - 1]; if (mid - x[i] >= 0) { ans += dp[mid - x[i]]; } } return (ans >= k); } void solve() { cin >> n >> k >> p; x.resize(n); y.resize(n); dp.resize(2 * p + 1); dp.assign(2 * p + 1, 0); for (ll i = 0; i < n; i++) { cin >> x[i]; } for (ll i = 0; i < n; i++) { cin >> y[i]; } for (int i = 0; i < n; i++) { dp[y[i]]++; } for (ll i = 1; i <= 2 * p; i++) { dp[i] += dp[i - 1]; } ll L = -1, R = p + 1; while (R - L > 1) { ll mid = (L + R) / 2; if (solve(mid))R = mid; else L = mid; } cout << R << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ll T; T = 1; //cin >> T; while (T--) { solve(); } }