We need to interpret problem: There are n people, k keys, all points on a line. Office at position p. Each person must pick up a key (any key) and then go to office. People move at speed 1 per second. Keys can be taken by only one person. People can pass through key points without taking. They need to minimize the maximum total time (makes sense: we need all to finish; we want min possible time such that there exists assignment of keys to people and ordering? Actually typical problem: minimize the time when all people have reached office with keys. Since they all operate simultaneously, the total time is the max over people of (time for that person to get key and go to office). We want to minimize this maximum by assignment. Keys are distinct. People are distinct. So it's an assignment problem: assign each person to a distinct key to minimize the max over persons of (|person position - key position| + |key position - office|). Actually careful: The person moves from initial position to key position, picks it up (instant? Presumably yes, taking time is zero), then moves to office. So total travel time = distance(person, key) + distance(key, office). Since movement is along line, these are absolute differences. So for each person i and key j, cost c(i,j) = |a_i - b_j| + |b_j - p|. We need to choose n distinct keys (since each key can be used at most once) and assign them to n people (bijection) to minimize the maximum cost. That's a classic assignment minimizing max cost, which can be solved via binary search on time T: check if we can assign keys to people such that each c(i,j) <= T.
But constraints: n up to 1000, k up to 2000, so we can do bipartite matching: n left, k right, edges where c(i,j) <= T. Then check if matching size = n. However, k could be 2000, n=1000, matching O(n * k) per check using Hungarian? But we can use simple max flow or greedy? Since it's bipartite with ordered? Possibly we can sort both lists and then greedily assign? Because cost is specific: |a_i - b_j| + |b_j - p|. That's not linear. But maybe we can transform.
We need to check feasibility for given T. Could be solved with a greedy: sort people positions and keys positions. But note that cost depends on person position and key position and office. Let's analyze.
c(i,j) = |a_i - b_j| + |b_j - p|. Consider two cases based on relative order of a_i, b_j, p.
We can think of person's path: from a_i to b_j to p. Equivalent to go from a_i to p but with a detour via b_j. If a_i and p are on same side of b_j, the detour adds extra distance. Actually total distance = |a_i - p| + 2 * distance from b_j to the segment between a_i and p? Let's derive:
Consider positions on line: Let x = a_i, y = b_j, o = p.
If y is between x and o (i.e., min(x,o) <= y <= max(x,o)), then |x - y| + |y - o| = |x - o|. Because you go from x to y to o, if y lies on the direct path, the total equals the direct distance. If y is outside the interval, say y < min(x,o) or y > max(x,o), then |x - y| + |y - o| = |x - o| + 2 * distance(y, nearest endpoint). For y left of both: y <= min(x,o) then |x-y|+|y-o| = (x-y)+(o-y) = (x+o-2y) = (x-o) + 2(o-y)? Actually careful: Assume x <= o? Let's do sign cases. But overall:
c(i,j) = |x-o| if y is between x and o, else = |x-o| + 2 * min(|y - x|, |y - o|?) Actually if y is outside interval, then the extra distance is 2 * distance from y to the interval's nearest endpoint. More precisely: c = |x-o| + 2 * d, where d = distance(y, [x,o]) = min(|y-x|,|y-o|) if y is outside? Actually if y is outside, say y < min(x,o): then distance from y to the interval = min(x,o) - y. And c = (x - y)+(o - y) = (x+o-2y) = (o-x) + 2*(x-y) if o >= x? Wait, let's set orientation: Without loss, assume x <= o. Then interval [x, o]. If y < x, then c = (x-y)+(o-y) = (o-x) + 2*(x-y) = |x-o| + 2*(x-y). Here distance from y to interval = x-y. If y > o, then c = (y-x)+(y-o) = (o-x) + 2*(y-o) = |x-o| + 2*(y-o). So yes c = |x-o| + 2 * distance(y, interval). So the cost is at least |x-o|, and extra if key not between person and office.
Thus to minimize max cost, we probably want to assign keys that are between the person and office when possible, else use keys that are not far outside.
But for feasibility check, we can treat it as bipartite matching with up to 1000x2000 edges if we test all combinations, which could be heavy but maybe okay if we do O(n*k) per check and do binary search (log up to 1e9 ≈ 30) gives 30 * 2e6 = 60e6 operations, maybe borderline but okay in C++? Python might be slower. But we can optimize by not building all edges; we can do greedy assignment after sorting something? Could we reduce to checking if there's a perfect matching in an interval graph? Let's think: Sort people and keys. But cost condition c(i,j) <= T imposes constraints: For a given person i, which keys are allowed? For each person i, solve |a_i - b_j| + |b_j - p| <= T. This is equivalent to two inequalities: Since both terms are nonnegative, we can think of bounding b_j relative to a_i and p.
We can rewrite: |b_j - a_i| <= T - |b_j - p|? Not simple. Let's solve by cases.
We want b_j such that |b_j - a_i| + |b_j - p| <= T. This is the condition that the distance from b_j to two points a_i and p (the sum of distances) is at most T. This defines a region on the line: the set of points y such that d(y, a_i) + d(y, p) <= T. This is known as an "ellipse" interval: For two fixed points, the set of points with sum of distances ≤ T is the closed interval between them if T >= |a_i - p|, plus extensions? Actually on a line, the condition |y - a_i| + |y - p| ≤ T. If T < |a_i - p|, then there is no solution because the minimum sum is exactly |a_i - p| when y between them. So we need T >= |a_i - p| for any key to be possible (but could be that key outside? Actually minimum sum is |a_i-p|, so if T < |a_i-p| then impossible for that person regardless of key. So we need T >= max_i |a_i-p|? Not necessarily because we could assign key? Wait: The minimal possible cost for person i is |a_i-p| (by picking a key exactly on the path). So if T < |a_i-p|, that person cannot finish within T. So necessary condition: T >= max_i |a_i-p|? Actually not; each person individually must have some key such that cost <= T. The minimal cost for i is |a_i-p|, but we need existence of a key that yields cost <= T. So we need T >= min_j c(i,j). That min_j is not necessarily |a_i-p| because if there is a key exactly on the path, then min_j = |a_i-p|. But if no key lies between a_i and p, min could be larger. However, the problem doesn't guarantee any key lies between, so we need to consider min over keys. So T must be at least min_j c(i,j) for each i, but overall we need to assign distinct keys. So binary search with matching is safe.
But we can try to derive interval constraints: The condition |y - a| + |y - p| <= T defines an interval: If we denote L = min(a,p), R = max(a,p). Then the sum is (R-L) + 2d where d is distance from y to [L,R] (if outside). So condition becomes (R-L) + 2d <= T => d <= (T - (R-L))/2. So if T < R-L, no solution (d negative). So we need T >= R-L. Then allowed y are those with distance to [L,R] <= (T - (R-L))/2. That means y ∈ [L - delta, R + delta] where delta = (T - (R-L))/2 >= 0. So it's a single contiguous interval: [ (a_i + p - T)/2 ? Actually compute precisely.
Define a = a_i, o = p. Let L = min(a,o), R = max(a,o). Then condition: y ∈ [L - delta, R + delta] with delta = (T - (R-L))/2.
But careful: This interval is derived assuming T >= R-L. Yes. Also note that if T is large, interval expands symmetrically? Actually it's symmetric around the midpoint? Not exactly symmetric if a and o positions not symmetric? But the condition |y-a|+|y-o| ≤ T yields: For y ≤ L, condition becomes (L - y)+(R - y) = R+L - 2y ≤ T -> y ≥ (R+L - T)/2. For y ≥ R, condition becomes (y - L)+(y - R) = 2y - (R+L) ≤ T -> y ≤ (R+L + T)/2. For y in [L,R], condition holds automatically? Actually if y ∈ [L,R], then sum = R-L ≤ T, so if T ≥ R-L then condition holds. So overall allowed y is: y ∈ [ (a+o - T)/2, (a+o + T)/2 ]? Let's test: For a <= o: a=0, o=10. Then condition: if y between 0 and 10, allowed if T>=10. The interval we compute: (a+o - T)/2 = (10 - T)/2, (a+o + T)/2 = (10+T)/2. For T=10, that gives (0,10). That's correct. For T=20, gives (-5,15). But earlier we derived [L-delta,R+delta] with L=0,R=10,delta=(20-10)/2=5 => [-5,15]. And (a+o - T)/2 = (10-20)/2 = -5, (a+o+T)/2 = (30)/2=15. So yes, it matches: allowed y ∈ [ (a+o - T)/2 , (a+o + T)/2 ]. But is that always? Let's verify for a>o (a on right). Suppose a=10, o=0: then a+o=10 still. The condition should be symmetric: it's same because sum of distances is symmetric. So indeed the allowed set is interval centered at (a+o)/2 with half-length T/2. But note that the condition automatically ensures T >= |a-o|? Actually if T < |a-o|, then (a+o - T)/2 > (a+o + T)/2? The interval would be reversed, meaning no real y. So condition T >= |a-o| necessary.
Thus for each person i, given T, the allowed keys are those b_j such that:
(a_i + p - T)/2 <= b_j <= (a_i + p + T)/2.
But wait, is this always equivalent? Let's test with some numbers: a_i = 20, p=50, T=30. Then a_i+p=70, half: 35. (70-30)/2=20, (70+30)/2=50. So allowed b_j ∈ [20,50]. According to our earlier reasoning: If key is between 20 and 50, cost = |20- key|+|key-50| = (key-20)+(50-key)=30, exactly T. So yes. What about b_j=60? Then cost = 40+10=50>30, not allowed. Interval says [20,50], so 60 not allowed. b_j=10? cost=10+40=50>30, not allowed. Good.
But what about a_i=100, p=50, T=50. a_i+p=150. (150-50)/2=50, (150+50)/2=100. Allowed [50,100]. Check b_j=80: cost = |100-80|+|80-50| =20+30=50 <=50. b_j=40: cost = 60+10=70>50 not allowed. Interval says 40 is less than 50, not allowed. Good.
Now consider T=40, a_i=100, p=50: allowed interval (150-40)/2=55, (150+40)/2=95 => [55,95]. Check b_j=60: cost = 40+10=50>40? Actually 100-60=40, 60-50=10 => total 50 >40, not allowed. So interval says 60 not in [55,95]? 60 is less than 55? Wait 60 is between 55 and 95? 60 >55, 60<95, so allowed? But we computed cost=50>40, so shouldn't be allowed. Something wrong. Let's recalc: a_i=100, p=50, T=40. Then a_i + p =150. Compute (150-40)/2 =55, (150+40)/2=95. So interval is [55,95]. Check b_j=60: According to our earlier formula c = |100-60|+|60-50| = 40+10 = 50 > 40, so indeed not allowed, but it lies in [55,95]? 60 is in [55,95], contradiction. So our derived interval is not correct? Let's verify the transformation.
We derived that condition |y-a| + |y-p| ≤ T is equivalent to y ∈ [ (a+p - T)/2 , (a+p + T)/2 ]? That seems symmetric, but we must check the case where a > p (i.e., a to the right of p). Let's do the inequality carefully.
General absolute sum: |y - a| + |y - p| ≤ T. This is a piecewise linear condition. Let's set A = a, B = p. Without loss, assume A ≤ B? Not necessary. Let's define L = min(A,B), R = max(A,B). Then we already had condition: if T < R-L then no solution; if T ≥ R-L, then allowed y are those with distance to interval ≤ (T - (R-L))/2, i.e., y ∈ [L - delta, R + delta] where delta = (T - (R-L))/2.
Now, is that interval the same as [ (A+B - T)/2 , (A+B + T)/2 ]? Let's compute L and R in terms of A and B. If A ≤ B, then L=A, R=B, so A+B = L+R. Then L - delta = A - (T - (B-A))/2 = (2A - (T - (B-A)))/2 = (2A - T + (B-A))/2 = (A + B - T)/2. And R + delta = B + (T - (B-A))/2 = (2B + T - (B-A))/2 = (B + A + T)/2? Let's compute: 2B + T - (B-A) = 2B + T - B + A = B + A + T, divided by 2 = (A+B+T)/2. So indeed if A ≤ B, interval = [ (A+B-T)/2, (A+B+T)/2 ]. So it's consistent.
Now take A=100, B=50, so A > B. Then we can't directly use the formula with L,R? But we can treat them symmetrically: The condition is symmetric in A and B: |y-A|+|y-B| is symmetric. So it should be the same interval centered at (A+B)/2 with half-length T/2. But our earlier test shows contradiction. Let's compute with A=100, B=50, T=40. Then (A+B-T)/2 = (150-40)/2=55, (A+B+T)/2 = (150+40)/2=95. So interval predicted = [55,95]. But check y=60: is |60-100|+|60-50| = 40+10=50 ≤ 40? No. So why does the interval method fail? Let's recalc using the L,R method for A=100, B=50. Then L = min(100,50) = 50, R = max(100,50) = 100. R-L = 50. Since T=40 < 50, the condition T ≥ R-L is false. So there is no solution at all for any y? But we see that if y is between? Actually the minimum possible sum is 50 (when y between 50 and 100). Since T=40 < 50, it's impossible for any y to satisfy. So indeed there should be no keys allowed for person i when T=40. But our interval gave [55,95] which is non-empty. That indicates our derived formula assuming T ≥ R-L is necessary; if T < R-L, then the interval should be empty. Our simplified formula [ (A+B-T)/2, (A+B+T)/2 ] gives non-empty if (A+B+T)/2 >= (A+B-T)/2 always true, but if T < |A-B|, then the lower bound may be larger than the upper bound? Let's check: For A=100, B=50, (A+B-T)/2 =55, (A+B+T)/2=95, and indeed 55 <= 95, so it's not empty. But we know condition impossible. So the simplified interval is only valid when T >= |A-B|. If T < |A-B|, the condition cannot be satisfied, but the interval still yields a positive length. Actually we need to impose also that the interval actually overlaps with the real line but also that any y in that interval must also satisfy? Let's test the inequality directly: For A=100, B=50, the inequality is |y-100|+|y-50| ≤ 40. The left side is at least 50 for all y, so no solution. So the set is empty. Our derived interval [55,95] is not correct for this case because we derived it under assumption that T >= R-L. So the correct allowed set is empty if T < |A-B|. So we must incorporate that condition.
Thus for each person i, allowed keys are those b_j such that |b_j - a_i| + |b_j - p| ≤ T. That's equivalent to: T must be at least |a_i - p|? Actually it's necessary that T >= |a_i - p| for there to exist any b_j? Actually it's necessary for existence of any b_j, but the key could be exactly at a_i or p? Let's see: minimum sum = |a_i - p| when b_j lies between a_i and p (including endpoints). So if T < |a_i - p|, then no key can satisfy the inequality. So the person cannot finish within T. So for feasibility we need T >= max over persons of something? Not exactly max, but for each person there must be some key. So if any person has |a_i - p| > T, then impossible. But if T >= |a_i-p|, then there exists at least one key? Only if there is a key located between a_i and p (or possibly outside but sum still ≤ T if T larger). Actually if T >= |a_i-p|, but all keys might be far away such that the sum > T. So even if T >= |a_i-p|, the person might not have any key within the allowed interval (which now is non-empty). So we still need to check existence of a key within the interval [L_i, R_i] where L_i = (a_i + p - T)/2, R_i = (a_i + p + T)/2, but also we need to ensure that the interval is defined only if T >= |a_i-p|. However, if T < |a_i-p|, the interval is still mathematically defined but actually the condition cannot be satisfied because the minimum sum exceeds T. In that case, the allowed set is empty. So we can define allowed keys as those b_j satisfying (a_i + p - T)/2 <= b_j <= (a_i + p + T)/2, but only if this lower bound <= upper bound and T >= |a_i - p|? Actually if T < |a_i-p|, the interval [L,R] still may be non-empty? Let's check: a_i=100, p=50, |a-p|=50. T=40 <50. L=55, R=95, L <= R. But condition fails for any y because the minimal sum is 50 >40. So the inequality is not equivalent to L <= y <= R unless T >= |a-p|. The derivation of the interval assumed y between L and R? Actually we derived from piecewise conditions, and we got that for y ≤ L (i.e., y <= min(a,p)), condition becomes y >= (a+p - T)/2. That derived inequality is correct but only applicable when y <= min(a,p). But the condition that there exists y satisfying the full inequality is equivalent to the union of conditions from each region. The union yields that y must satisfy simultaneously: if y <= min(a,p) then y >= (a+p - T)/2; if min(a,p) <= y <= max(a,p) then automatically? But that region only exists if T >= max(a,p)-min(a,p) because otherwise the condition in that region would be max(a,p)-min(a,p) <= T? Actually inside the interval, the sum is exactly max-min, which is |a-p|. So for y in [L,R] (the inner interval), the condition reduces to |a-p| <= T. So if T < |a-p|, then no y in [min(a,p),max(a,p)] satisfies. So the only possible y are outside, but then the inequalities from outside also impose bounds that may intersect with outside region, but also need to satisfy the region condition (y <= min or y >= max). So let's solve correctly:
For A=100, B=50, L= min=50, R=100.
Case 1: y ≤ 50: condition: (50 - y)+(100 - y) = 150 - 2y ≤ 40 => 2y ≥ 110 => y ≥ 55. But this conflicts with y ≤ 50, so no solution in this case.
Case 2: 50 ≤ y ≤ 100: condition: 100-50 = 50 ≤ 40? Not satisfied. So no solution.
Case 3: y ≥ 100: condition: (y-50)+(y-100)=2y-150 ≤ 40 => 2y ≤ 190 => y ≤ 95. But y ≥ 100 conflicts. So no solution. So indeed empty set.
Thus the correct allowed set is: if T < |a-p|, empty. Else, allowed y ∈ [ (a+p - T)/2 , (a+p + T)/2 ]? But we need to ensure that this interval actually falls within the feasible region? Actually for T >= |a-p|, we can check that (a+p - T)/2 ≤ min(a,p) and (a+p+T)/2 ≥ max(a,p)? Let's verify: For a=100, p=50, |a-p|=50. If T=60 >=50, then L = (150-60)/2=45, R=(150+60)/2=105. And indeed any y in [45,105] will satisfy? Let's test y=45: cost = |100-45|+|45-50| =55+5=60 <=60? 55+5=60, yes. y=105: |100-105|+|105-50|=5+55=60. y=80: 20+30=50 ≤60. So interval works. Also note that for T exactly 50, L=50, R=100. y=50: cost=50+0=50; y=100:0+50=50; y=70:30+20=50; all good. So for T >= |a-p|, the interval formula gives the correct allowed set. So we can define allowed keys for person i when T >= |a_i - p| as b_j ∈ [L_i, R_i] where L_i = (a_i + p - T)/2, R_i = (a_i + p + T)/2. If T < |a_i - p|, then no keys allowed.
Thus for feasibility check, we need to assign distinct keys to people, each key can be used by at most one person, each person must be assigned a key within their interval (if T >= |a_i-p|, else impossible). This becomes an interval assignment problem: each person gives an interval of acceptable key positions. And keys are points on line. We need to check if we can match each person to a distinct key within its interval. This is like checking if there exists a matching in a bipartite graph that is an "interval graph" (each person interval can be matched to any key point within it). Since intervals are one-dimensional, we can use a greedy algorithm: sort people by something (e.g., right endpoint of their interval) and assign keys in order. But careful: People have intervals that may overlap; the classic problem of assigning points to intervals with each point used at most once can be solved greedily by sorting intervals by right endpoint and iterating over sorted keys, assigning to the earliest finishing interval that hasn't been assigned yet (or similar). Actually standard: Given intervals and points, can we assign each interval a distinct point? That is like checking if the bipartite matching exists when intervals are on one side and points on the other, edges if point lies in interval. This can be solved by sorting both intervals and points, then using a priority queue: iterate points in increasing order, add all intervals that start at <= point, then assign the point to the interval with smallest right endpoint (that is still >= point). This yields a maximum matching and can determine if all intervals can be assigned.
But we have also condition that if T < |a_i-p| then no interval. So we can incorporate that.
Thus we can avoid explicit bipartite matching O(n*k) by using greedy matching algorithm O((n+k) log n) per check.
Let's validate: The problem reduces to: For each person i, define interval [L_i, R_i] if T >= |a_i-p|, else no interval -> impossible. Then we need to assign distinct keys (as points) to these intervals, each key can be used at most once. Keys are given as list of coordinates. This is exactly the "assign each interval a point" problem.
But careful: The intervals derived from the condition might have non-integer endpoints (since (a_i + p - T)/2 may not be integer). But keys are integer positions, but that's fine; we treat intervals as real intervals. Since we only care about integer keys, we can keep as floating or rational, but we can also multiply by 2 to avoid fractions: define L_i' = a_i + p - T, R_i' = a_i + p + T. Then condition b_j ∈ [L_i, R_i] <=> 2b_j ∈ [a_i + p - T, a_i + p + T]? Actually: (a_i + p - T)/2 <= b_j <= (a_i + p + T)/2 is equivalent to a_i + p - T <= 2b_j <= a_i + p + T. So we can define transformed key coordinate: 2b_j. But T is integer, a_i and p integers, so everything integer. However careful: Since T may be odd, then a_i + p - T may be odd/even but 2b_j is even. The inequality a_i + p - T <= 2b_j <= a_i + p + T. This is equivalent to b_j being integer? Actually if we multiply by 2, we get integer bounds. Then the condition becomes 2b_j must lie in that inclusive interval. Since 2b_j is even, and bounds may be odd, the integer check works fine: b_j satisfies if and only if (a_i + p - T) <= 2b_j <= (a_i + p + T). But is it equivalent? Suppose L_i = 2.5, b_j=2, then 2b_j=4, and a_i+p-T maybe 5? Let's test with actual numbers: a_i=20, p=50, T=30 => a_i+p-T=40, a_i+p+T=100. So interval for 2b_j: [40,100]. For b_j=20 => 2b_j=40, satisfies, so allowed (b_j=20 allowed? Original: L_i=(70-30)/2=20, R_i=(70+30)/2=50, b_j=20 in [20,50] allowed). For b_j=21 => 2b_j=42, in [40,100], allowed? Original: 21 in [20,50] yes. For b_j=19 => 2*b_j=38 not in [40,100], not allowed, original: 19 not in [20,50]? 19 <20, so not allowed. So yes equivalence holds. However, what about fractional b_j? Keys are integer positions, so b_j integer. So we can just keep intervals as rational but use integer comparisons via the transformed inequality to avoid floating.
But note: The derivation assumed T >= |a_i-p|? Because if T < |a_i-p|, the interval [L_i, R_i] as defined by these formulas might still have some keys? But as we saw for a_i=100,p=50,T=40 gives L_i=55,R_i=95, but original condition fails. However if we check the transformed inequality: a_i+p-T=150-40=110, a_i+p+T=150+40=190. Condition a_i+p-T <= 2b_j <= a_i+p+T would be 110 <= 2b_j <= 190. For b_j=60, 2*b_j=120, satisfies. But original condition is not satisfied. So the transformation only holds when T >= |a_i-p|? Actually the equivalence we derived earlier assumed T >= |a_i-p|. Let's check the derivation: Starting from inequality |b_j - a_i| + |b_j - p| <= T. We can manipulate to get double inequality? Let's derive properly.
We want to find condition on b_j. For any real numbers a, o, T, the set of y satisfying |y-a|+|y-o| <= T is: if T < |a-o|, empty; else y in [ (a+o - T)/2, (a+o + T)/2 ].
So the correct condition is: if T >= |a-o| then (a+o - T)/2 <= y <= (a+o + T)/2. So we need to enforce both T >= |a_i-p| and the interval bounds.
Thus for feasibility, we must check for each person: if T < |a_i-p|, then person cannot be satisfied. So we can early reject. If T >= |a_i-p|, then we compute L_i = (a_i + p - T)/2, R_i = (a_i + p + T)/2. Since we want to use integer arithmetic, we can keep as fractions. But we need to sort intervals, compare with key positions. Since we may have fractional endpoints, we need to handle carefully.
Alternatively, we could avoid intervals and directly check condition using absolute sums while greedy? The greedy algorithm may need to compare keys and intervals. But we can implement the matching without intervals: For each key, we could determine which persons can use it based on T, but that might be O(n*k). However we want O((n+k) log n). So better to use intervals.
But careful: The intervals derived are only valid when T >= |a_i-p|. So we will only create intervals for persons with |a_i-p| <= T. If any person has |a_i-p| > T, we return false.
Now, is it sufficient to have the intervals and perform greedy assignment? Let's test on examples.
Example 1: n=2, k=4, p=50. People: 20,100. Keys: 60,10,40,80.
We need minimal T. Let's compute intervals for given T.
We'll test T=50 (answer). For person at 20: |20-50|=30 <=50, so interval: L=(70-50)/2=10, R=(70+50)/2=60 => [10,60]. For person at 100: |100-50|=50 <=50, L=(150-50)/2=50, R=(150+50)/2=100 => [50,100]. Keys: 10,40,60,80. Assign: person20 can take 10,40,60; person100 can take 50? But 50 not in keys; keys: 10,40,60,80 -> 80 is in [50,100], also 60? 60 is not in [50,100]? Actually 60 is <=100 and >=50, yes 60 is in [50,100]? 60 between 50 and 100 inclusive, so person100 can take 60 as well. But we need distinct. Greedy: sort intervals by right endpoint? Person20: [10,60], Person100: [50,100] (right 100 > 60). Sort keys ascending: 10,40,60,80. Use algorithm: for each key in order, assign to the interval with the smallest right endpoint that is not yet assigned and whose left <= key <= right? Or typical greedy: sort intervals by left? Actually standard algorithm for matching points to intervals (each point can be assigned to at most one interval) is:
- Sort intervals by their left endpoint (or right? I recall the problem: given intervals and points, can we assign each interval a distinct point? This is like checking if the maximum matching size equals number of intervals. A greedy that works: sort intervals by right endpoint (ascending). Sort points ascending. Iterate over intervals in order of increasing right endpoint. For each interval, choose the smallest available point that is >= left endpoint. If none, fail. This is optimal because it gives the interval with earliest deadline the earliest possible point, leaving larger points for later intervals with later deadlines. This works when each point can be used at most once, and points are on the real line. Yes, that's classic: assign tasks (intervals) to time slots (points) where each task requires a time slot at some time within its interval. Greedy: sort tasks by deadline (right endpoint), assign each the earliest available time slot not earlier than its start. That yields maximum assignments if slots are sorted. So we can apply.
But note: intervals are not necessarily "tasks" that require a specific time; points are keys positions. However, the structure is same: we have intervals on the line (allowed key positions) and we need to assign each interval a distinct point from a given set. Greedy works: sort intervals by right endpoint; sort keys ascending. Maintain pointer over keys. For each interval in order of increasing R, we pick the smallest key >= L that hasn't been used yet. If none, fail.
Alternatively, we can iterate over keys and assign to the interval with smallest R that contains the key? There are variations but O((n+k) log n) using a min-heap of intervals' right endpoints that are currently open when scanning keys.
Let's adopt scanning with keys sorted, and intervals sorted by left endpoint. As we sweep through keys from low to high, we maintain a min-heap of right endpoints of intervals whose left endpoint <= current key. Then for each key, we assign it to the interval with the smallest right endpoint (since that interval is most constrained). This is a typical algorithm for matching intervals to points.
Algorithm:
- Preprocess: for each person i, if T < |a_i-p| return false.
- else compute left_i = (a_i + p - T)/2, right_i = (a_i + p + T)/2. Since we need to compare with key positions, we can keep as rational numbers, but better to avoid fractions by doubling: Define left_i = a_i + p - T, right_i = a_i + p + T, and treat key as 2b_j. Then condition becomes left_i <= 2b_j <= right_i. However, this is valid only when T >= |a_i-p|? Let's check: For T < |a_i-p|, left_i might be > right_i? Actually if T < |a_i-p|, left_i > right_i? For a_i=100, p=50, left_i =150-40=110, right_i=150+40=190, left_i < right_i, so no. So the doubled representation does not automatically capture the T>=|a-p| condition. So we need to still enforce that separately. But we can also note that if T < |a_i-p|, then the interval [ (a+p-T)/2, (a+p+T)/2 ] may still exist but does not correspond to valid points because the condition inside the interval fails? Actually the condition derived for y inside [min,max] requires T >= |a-p|. So it's not just the interval; the inequality holds only if T >= |a-p|. So we must check that.
Thus algorithm: For given T, compute for each person i: if |a_i - p| > T, then impossible for this T. Else compute L_i = (a_i + p - T)/2, R_i = (a_i + p + T)/2. Since a_i, p, T are integers, L_i and R_i can be half-integers. To avoid floats, multiply by 2: L2_i = a_i + p - T, R2_i = a_i + p + T. Then for a key at b_j, condition becomes L2_i <= 2b_j <= R2_i. However, note that L_i and R_i are inclusive endpoints. When we double, we maintain inclusivity. Since all numbers are integers, 2b_j is even integer, and L2_i, R2_i are integers. So condition holds exactly. But what about the requirement T >= |a_i-p|? In the case T < |a_i-p|, there might still be keys that satisfy L2_i <= 2b_j <= R2_i, but they wouldn't satisfy the original absolute sum condition. For example: a=100, p=50, T=40 => L2=110, R2=190. Key b=60 => 2b=120 in [110,190], but original cost 50 > 40. So why does the doubled inequality not capture the condition? Because the derivation from |y-a|+|y-o| <= T to the interval condition assumed T >= |a-o|. If T < |a-o|, the interval [L_i,R_i] is not the solution set. Actually the inequality |y-a|+|y-o| <= T is equivalent to (a+o - T) <= 2y <= (a+o+T) AND ??? Let's solve directly: For any y, |y-a|+|y-o| <= T. This can be rewritten as two inequalities? Actually the condition is equivalent to: (a+o - T) <= 2y <= (a+o+T) AND something about the distance between a and o? Let's derive algebraically:
Consider both sides square? There is known equivalence: |y-a|+|y-o| <= T is equivalent to both: (a+o - T) <= 2*y <= (a+o+T) AND |a-o| <= T. Because the latter ensures the region between a and o is not empty? Let's derive:
We have the piecewise expression:
- If y <= min(a,o): condition = (a+o) - 2y <= T -> 2y >= a+o - T -> y >= (a+o - T)/2.
But also must have y <= min(a,o). So solution in this region is y in [ (a+o - T)/2, min(a,o) ] provided (a+o - T)/2 <= min(a,o). This holds if T >= max(a,o) - min(a,o)? Actually (a+o - T)/2 <= min(a,o) is equivalent to a+o - T <= 2min(a,o) => a+o - 2min(a,o) <= T => (max(a,o) - min(a,o)) <= T? Let's derive: Since a+o = min+max, then a+o - 2min = max - min. So condition is max-min <= T. So if T < max-min, then (a+o - T)/2 > min(a,o). So in that case, the lower bound is above the region, so no y in this region. Similarly for y >= max(a,o), condition gives y <= (a+o+T)/2, and also need y >= max(a,o). That yields condition (a+o+T)/2 >= max(a,o) => a+o+T >= 2max => (max+min) + T >= 2*max => T >= max-min. So again, if T < max-min, no y in that region. In the middle region (between min and max), condition is |a-o| <= T, which again requires T >= max-min. So overall, the inequality |y-a|+|y-o| <= T has solutions iff T >= |a-o|, and then the solution set is exactly the interval [ (a+o - T)/2, (a+o + T)/2 ] (and that interval automatically lies within? Actually it's guaranteed to contain the whole [min, max] region because when T >= |a-o|, (a+o - T)/2 <= min and (a+o+T)/2 >= max). So indeed, the condition is equivalent to: T >= |a-o| AND (a+o - T)/2 <= y <= (a+o+T)/2.
Thus the correct condition is: T >= |a_i - p| AND a_i + p - T <= 2*y <= a_i + p + T. Because we double the inequality.
Thus for each person i, we need T >= dist_i where dist_i = |a_i - p|, and then key 2*b_j must lie between L2_i and R2_i inclusive.
So for feasibility test, we need to check two things: For each person, T must be at least dist_i. Then we have intervals on 2*b_j.
So algorithm for a given T:
- If for any person |a_i - p| > T: return false.
- Otherwise, for each person, compute left_i = a_i + p - T, right_i = a_i + p + T.
- We need to assign each person a distinct key b_j such that left_i <= 2*b_j <= right_i.
- Since 2*b_j is an integer, we can work with key values multiplied by 2.
Now the intervals are on the integer line (with integer endpoints). They may overlap, and we need to assign each interval a point (the doubled key) from a set of points (2*b_j). This is classic.
We can sort intervals by left (or right) and use greedy.
One nuance: The intervals [left_i, right_i] are inclusive and may be large. The greedy matching algorithm (assign points to intervals sorted by right endpoint) works because it's essentially a bipartite matching where intervals are "tasks" and points are "time slots". The standard greedy: sort intervals by right endpoint ascending, sort points ascending. Then iterate intervals: for each interval, find the smallest point >= left_i that is not yet used. If none, fail. This runs in O(n log n + k log k) if we use a balanced BST to find the next available point, but we can use two-pointer technique because we only need to pick the smallest available point >= left_i, and we are scanning intervals in order of right endpoint. However, the points are static; we can sort them and maintain an index that moves forward. But careful: If we process intervals sorted by right endpoint, and for each interval we need the smallest point >= left_i, we can iterate through sorted points and assign. But if we simply use a pointer to the smallest unused point that is >= left_i, we need to know if that point is also <= right_i. Since intervals sorted by right endpoint, it's optimal to assign the earliest possible point. The typical algorithm:
sort intervals by right endpoint
sort points
j = 0
for each interval in sorted order:
find the smallest point >= left_i (starting from current j maybe) that is not used. If found and point <= right_i, assign it and mark used (and increment some pointer). else fail.
But to efficiently find the smallest unused point >= left_i, we could use a set (like a balanced BST) of available points, and for each interval, we query the lower bound of left_i. Since we might assign and remove points, a set works well. Complexity O((n+k) log k) which is fine (k up to 2000, n up to 1000, log factor trivial). Alternatively, we could use a greedy sweeping with min-heap as described earlier.
Given constraints small, we could also do bipartite matching using DFS (Hungarian not needed; just maximum bipartite matching with O(nk) per check). That's nk up to 2e6, times 30 = 60e6, which might be okay in Python if optimized? 60e6 operations maybe borderline but with early pruning could pass? But using greedy is simpler and more efficient.
But careful: Is the greedy algorithm correct for this problem? The bipartite graph is a convex bipartite graph? Intervals to points, it's an "interval scheduling" matching problem. The greedy algorithm of sorting intervals by right endpoint and assigning the smallest available point >= left is indeed optimal for maximizing number of matched intervals (or checking if all intervals can be assigned) when points are all distinct and we have to assign each point at most one interval. This is analogous to the problem of scheduling tasks with given windows, each task requires one time slot within its window. The greedy algorithm (earliest deadline first) is optimal. The proof: Exchange argument.
Thus we can adopt that.
Implementation steps for feasibility(T):
- For each person i:
if abs(a_i - p) > T: return False
left_i = a_i + p - T
right_i = a_i + p + T
- Build list of intervals (left_i, right_i) for all i.
- Sort intervals by right_i (ascending).
- Sort keys, but we need 2*b_j. Compute double_keys = [2 * b_j for b_j in keys] and sort them.
- We need to assign points to intervals. We'll use a multiset (e.g., list) of available keys and for each interval pick the smallest key >= left_i, and also <= right_i. Since intervals are sorted by right, we can maintain a pointer through the sorted double_keys? But the keys need to be assigned uniquely. We can do:
Initialize a pointer index = 0, but that doesn't work because keys might be used out of order. Instead use a data structure: e.g., use a list of keys and mark used, but searching for smallest unused >= left each time could be O(k) per interval, O(nk) worst-case, which is okay (nk=2e6) for each T? Possibly fine. But binary search with a sorted list and removing used keys is tricky. Simpler: Use a boolean array used of size k. For each interval, loop over keys from beginning to find first unused key with 2b_j >= left_i and <= right_i. Since k up to 2000, and n up to 1000, O(nk) = 2e6 per check, times 30 = 60e6, which in Python might be acceptable if optimized (60 million operations may be okay within 2 seconds? Possibly borderline but likely fine given constraints are small. However we also have sorting O(n log n + k log k) each check, but that's fine.
But we can do better: Use a binary search to find the first key >= left_i among unused, but then need to skip used ones. Could use a sorted list of available keys and pop using bisect. Python's bisect on a sorted list but removal is O(k) unless using something like 'sortedcontainers' but not allowed. Could use a heap? Not straightforward.
Given k <= 2000, O(n*k) per check is 2e6 per check, with up to ~30 checks = 60e6, which might be okay in 2 seconds if coded efficiently in Python, especially since operations are simple integer comparisons. However, we also have to compute intervals for each person: O(n). So overall maybe 60 million simple loops. Python can handle ~50-100 million operations per second? Actually Python's loops are slower; 60 million might be around 1-2 seconds if optimized and not too many function calls. But we can reduce number of checks by using a better binary search range? The answer is maximum over some combination, so we can binary search between 0 and some upper bound. Upper bound could be max over (abs(a_i - p) + max distance to nearest key?) But we can set upper bound as max possible time: maximum over all i,j of cost? That could be up to 2e9 (since positions up to 1e9). But binary search on 0..2e9 ~ 31 steps. That's fine.
But we can also try to compute answer directly via sorting and two-pointer? There might be a more direct solution: Because we need to minimize max over assigned costs, this is a classic "assign people to keys" on line with office. However, because cost is not simply distance to key plus distance to office, but as derived, it's essentially requiring key to lie within a certain interval around the midpoint? Could there be a DP? Probably binary search with greedy check is standard.
Given n <= 1000, k <= 2000, O(n*k log ans) is okay. But we can also improve matching using greedy interval assignment O((n+k) log (n+k)) per check. Let's implement the greedy algorithm with sorted intervals by right and using a data structure to pick the smallest available key >= left.
We can do:
- sort intervals by right_i.
- sort keys (as 2*b).
- maintain a list of unused keys (or a boolean array). For each interval, iterate over sorted keys and pick the first unused key such that key >= left_i and key <= right_i. Since intervals are processed by increasing right, we can also maintain a pointer start index to avoid scanning from beginning each time? But we cannot because keys may be used earlier. However we can keep an index pointer that moves forward as we assign keys? Actually we can maintain a pointer to the smallest key that is >= left_i among unused keys. Because keys are sorted, we could use a binary search to find the lower bound index for left_i, then iterate forward until find an unused key <= right_i. Since k is only 2000, the iteration per interval will be at most k, so O(n*k). But we could also use a "next available" array like union-find to skip used keys efficiently, known as "disjoint set for next free slot". This would give nearly O(k α(k)) per check. But given k=2000, it's overkill.
I'll implement O(n*k) per check and hope it passes.
But we must be careful: There are up to 1000 persons, 2000 keys. In worst case, for each interval we might scan all keys (2000) -> 2 million per check, times 31 = 62 million iterations. In Python, 62 million integer comparisons and loops might be okay if optimized (e.g., using plain lists, while loops, early breaks). Probably fine for Codeforces time limit (2 sec)? Many Python solutions for similar problem use binary search + matching with greedy O(nk) and pass because constraints are 1000 and 2000. I recall a problem "Roads in Berland" etc. Actually this problem is Codeforces Round #424 Div2 problem? I think it's Codeforces 830B? Wait no, it's Codeforces problem "Office Keys"? I recall a problem with n people and k keys on a line and office. The solution often involves DP after sorting, because it's also possible to solve with DP O(n(k-n))? Actually there is known solution: sort people and keys, then DP where dp[i][j] is min max time when first i people take some j keys? But because we need to minimize max time, binary search + greedy is simpler.
But there might be a direct DP: after sorting people positions and keys positions, the optimal assignment is monotonic: the people sorted left to right will take keys in increasing order. I think this holds for this cost function? Let's analyze: If we sort people positions (maybe relative to office?) The cost c(i,j) = |a_i - b_j| + |b_j - p|. If we assume the office is to the right of all? Not necessarily. But known result: For the problem of people and keys on a line with office, the optimal assignment when minimizing maximum time is to match people to keys in order after sorting people and keys (maybe with office location). I've seen a Codeforces problem "Office Keys" (CF 830B maybe?) Actually I recall Codeforces Round #424 Div2 problem B? Wait, let's search memory: There is a problem "Office Keys" (CF 830A?). No, I think it's Codeforces 830A? Actually there is "Office Keys" problem with n people, k keys, office location p. Many solutions use DP after sorting both arrays. Let's think: The cost function can be expressed as: if the office is to the right of both person and key? Not symmetric. But maybe we can transform: The time for person at a to get key at b and go to office p is max of something? Actually it's total distance traveled. If we sort people and keys, the optimal assignment (to minimize maximum) is to assign in order when considering the positions relative to office? There might be a known property: Since all people must end at p, the optimal assignment is to pair the leftmost person with the leftmost key, etc., after appropriate sorting? Let's test with example 1: people: 20 and 100 (sorted: 20,100). keys: 10,40,60,80 sorted: 10,40,60,80. If we assign in order, person 20 gets key 10 -> cost = |20-10|+|10-50| =10+40=50; person 100 gets key 40 -> cost = 60+10=70; max=70, not optimal 50. So assignment in order of sorted positions is not optimal. Perhaps if we sort by something else? Person at 20 took key 40 (which is the second key) and person 100 took key 80 (fourth key). That's not monotone in simple sorting.
But maybe we need to consider the positions relative to office. In optimal, each person picks a key that lies between them and office if possible. In the example, person 20 (left of office) took key 40 (also left of office? Actually office at 50, 40 is left of office, and 20 left of office, key is between them? 20 to 50: key 40 is between, good. Person 100 (right of office) took key 80 (right of office, between 100 and 50? 100 to 50: key 80 is between 100 and