import bisect def find_kth_smallest(): import sys input = sys.stdin.read data = input().split() idx = 0 N = int(data[idx]) idx += 1 K = int(data[idx]) idx += 1 P = int(data[idx]) idx += 1 A = list(map(int, data[idx:idx+N])) idx += N B = list(map(int, data[idx:idx+N])) idx += N B.sort() low = 0 high = P - 1 answer = 0 while low <= high: mid = (low + high) // 2 total = 0 for x in A: # Case 1: y <= mid - x target_case1 = mid - x if target_case1 >= 0: cnt1 = bisect.bisect_right(B, target_case1) else: cnt1 = 0 # Case 2: y >= (P - x) and y <= min(mid + P - x, P-1) lower_case2 = P - x upper_case2 = mid + P - x if upper_case2 > P - 1: upper_case2 = P - 1 if lower_case2 > upper_case2: cnt2 = 0 else: left = bisect.bisect_left(B, lower_case2) right = bisect.bisect_right(B, upper_case2) cnt2 = right - left total += cnt1 + cnt2 if total >= K: answer = mid high = mid - 1 else: low = mid + 1 print(answer) find_kth_smallest()