We need to parse the problem: There are n apartments in a row, numbered 1 to n. Some are already inhabited, exactly k apartments. We don't know which ones. An apartment is "good" for Maxim if it is available for sale (i.e., not inhabited) and there is at least one already inhabited apartment adjacent to it. So basically, among the n - k uninhabited apartments, count those that are adjacent to at least one inhabited apartment. We need to find the minimum possible and maximum possible number of such good apartments, over all choices of k inhabited apartments.
Constraints: n up to 1e9, k up to n.
We need to compute min and max.
Observations:
This is similar to covering the line: each inhabited apartment can "make good" its adjacent uninhabited apartments (left and right, except boundaries). However, overlap: if two inhabited apartments are adjacent, then the apartment between them? Wait if they are adjacent, then both are inhabited, so the apartment between them doesn't exist because they are adjacent? Actually adjacent means indices differ by 1. If two inhabited apartments are next to each other, then the space between them is none (they are neighbors). So an uninhabited apartment can be adjacent to multiple inhabited apartments, but it counts as good if at least one.
Goal: minimize and maximize number of uninhabited apartments that have at least one inhabited neighbor.
Let's denote the set of inhabited apartments as I, size k.
Define good apartments = {i not in I : i-1 ∈ I or i+1 ∈ I}.
We need min|good| and max|good|.
Since n can be huge, we need formulas.
First, consider trivial cases:
-
If k = 0: No inhabited apartments, so no good apartments. min = max = 0.
-
If n = 1: then if k=0, good=0; if k=1, all apartments are inhabited so no uninhabited, good=0.
But also if k = n: all apartments inhabited, good = 0. So for k = n, min = max = 0.
Now assume 1 <= k <= n-1.
We need both extremes.
Maximum:
We want to maximize the number of uninhabited apartments that have an inhabited neighbor. Equivalent to maximize coverage of uninhabited apartments by "adjacent" influence of inhabited ones.
We have k inhabited apartments placed among n positions. Each inhabited apartment can potentially "cover" up to 2 adjacent uninhabited apartments (left and right), except at boundaries (apartment 1 has only right neighbor, apartment n has only left neighbor). However, there can be overlap: an uninhabited apartment might be adjacent to two inhabited apartments (if it lies between two inhabited apartments that are two apart? Wait: if we have inhabited at i and i+2, then apartment i+1 is uninhabited and adjacent to both i and i+2, but it only counts once. So overlapping doesn't increase count beyond 1 per uninhabited. Also, if two inhabited apartments are adjacent (i and i+1), then the space between them is not an uninhabited apartment; but they also cover each other's other side? Let's think: if inhabited at i and i+1, then i-1 (if exists) is covered by i, i+2 is covered by i+1. But the apartments i and i+1 themselves are inhabited, not good. So no issue.
So to maximize coverage, we want the inhabited apartments to be as spread out as possible? Actually, we want each inhabited apartment to cover distinct uninhabited apartments, and also cover as many as possible. Each inhabited apartment can cover at most 2 uninhabited apartments, but some of those may be already covered by another inhabited apartment, reducing distinct count. So to maximize distinct coverage, we want to minimize overlap.
But also we must consider that if inhabited apartments are too spread out, some uninhabited apartments might be isolated from any inhabited apartment, i.e., not covered. But we want to cover as many as possible, ideally all n - k uninhabited apartments. Is it possible to cover all uninhabited apartments? That would require that every uninhabited apartment is adjacent to at least one inhabited apartment. That is like a dominating set condition on the line where the dominated vertices are the uninhabited ones, but the dominating vertices are the inhabited ones, and we are dominating the complement. Actually we want a set I of size k such that every vertex not in I is adjacent to some vertex in I. In graph terms, I is a dominating set for the graph (the line graph). But note: inhabited vertices themselves need not be dominated; we only require uninhabited vertices have neighbor in I. That's essentially a dominating set condition for the complement: I is a dominating set of the whole graph? Actually in dominating set, every vertex (including those in I) must be either in I or adjacent to I. Since vertices in I are trivially dominated (by being in the set), the condition is: every vertex not in I must be adjacent to I. That's exactly the definition of a dominating set. So we need I to be a dominating set of the line graph of order n, with the condition that vertices in I are also considered dominated but they don't need adjacency; but dominating set includes I covering itself, so it's the same: a set D is dominating if every vertex not in D has a neighbor in D. So we need to choose I such that it dominates all vertices. Then all uninhabited apartments are good. That yields good = n - k (all uninhabited are good). So maximum good could be n - k if we can achieve domination with k vertices. But is it always possible to have I dominating? Not for all k; if k is too small, domination may be impossible. But we want the maximum possible number of good apartments; we may not be able to dominate all, but we can try to cover as many as possible.
But maybe we can achieve domination with certain k. For a line of n vertices, the domination number γ(G) (minimum size of a dominating set) is ceil(n/3). Actually for a path P_n, the domination number is ceil(n/3). Because you can dominate by placing a vertex every three positions (like pattern: vertex at positions 2,5,8,...). Indeed, minimum dominating set size = ceil(n/3). So if k >= ceil(n/3), then we can choose I to be a dominating set, and then all uninhabited are good, so good = n - k. That would be maximum possible? But could we have good > n - k? No, because good cannot exceed number of uninhabited apartments, which is n - k. So maximum good is at most n - k. If we can achieve all uninhabited being good, then max = n - k. If not, then max < n - k.
But wait: could there be a configuration where some uninhabited apartment is adjacent to an inhabited one, but also some uninhabited apartments are not, so good = some number less than n - k. So the maximum possible good is min( n - k, some upper bound based on k). But we need to compute exactly the maximum possible good over all choices of I.
Given k inhabited apartments, we want to maximize the number of uninhabited vertices that have a neighbor in I.
This is a combinatorial optimization problem. Since n is large, we need a formula.
Let's consider representing the line and the gaps.
We can think of the positions of inhabited apartments splitting the line into segments of consecutive uninhabited apartments (maybe with zero length if two inhabited are adjacent). But also edges at boundaries.
Define the positions of inhabited apartments sorted: i1 < i2 < ... < ik.
Then consider intervals between them, and also before the first and after the last.
Let's define:
- Left segment: apartments 1 to i1-1 (if i1 > 1). These are uninhabited and not adjacent to an inhabited apartment? Actually apartment i1-1 is adjacent to i1 if exists, so it would be good. Others further left? For example, if i1=3, then apartments 1 and 2: apartment 2 is adjacent to 3? Yes, 2 is adjacent to 3 (since 2 and 3 differ by 1), so apartment 2 would be good. Apartment 1 is adjacent to 2? But 2 is uninhabited, not inhabited. So apartment 1 is not adjacent to any inhabited apartment (since only adjacent to 2, which is uninhabited). So in the left segment, the rightmost uninhabited (i1-1) is good, but any others to its left are not (unless there is another inhabited to the left, but there is none). So in a contiguous block of uninhabited apartments at the left end, only the one adjacent to the first inhabited is good (if the block length >0). Similarly, at the right end: after last inhabited i_k, the apartments i_k+1 to n: the leftmost (i_k+1) is good, others not.
Now for interior gaps between inhabited apartments. Consider two consecutive inhabited apartments at positions a and b (a < b). The apartments between them are positions a+1, a+2, ..., b-1. These are uninhabited. Among these, which are adjacent to an inhabited apartment? The ones immediately next to a: a+1 is adjacent to a, so good. The one immediately next to b: b-1 is adjacent to b, so good. If the gap length L = b - a - 1 (number of uninhabited between them). Cases:
-
If L = 0 (a and b adjacent): then there is no uninhabited between them; so nothing.
-
If L = 1: then there is one uninhabited apartment at a+1, which is adjacent to both a and b (since a+1 is adjacent to both a and b? Actually a+1 is adjacent to a (difference 1) and also to b? If b = a+1, L=0; if b = a+2, then L=1: uninhabited at a+1, adjacent to both a and b? Check: a+1 is adjacent to a (diff=1) and also to b = a+2 (diff=1). So yes it's adjacent to both. So it is good. So that one apartment is good.
-
If L >= 2: then we have two apartments at the ends that are good: a+1 and b-1. The interior apartments (a+2 to b-2) are not adjacent to any inhabited apartment (since their only neighbors are uninhabited). So they are not good.
So in each interior gap of length L, the number of good apartments is:
- if L = 0: 0
- if L = 1: 1 (but note that it is adjacent to both ends, still counts as 1)
- if L >= 2: 2
Also, at the ends, if there is a left segment of length L_left = i1 - 1, then number of good apartments in left segment:
- if L_left = 0: 0
- if L_left >= 1: only the rightmost one (i1 - 1) is good, so count = 1
Similarly, right segment length L_right = n - i_k, good count = 1 if L_right >= 1, else 0.
Now total good = sum over segments.
So the problem reduces to: given we can choose positions of k inhabited apartments, we can control the gap lengths (including ends) to either maximize or minimize total good.
Let the gaps be: g0 (left), g1, g2, ..., gk (right), where g0 = i1 - 1, gk = n - i_k, and for j=1..k-1, gj = i_{j+1} - i_j - 1 (the number of uninhabited between consecutive inhabited). All gj are nonnegative integers, and they satisfy sum_{j=0}^{k} gj = n - k (since total uninhabited = n - k).
Total good count = f(g0) + f(gk) + sum_{j=1}^{k-1} h(gj), where:
-
For end gaps (g0, gk): f(g) = 1 if g > 0, else 0.
-
For interior gaps (gj for j=1..k-1): h(g) = 0 if g = 0; 1 if g = 1; 2 if g >= 2.
Thus total good = (g0 > 0 ? 1 : 0) + (gk > 0 ? 1 : 0) + sum_{j=1}^{k-1} c(gj), where c(g) = min(2, g) but with a twist: c(0)=0, c(1)=1, c(>=2)=2.
Note that c(g) = min(2, g) for g>=1? Actually min(2,g) yields: g=1 ->1, g>=2 ->2, g=0 ->0. So yes c(g) = min(2,g) for all g>=0 if we treat min(2,0)=0. So c(g) = min(2, g). Similarly for ends, f(g) = min(1, g) if we treat 0? Actually f(g)=1 if g>=1 else 0, which is min(1,g) but min(1,0)=0, min(1,>=1)=1, so f(g)=min(1,g). So total good = min(1, g0) + sum_{j=1}^{k-1} min(2, gj) + min(1, gk).
Now we have total uninhabited = n - k = sum gj.
We want to maximize or minimize total good by choosing nonnegative integers gj summing to n - k, with the structure: there are k-1 interior gaps and 2 end gaps (if k>=1). But note: if k = 1, there are no interior gaps; only g0 and gk (which are left and right). Actually if k=1, then there is only one inhabited apartment, so we have left gap g0 = i1 - 1, right gap g1 = n - i1. Then good = min(1, g0) + min(1, g1). So we need to handle k=1 separately.
If k=0, no gaps? Actually no inhabited, then all n are uninhabited, but good=0. But we can treat separately.
So for k >= 2, we have 2 end gaps and (k-1) interior gaps.
Now, the problem reduces to: given S = n - k, partition it into 2 + (k-1) = k+1 gaps (g0,..., gk) (all nonnegative integers). For each gap we have a contribution: end gaps contribute min(1, g), interior contribute min(2, g). We want to maximize or minimize total contribution.
Because we can choose positions arbitrarily, we can realize any distribution of gap sizes that sums to n - k? Yes, as long as we order them appropriately: left gap, then interior gaps in order, then right gap. Since any nonnegative integers summing to n - k can be assigned as gaps, and we can place inhabited apartments accordingly (by setting i1 = g0+1, then i2 = i1+1+g1, etc.). So the set of feasible gap vectors is all (g0,...,gk) with nonnegative integers summing to n - k. So the optimization is over such partitions.
Thus we need to compute:
Max_good(S, k) = max over g0+...+gk = S, gi >=0 of [min(1,g0)+min(1,gk)+ sum_{i=1}^{k-1} min(2,gi)].
Min_good(S, k) = min similarly.
We can solve these analytically.
First, for maximum:
We want to maximize contributions. Each gap can contribute at most: end gaps at most 1, interior gaps at most 2. So the theoretical upper bound is: 2 * (k-1) + 2 = 2k? Actually ends max 1 each so total max from ends = 2, interior max 2 each, total max = 2*(k-1) + 2 = 2k. But is this achievable? Only if we have enough uninhabited apartments to "activate" those contributions. To get an end gap to contribute 1, we need g0 >= 1. To get an interior gap to contribute 2, we need gj >= 2. So we need at least 1 for each end gap (if we want max ends) and at least 2 for each interior gap. So total S needed to achieve max contributions is: for ends: at least 1 each => 2; for interior: at least 2 each => 2*(k-1). So total minimal S to achieve full max = 2 + 2*(k-1) = 2k. But S = n - k. So if n - k >= 2k, i.e., n >= 3k, then we can achieve max = 2k? Wait check: max contributions if all gaps are at least the required amounts: ends >=1, interiors >=2. Then contributions: ends: 1 each (total 2), interiors: 2 each (total 2(k-1)), sum = 2 + 2k - 2 = 2k. So maximum possible good = 2k, but is that always reachable? Also note that good cannot exceed n - k (total uninhabited). So maximum good <= min(2k, n - k). But if n - k >= 2k, then 2k <= n - k? Actually if n - k >= 2k, then n >= 3k, then n - k >= 2k, so 2k <= n - k, so the upper bound 2k is less than or equal to the total uninhabited, so it's possible. However, if n - k < 2k, we cannot achieve the full 2k because we don't have enough uninhabited to make all interior gaps >=2 and ends >=1. But we might still get contributions up to S? Actually each gap's contribution is capped, but we also have total contributions limited by S because each good apartment corresponds to an actual uninhabited apartment, but note that not every uninhabited apartment is necessarily good; but the sum of contributions cannot exceed the number of uninhabited apartments? Wait each good apartment is an uninhabited apartment. So total good = number of uninhabited apartments that are good. Since there are S uninhabited apartments total, we have good <= S. Also each good apartment is counted exactly once. So good <= S. But also each gap contribution counts distinct uninhabited apartments (the ones at the edges of gaps). Indeed, the contributions come from specific uninhabited apartments: the ones adjacent to inhabited apartments. But could there be overlap? The apartments counted from different gaps are distinct because they are in different segments. For example, left end's good apartment is at position i1-1, interior gaps good apartments are at positions adjacent to inhabited on both sides; they are all distinct because they are in different intervals. So total good is sum of contributions, each contribution corresponds to a specific uninhabited apartment. So indeed, good <= S, and also good <= 2k (since each inhabited can "produce" at most 2 good apartments, but careful: an inhabited apartment can be adjacent to up to two uninhabited apartments; but if it's at the end, only one; so total maximum good is at most 2k - (number of inhabited at ends? Actually each inhabited apartment at interior has two neighbors, but those neighbors may be other inhabited apartments, in which case they are not counted. So maximum distinct good apartments that can be produced is at most 2k, but if some inhabited apartments are at ends, they have only one neighbor, so the absolute maximum is 2k? Consider k=1: maximum good is 2? Actually if one inhabited apartment, it can have up to 2 neighbors if placed not at ends. But both neighbors are uninhabited? Yes, if it's at position i (2 <= i <= n-1), then the two neighbors are uninhabited if we ensure they are not inhabited (since only one inhabited). So good = 2. But is that achievable? Only if n >= 3? For k=1, n=3, i=2 gives both neighbors uninhabited, good=2. But is 2 <= S = n-k = 2? Yes. So maximum good for k=1 is min(2, n-1)? Actually S = n-1. So maximum good = min(2, n-1) because we can only have at most 2 neighbors. So indeed max good <= 2k, but also <= S. So max good = min(2k, S)? But need to verify if it's always achievable to get min(2k, S) good apartments? Possibly, but we have to consider constraints from the structure: we have to allocate S uninhabited among gaps, and contributions per gap have caps: ends capped at 1, interior at 2. So the maximum total contributions given S is essentially the maximum sum of contributions subject to each gap's contribution <= cap, and also contribution <= gap size (since min(cap, gap)). But since we can assign any gap sizes, we can try to achieve sum of contributions = min(2k, S) by using appropriate distribution.
Let's check: if S >= 2k, we want to achieve 2k. We need to allocate gap sizes such that each end gap at least 1, each interior gap at least 2. That uses at least 2 (ends) + 2*(k-1) = 2k total uninhabited. Since S >= 2k, we can allocate exactly 1 to each end, 2 to each interior, and the remaining S - 2k uninhabited can be placed anywhere (they will be extra uninhabited that are not good, because they will be in the "interior" of gaps beyond the first two for interior? Wait if we put extra uninhabited beyond the required, contributions remain capped: for end gaps, if we add more to an end gap beyond 1, the contribution stays 1 because only the immediate neighbor is good; the extra apartments further from the inhabited are not good. For interior gaps, adding more beyond 2 keeps contribution at 2, because only the two at the edges are good. So we can indeed achieve total contribution = 2k, regardless of extra uninhabited, as long as we meet the minimum requirements for each gap. So maximum = 2k when S >= 2k. But is there any scenario where 2k > S and we can achieve S? If S < 2k, then we cannot achieve 2k because total contributions cannot exceed S (since each contribution corresponds to a distinct uninhabited apartment, and we only have S uninhabited). But can we achieve S? That would require that every uninhabited apartment is good. That is possible only if the set I dominates all vertices, i.e., every uninhabited is adjacent to an inhabited. But is that always possible when S < 2k? Actually S < 2k means n - k < 2k => n < 3k. So n < 3k. In this region, the total number of uninhabited is less than 2k. But can we arrange so that all uninhabited are good? That would require that every uninhabited apartment has an inhabited neighbor. Since each inhabited can "cover" at most 2 uninhabited (its two neighbors, but if inhabited at ends, only one). To cover all S uninhabited, we need to have enough coverage. But also overlap can cause inefficiencies. We need to see if we can always achieve good = S when S < 2k? Not necessarily. For example, n=5, k=3 => S=2. We have 3 inhabited and 2 uninhabited. Can we make both uninhabited good? Possibly yes. For instance, choose inhabited at positions 1,3,5? Then uninhabited are 2 and 4; both are adjacent to inhabited (2 adjacent to 1 and 3, 4 adjacent to 3 and 5). So good=2 = S. Another: n=4, k=3 => S=1. Inhabited at 1,2,4? Then uninhabited is 3; 3 adjacent to 2 and 4? Actually 3 adjacent to 2 and 4, both inhabited? 4 is inhabited, 2 is inhabited, so good. So good=1 = S. But what about n=4, k=2 => S=2. Can we have both uninhabited good? Choose I={1,4}: then uninhabited 2,3; 2 adjacent to 1, 3 adjacent to 4 => both good. So good=2. n=4, k=2 works. n=3, k=2 => S=1. Inhabited {1,3}, uninhabited 2 is adjacent to both => good=1. So seems maybe always possible to have all uninhabited good if k >= something? Wait, is there any configuration where S < 2k but not all uninhabited can be good? Consider extreme: n=2, k=1 => S=1. Inhabited at 1: then uninhabited 2 is adjacent to 1 => good=1 = S. Inhabited at 2: similarly. So works. n=2, k=1 S=1. n=2, k=1. So seems always possible? But think: n=3, k=1 => S=2. Can we have both uninhabited good? If inhabited at 2, then both 1 and 3 are adjacent => good=2 = S. So yes. n=3, k=2 => S=1. If inhabited at {1,2}, then uninhabited 3 is adjacent to 2 => good=1. So works. So maybe it's always possible to have all uninhabited good as long as k>0? Actually if k=0, S=n, good=0 cannot be n. So for k>=1, can we always achieve that all uninhabited apartments are good? That would mean the set of inhabited apartments is a dominating set (dominates all vertices). But domination number for a path is ceil(n/3). If k is less than domination number, we cannot dominate all vertices. For example, n=5, domination number = ceil(5/3)=2. So with k=1, can we dominate all vertices? With one inhabited, it can dominate at most itself and its neighbors, so it can dominate at most 3 vertices. For n=5, you cannot dominate all 5 with one vertex because the farthest vertices may not be adjacent. Indeed, if we put inhabited at position 3, then vertices 1 and 5 are at distance 2 from 3, not adjacent. So they are not good (they are uninhabited but not adjacent to inhabited). So with k=1, n=5, S=4, but we cannot have all 4 uninhabited good. Maximum good is? Let's compute: with one inhabited at 3, good are 2 and 4 (adjacent) => 2 good. Could we get 3? If placed at 2, good: 1 and 3 (since 3 uninhabited? Actually if inhabited at 2, then uninhabited are 1,3,4,5. 1 is adjacent to 2 (good), 3 is adjacent to 2 (good), 4 and 5: 4 adjacent to 3 (uninhabited) and 5 (uninhabited), so not good. So good=2. If at 1, good: 2 only (since only neighbor) =>1. So max good=2. So not all S=4 uninhabited are good. So the claim that all uninhabited can be good when S < 2k does not always hold; it depends on k relative to n.
Specifically, to have all uninhabited good, we need I to be a dominating set. The condition for existence of a dominating set of size k is k >= ceil(n/3). Since we can choose I arbitrarily, if k >= ceil(n/3), we can achieve domination and thus good = S = n - k. If k < ceil(n/3), then it's impossible to have all uninhabited good, and the maximum good will be less than S.
But note that our earlier analysis with caps gave maximum possible good as min(2k, S) for any k? But that's not correct because for k=1, n=5, min(2*1, S=4)=2, matches our computed max=2. For k=2, n=5, min(4, S=3)=3. Can we achieve 3? Let's test n=5, k=2, S=3. Is it possible to have 3 good? We want all 3 uninhabited good. That would require dominating set of size 2. Since domination number for n=5 is 2, yes possible. For example I={2,5}? Uninhabited: 1,3,4. Check: 1 adjacent to 2 (good), 3 adjacent to 2 (good), 4 adjacent to 5 (good) -> all good => good=3. So min(4,3)=3 works. For k=2, n=6, S=4, min(4,4)=4. Can we achieve 4? Domination number for n=6 is ceil(6/3)=2. So possible. Example: I={2,5}? Uninhabited:1,3,4,6. 1 adjacent to 2, 3 adjacent to 2,4 adjacent to 5,6 adjacent to 5 => all good? Check 4 adjacent to 5 yes; 3 adjacent to 2 yes; 1 yes; 6 yes. So all 4 good => good=4. So works.
For k=2, n=7, S=5, min(4,5)=4. So maximum would be 4, not 5. Can we achieve 4? Possibly yes. Let's test if we can get 4 good. Domination number for n=7 is ceil(7/3)=3. So we cannot have all 5 good. Max good might be 4. Check configuration: I={2,6}? Then uninhabited:1,3,4,5,7. Good: 1 adjacent to 2, 3 adjacent to 2, 5 adjacent to 6? Actually 5 is adjacent to 6 (good), 7 adjacent to 6 (good). 4 is not adjacent to any inhabited? 4 neighbors 3 and 5, both uninhabited. So good = 4. So max =4. So min(2k, S) seems to give correct maximum for these examples.
But is min(2k, S) always achievable? Let's test more edge cases.
We need to verify that for any n and k (1 <= k <= n-1), the maximum possible good is min(2k, n - k). But careful: For k=1, min(2, n-1) = 2 if n-1 >=2 i.e., n>=3; if n=2, min(2,1)=1; if n=1 (k=1, but n=1 => k=n, trivial 0). So matches earlier: n=2,k=1 -> max good=1 (achievable by placing inhabited at either end? Actually if placed at 1, good=1 (neighbor 2). If placed at 2, good=1. So yes. n=3,k=1 -> max=2, achievable by middle. n=4,k=1 -> min(2,3)=2. Can we achieve 2? Yes, place inhabited at position 2 or 3. For position 2: good are 1 and 3 =>2. So yes.
Now, what about k large relative to n? For k = n-1, S = 1. Then min(2k, S) = min(2(n-1), 1) = 1. So predicted max = 1. Is that always achievable? For n=3, k=2, S=1, max=1. Can we achieve 1? Yes, choose inhabited at 1 and 2, then uninhabited 3 is adjacent to 2 => good=1. Or 1 and 3: uninhabited 2 adjacent to both => good=1. So works. For n=2, k=1, S=1, max=1 works. For n=4, k=3, S=1, max=1, achievable: e.g., inhabited at 1,2,4, uninhabited 3 adjacent to 2 and 4 => good=1. So yes.
What about k= n? then good=0, min(2k,0)=0, ok.
Now, does min(2k, n-k) always achievable? We need to construct configurations achieving that value. For the case where n-k >= 2k, we need to achieve 2k. That's done by ensuring each inhabited apartment contributes its maximum possible (ends 1 each, interior 2 each) and that there are enough uninhabited to satisfy the minima. But we must also ensure that we can place inhabited apartments such that there are exactly k of them, and we have S >= 2k, i.e., n >= 3k. In such case, we can design gaps: left end gap = at least 1, right end gap = at least 1, each interior gap = at least 2. We can set exactly: g0 = 1, gk = 1, for j=1..k-1 set gj = 2. That uses 1+1+2*(k-1)=2k uninhabited. The remaining S - 2k uninhabited can be placed anywhere, e.g., added to some interior gap making it larger, but contributions stay same. So this yields total good = 2k. So achievable.
Now for the case where n - k < 2k, i.e., n < 3k. Then we need to achieve S = n - k good apartments (since min(2k, S) = S). This means we need all uninhabited apartments to be good. Is it always possible when S < 2k? Not automatically. As we saw with n=5, k=1 gives S=4 but 2k=2, so min=2, not S. So S < 2k? Actually 4 < 2? No, 4 > 2, so that's the other case. So S < 2k corresponds to n - k < 2k -> n < 3k. For k=1, n<3 means n <=2? Since n integer, n<3 gives n=1,2. For n=2, k=1 gives S=1, min(2,1)=1 = S. Achievable yes. For n=1, k=1 gives S=0, min=0. So for k=1, S < 2k only for n=2. So that works. For k=2, n < 6 gives n=3,4,5. Check: n=3, k=2: S=1, min(4,1)=1 = S, achievable (all uninhabited good). n=4, k=2: S=2, min(4,2)=2, achievable? Need all 2 uninhabited good. As we saw earlier, can we have both good? For n=4, k=2. Choose I={1,4}: uninhabited 2,3 both good? 2 adjacent to 1, 3 adjacent to 4 => yes. So good=2. I={1,3}: uninhabited 2,4. 2 adjacent to 1 and 3; 4 adjacent to 3? Actually 4 neighbor 3, so both good. So yes. n=5, k=2: S=3, min(4,3)=3, achievable? Need all 3 uninhabited good. Is there a dominating set of size 2 for n=5? Yes, as we gave {2,5} or {1,4}? {1,4}: uninhabited 2,3,5: 2 adjacent to 1, 3 adjacent to 4? 3 neighbor 4? Yes, 3 adjacent to 4? Actually 3 adjacent to 2 and 4, so yes adjacent to 4. 5 adjacent to 4. So all three good. So works. So for k=2 and n<6, domination number ceil(n/3) is at most 2? For n=5, ceil(5/3)=2, so yes. For n=3, ceil(3/3)=1, so 2>=1 obviously. So indeed if k >= ceil(n/3), then all uninhabited can be good. But our condition S < 2k (i.e., n<3k) does not guarantee k >= ceil(n/3). Is it possible that n<3k but k < ceil(n/3)? Let's check: For n=7, k=2 => n<6? 7<6 false, so not. For n=8, k=3 => n<9 true (8<9), so n=8, k=3, S=5. Check if k >= ceil(n/3)? ceil(8/3)=3, so k=3 meets equality. Good. For n=9, k=3 => n=9, 3k=9, so S =6, n<3k? n=9 is not less than 9, it's equal. So for n=3k exactly, S = 2k? Actually n=3k => S = n-k = 2k. So that is the boundary case where S = 2k. So for n<3k, we have S<2k. For n=3k-1, k=? For n=5,k=2 => n<6, S=3 <4, k=2, ceil(5/3)=2, so k=ceil. For n=4,k=2 => ceil(4/3)=2, k=2. n=5,k=2: works. n=7,k=3? n=7 <9, S=4, k=3, ceil(7/3)=3, okay. n=8,k=3: ceil(8/3)=3, okay. n=10,k=4: n<12, S=6, ceil(10/3)=4, okay. Is there any n<3k where k < ceil(n/3)? Let's find. ceil(n/3) > k means n/3 > k-1? Actually ceil(n/3) > k implies n/3 > k-1? Let's solve: ceil(n/3) > k => n/3 > k-1? Not exactly. Since ceil(x) > k iff x > k? Actually if x is not integer, ceil(x) > k iff x > k? For integer k, ceil(x) > k means x > k? Because if x > k, then ceil(x) >= k+1. If x = k exactly, ceil(x)=k. So condition ceil(n/3) > k is equivalent to n/3 > k? Wait if n/3 > k, then ceil(n/3) >= k+1 > k. If n/3 = k, then ceil = k, not >. So ceil(n/3) > k iff n/3 > k. Multiply by 3: n > 3k. So if n > 3k, then ceil(n/3) > k. If n <= 3k, then ceil(n/3) <= k? Let's check: For n = 3k, ceil(3k/3)=k, so equality. For n = 3k - 1, then n/3 = k - 1/3, ceil = k, so ceil = k. So for n <= 3k, ceil(n/3) <= k. Actually need to check n=3k-2: n/3 = k - 2/3, ceil = k. So indeed for n <= 3k, ceil(n/3) <= k? Consider n=3k-3 = 3(k-1), then ceil = k-1? Actually n=3(k-1), ceil(3(k-1)/3)=k-1, which is <= k. So yes, for n <= 3k, ceil(n/3) <= k. But careful: when k is small, say k=1, n<=3 gives ceil(n/3) <=1, so k=1 >= ceil. So indeed, if n <= 3k, then k >= ceil(n/3). Let's check boundary: n = 3k+1 => n > 3k, then ceil((3k+1)/3) = ceil(k + 1/3) = k+1 > k. So for n > 3k, we have k < ceil(n/3). So condition n <= 3k is equivalent to k >= ceil(n/3). Actually check: n <= 3k implies ceil(n/3) <= k? Yes because if n <= 3k, then n/3 <= k, so ceil(n/3) <= k. Conversely, if k >= ceil(n/3), then n <= 3k? Not necessarily? If k >= ceil(n/3), we have k >= ceil(n/3) >= n/3, so 3k >= n, so n <= 3k. So equivalence: k >= ceil(n/3) <=> n <= 3k. So indeed the condition for existence of a dominating set of size k is exactly that k >= ceil(n/3), which is equivalent to n <= 3k. But note: if n = 3k, then S = 2k. Domination number is k (since ceil(3k/3)=k), so it is possible to dominate with k vertices. For example, pattern: place inhabited at positions 2,5,8,...? For n=3k, a dominating set of size k can be e.g., positions 2,5,8,... up to 3k-1? That covers all. Let's test n=6,k=2: pattern 2,5 works; n=9,k=3: positions 2,5,8? Then uninhabited: 1,3,4,6,7,9. Check: 1 adjacent to 2; 3 adjacent to 2; 4 adjacent to 5; 6 adjacent to 5; 7 adjacent to 8; 9 adjacent to 8 => all uninhabited good. So indeed when n=3k, we can have all S=2k uninhabited good, which equals min(2k,S)=2k. So the maximum good equals S (since S=2k). So in the case n <= 3k, we have S >=? Actually if n <= 3k, then S = n - k <= 2k. So S <= 2k. So min(2k, S) = S. And we can achieve S (all uninhabited good) because k >= ceil(n/3) ensures domination possible. So maximum good = S when n <= 3k? Wait we need to verify the boundary: For n <= 3k, we claim we can have all uninhabited good. But is it always possible when n exactly less than 3k? For n=2, k=1 (n=2 <= 3*1=3) S=1, yes achievable. For n=4, k=2 (4 <= 6) S=2, yes. For n=5,k=2 (5 <=6) S=3, yes. For n=7,k=3 (7<=9) S=4, yes (we found config with 4 good, but we need all 4 uninhabited good). In n=7,k=3, S=4. Can we have all 4 uninhabited good? Need dominating set of size 3 for n=7. Domination number is 3 (ceil(7/3)=3). So yes possible. Example: I = {2,5,7}? Let's test: uninhabited:1,3,4,6. Check: 1 adjacent to 2, 3 adjacent to 2, 4 adjacent to 5? Actually 4 adjacent to 5? Yes, 5 is inhabited; 6 adjacent to 5 and 7? 6 is adjacent to 5 and 7; so all good. So yes.
Now what about n=8,k=3 (8 <=9) S=5, domination number ceil(8/3)=3, so possible. Example: pattern maybe 2,5,8? Let's test: I={2,5,8}, uninhabited:1,3,4,6,7. 1 adjacent to 2; 3 adjacent to 2; 4 adjacent to 5; 6 adjacent to 5? 6 adjacent to 5? Yes; 7 adjacent to 8? Actually 7 adjacent to 8, so all good. So yes.
So for n <= 3k, we can achieve all uninhabited good. For n > 3k, we cannot dominate all, and the maximum good is at most 2k. But can we always achieve 2k when n > 3k? That is when S > 2k (since n > 3k implies n - k > 2k). So we have S > 2k. In that case, min(2k, S) = 2k. So predicted max = 2k. But is 2k always achievable when n > 3k? We need to construct a configuration that yields exactly 2k good apartments, i.e., each inhabited apartment contributes its maximum possible (ends 1 each, interior 2 each) and we have at least enough uninhabited to support that. For n > 3k, we have S > 2k, so we can assign gaps to meet minima: left gap >=1, right gap >=1, interior gaps >=2. That uses exactly 2k uninhabited. The remaining S - 2k > 0 can be placed anywhere (e.g., added to an interior gap as extra uninhabited beyond the first two, which do not increase good count). However, we need to ensure that we can have ends with at least 1 and interior at least 2 while also having exactly k inhabited apartments. That's possible as long as k >= 1. But what about k=1? For k=1, there are no interior gaps. The max good is min(2, S). When n > 3k = 3, i.e., n >=4? Actually k=1, n>3 means n>=4, S = n-1 >=3 > 2. So predicted max = 2. But is 2 always achievable? For k=1, we need to get both neighbors good. That requires the inhabited apartment to be not at an end, i.e., position between 2 and n-1 inclusive. So we need n >= 3. For n >= 4, we can place inhabited at position 2 (or any interior) and get two good neighbors: positions 1 and 3 are good. But what about position 2: it's not at end? For n=4, position 2 is interior? Actually apartments 1,2,3,4. Position 2 has neighbors 1 and 3, both exist. So good=2. So yes achievable. So for k=1, max=2 for n>=3, which matches min(2, S) when S>=2. So fine.
For k >=2, n > 3k, we need to have left gap >=1 and right gap >=1. That means the first inhabited apartment must be at least 2 (so that g0>=1) and the last inhabited at most n-1 (so that gk>=1). So we need at least one uninhabited at each end. Also each interior gap must be at least 2, meaning there must be at least two uninhabited between consecutive inhabited apartments. Is it possible to satisfy all these if n is large relative to k? Yes, because total needed uninhabited for these minima is 2k, and we have S > 2k, so we can allocate. For example, set g0 = 1, gk = 1, and for each of the k-1 interior gaps set gj = 2. That uses 2k uninhabited. The remaining extra uninhabited can be added to any gap, say to g0 making it larger. So construction: place inhabited at positions: start with left gap 1: first inhabited at 2. Then interior gaps of 2: so next inhabited at 2+1+2 = 5? Actually if g0=1, then i1 = g0+1 = 2. Then for each interior gap of length 2, we add: i2 = i1 + 1 + 2 = 2+1+2=5; i3 = i2 + 1 + 2 = 5+1+2=8; ... This yields positions 2,5,8,..., up to? After k-th inhabited, we have used left gap 1, interior gaps 2 each (k-1 of them) so total uninhabited used so far = 1 + 2*(k-1) = 2k-1. Then we need right gap at least 1. After last inhabited, we set gk = 1. The position of last inhabited: i_k =? Let's compute: Starting from 1 (index 0). The pattern: i1 = 2, i2 = 5, i3 = 8, ... i_j = 2 + (j-1)*3? Because each step adds 1 (the inhabited apartment itself) plus the gap of 2 uninhabited? Actually between i_{j} and i_{j+1}, there are gj uninhabited and then i_{j+1}. If we set gj=2, then i_{j+1} = i_j + 1 + 2 = i_j + 3. So it's arithmetic progression with difference 3. So i_k = 2 + (k-1)3 = 3k -1. Then after that, we need at least 1 uninhabited at the end: position i_k + 1 <= n? Actually we need n >= i_k + 1? Since right gap gk = at least 1, we need n >= i_k + 1. So n >= (3k -1) + 1 = 3k. But if n > 3k, then n >= 3k+1, so we have at least i_k + 1 = 3k, and we can set gk = n - i_k which would be at least 1. Actually for n = 3k+1, i_k = 3k-1, then right gap = n - i_k = (3k+1) - (3k-1) = 2, which is >=1. So that works. For n = 3k exactly, i_k = 3k-1, right gap = n - i_k = 1, which is exactly 1, so it still works, but then total S = 2k, and we achieve exactly 2k good? Wait if n=3k, then S=2k, and our construction with g0=1, interior=2, right=1 yields total good = ends: 1+1=2, interior: 2(k-1) = 2k-2, total 2k. But we also have S=2k, and we have used all uninhabited exactly as per the gaps: left 1, interior each 2, right 1. So good = 2k = S. So that also works and yields all uninhabited good (since each uninhabited is either at ends? Let's verify: In this construction, the uninhabited positions are: 1 (left), then between inhabited: for each interior gap of length 2, positions i_j+1 and i_j+2 (two uninhabited). Are both good? Yes: the first is adjacent to i_j, the second adjacent to i_{j+1}. So both are good. And the rightmost uninhabited is at position n (if right gap=1) adjacent to i_k. So all uninhabited are good. So for n=3k, we also achieve S = 2k good, which equals min(2k, S) = S = 2k. So actually for n = 3k, it's both the case of S=2k and we can achieve all uninhabited good, which is consistent.
Now for n > 3k, we set g0=1, interior=2, and right gap = n - i_k. Since n > 3k, right gap > 1? Actually if n = 3k+1, i_k = 3k-1, right gap = 2; if larger, right gap bigger. But still the good count from right gap is min(1, gk) = 1 (since only the first uninhabited adjacent to i_k counts). So total good = 2k. So achievable.
Thus, for all k>=1, maximum good = min(2k, n - k). But we should also consider k=0: max=0. So formula holds: max = min(2