n,k,p=map(int, input().split()) al=list(map(int, input().split())) bl=list(map(int, input().split())) al.sort() bl.sort() from bisect import bisect_left, bisect_right def check_cnt(val): res=0 for a in al: cnt1 = bisect_right(bl, val-a) # cnt2 = n - bisect_left(bl, p-a) cnt2 = n - bisect_right(bl, p-a-1) cnt3 = n - bisect_right(bl, val+p-a) res+=(cnt1+cnt2-cnt3) return res ok, ng = 0, p ok, ng = p-1, -1 while abs(ok-ng) > 1: mid = (ok+ng)//2 res = True # ... cnt=check_cnt(mid) # print('---',mid,cnt) if cnt>=k: ok = mid else: ng = mid print(ok)