module Main; // import BinaryHeap; // import BoolArray; // import CPLib.Arithmetic; // import CPLib.Bipartite; // import CPLib.Bits; // import CPLib.Convolution; // import CPLib.DSegtree; // import CPLib.Graph; import CPLib.IO; // import CPLib.LDSegtree; // import CPLib.LSegtree; // import CPLib.MaxFlow; // import CPLib.Memoize; // import CPLib.MinCostFlow; // import CPLib.Misc; // import CPLib.Monoid; // import CPLib.Segtree; // import CPLib.String; // import CPLib.Trait; // import CPLib.TwoSat; // import CPLib.UnionFind; // import CPLib.ZP; // import HashMap; // import HashSet; // import Math; main : IO () = ( let n = *read_i; let k = *read_i; let p = *read_i; let as = *read_is(n); let bs = *read_is(n); let as = as.sort; let bs = bs.sort; let count_below_in_bs : I64 -> I64 = |x| ( if x < 0 { 0 }; if x >= p { bs.@size }; bs.search_partition_point(|b| b < x) ); let count_below_in_mat : I64 -> I64 = |x| ( as.to_iter.map(|a| count_below_in_bs(x-a) - count_below_in_bs(-a) + count_below_in_bs(p+x-a) - count_below_in_bs(p-a) ).sum ); // loより小さい数の個数 < k // hiより小さい数の個数 >= k let lo = 0; let hi = p; let ans = loop((lo, hi), |(lo, hi)| if hi - lo == 1 { lo.break }; let mid = lo + (hi - lo) / 2; if count_below_in_mat(mid) < k { let lo = mid; (lo, hi).continue } else { let hi = mid; (lo, hi).continue } ); ans.to_string.println;; pure() );