import bisect def find_kth_smallest(): import sys input = sys.stdin.read().split() ptr = 0 N = int(input[ptr]) ptr +=1 K = int(input[ptr]) ptr +=1 P = int(input[ptr]) ptr +=1 A = list(map(int, input[ptr:ptr+N])) ptr += N B = list(map(int, input[ptr:ptr+N])) ptr += N A.sort() B.sort() def count(x): count1 = 0 j = len(B) - 1 for a in A: target = x - a if target < 0: continue while j >= 0 and B[j] > target: j -= 1 if j >= 0: count1 += (j + 1) count2 = 0 for a in A: lower = max(P - a, 0) upper = min(x + P - a, P - 1) if lower > upper: continue left = bisect.bisect_left(B, lower) right = bisect.bisect_right(B, upper) count2 += (right - left) return count1 + count2 low = 0 high = P - 1 answer = high # Initialize with the maximum possible value while low <= high: mid = (low + high) // 2 c = count(mid) if c >= K: answer = mid high = mid - 1 else: low = mid + 1 print(answer) find_kth_smallest()