Mr. Bender has a digital table of size n × n, each cell can be switched on or off. He wants the field to have at least c switched on squares. When this condition is fulfilled, Mr Bender will be happy.
We'll consider the table rows numbered from top to bottom from 1 to n, and the columns — numbered from left to right from 1 to n. Initially there is exactly one switched on cell with coordinates (x, y) (x is the row number, y is the column number), and all other cells are switched off. Then each second we switch on the cells that are off but have the side-adjacent cells that are on.
For a cell with coordinates (x, y) the side-adjacent cells are cells with coordinates (x - 1, y), (x + 1, y), (x, y - 1), (x, y + 1).
In how many seconds will Mr. Bender get happy?
Input
The first line contains four space-separated integers n, x, y, c (1 ≤ n, c ≤ 109; 1 ≤ x, y ≤ n; c ≤ n2).
Output
In a single line print a single integer — the answer to the problem.
Examples
Input
6 4 3 1
Output
0
Input
9 3 8 10
Output
2
Note
Initially the first test has one painted cell, so the answer is 0. In the second test all events will go as is shown on the figure. .
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We need to compute the minimal time t (in seconds) such that the number of switched-on cells after t seconds (starting from a single cell at (x,y) on an n x n grid, with propagation to side-adjacent cells each second) is at least c. c ≤ n^2.
We can think of the process: at time 0, only (x,y) is on. At each second, any cell that is off but has at least one on neighbor (side-adjacent) becomes on. This is essentially the BFS flood fill from the starting cell, where each cell's Manhattan distance from (x,y) determines when it becomes on: it becomes on at time equal to its Manhattan distance from (x,y). Because the propagation moves outward one Manhattan step per second. However, there is a boundary: the grid is finite (n x n). So cells beyond the boundary are absent. So the number of cells switched on by time t is the number of cells within Manhattan distance ≤ t from (x,y), but clipped to the grid boundaries.
Thus we need to compute the number of grid points (i,j) with 1 ≤ i ≤ n, 1 ≤ j ≤ n, and |i - x| + |j - y| ≤ t. Then find smallest t such that this count >= c.
We have n up to 1e9, so t could be large up to about 2n? Actually Manhattan distance max in grid is (x-1)+(y-1) to far corner, or (n-x)+(n-y). So maximum distance is up to 2(n-1) but less than 2n. So t could be up to 2e9. But we can binary search t, and compute count function efficiently using formulas.
We need to compute, for given t, the number of integer lattice points (i,j) within the diamond (Manhattan ball) intersected with rectangle [1,n] × [1,n].
Classic problem: count lattice points in diamond limited by borders.
Let’s define coordinates relative to starting point: let dx = i - x, dy = j - y. Condition: |dx|+|dy| ≤ t. And i in [1,n] => dx in [1-x, n-x]; similarly dy in [1-y, n-y].
We can compute total points in the infinite diamond (unbounded) as 1 + 2t(t+1)? Wait, number of integer points with |dx|+|dy| ≤ t is: For each k from 0 to t, number of points with L1 norm exactly k is 4k (for k>0) except careful: Actually points with |dx|+|dy| = k: if k=0, 1 point. For k>=1, the number is 4k. Because dx from -k to k, dy = ±(k - |dx|) with both signs, total: for each |dx| from 0 to k, there are contributions. But typical formula: Number of integer points in diamond radius t inclusive is 1 + 2t(t+1). Let's check: For t=0, count=1. For t=1: points: (0,0), (±1,0),(0,±1) => total 5. 1+212=5 correct. For t=2: points with norm 2: count 42=8? Actually points with exactly 2: (0,±2), (±1,±1)?? Let's enumerate: (0,2),(0,-2),(2,0),(-2,0), (1,1),(1,-1),(-1,1),(-1,-1) => 8. So total = 1 (norm0) +4 (norm1) +8 =13. 1+223=13 correct. So indeed total = 2t^2 + 2t +1 = 2t*(t+1)+1.
But we must clip to rectangle boundaries. So we need to subtract points that fall outside the grid.
Compute number of points with |dx|+|dy| ≤ t such that i = x+dx not in [1,n] or j = y+dy not in [1,n]. Actually we need intersection count. A typical approach is to compute the area of the diamond, then subtract the parts that exceed each border. Since n up to 1e9, we can compute contributions by considering the distance from starting point to each border: up = x-1 (number of rows above), down = n-x (rows below), left = y-1, right = n-y.
We need to compute count of points in the diamond that lie within the grid. This is equivalent to total points in diamond minus points that violate i ≤ 0? But easier: The count can be computed by summing over possible dx values? That might be O(t) but t could be large. Need O(1) formula.
We can think of the diamond clipped by rectangle: For each row i (or each dx), the allowed j range in the diamond is [y - (t - |dx|), y + (t - |dx|)] intersect with [1,n]. Then sum over dx from -t to t, but only those dx such that x+dx in [1,n]. So we can sum over dx in [max(-t, 1-x), min(t, n-x)], and for each dx compute the length of dy interval: from -(t - |dx|) to (t - |dx|), then intersect with [1-y, n-y] => dy_min_allowed = max( - (t - |dx|), 1-y )? Actually dy must satisfy y+dy in [1,n] => dy ∈ [1-y, n-y]. Also |dy| ≤ t - |dx|. So dy ∈ [-(t - |dx|), t - |dx|] ∩ [1-y, n-y]. Number of integer dy = max(0, min(t - |dx|, n-y) - max(-(t - |dx|), 1-y) + 1). Summing that over dx.
But t may be up to 2e9, and we need O(1) per query (binary search) for large n. So we need a closed form.
We can compute the clipped count by starting with total diamond points, then subtract points that fall outside each of the four boundaries (but careful with overlapping subtraction). However, because the shape is convex and the rectangle is axis-aligned, we might compute the count as the diamond area minus the portions that extend beyond each side, then add back intersections of two sides (like beyond top and left) because they were subtracted twice. This is like inclusion-exclusion for the diamond clipped by the half-planes: i >=1, i <= n, j >=1, j <= n.
So define four forbidden regions:
i < 1 (i.e., dx < 1-x)
i > n (dx > n-x)
j < 1 (dy < 1-y)
j > n (dy > n-y)
We want count of points in diamond that satisfy all constraints.
Let top_excess = number of points in diamond with i < 1, i.e., dx <= - (x)?? Actually i = x+dx < 1 => dx < 1-x. Since dx integer, dx ≤ 1-x -1? Actually condition i ≤ 0? Wait i < 1 means i <= 0? But i is integer row index starting from 1. So i must be >=1, so i < 1 is impossible; we need i <= 0. Since i integer, i ≤ 0 is equivalent to i ≤ 0. So i < 1 means i <= 0. So condition: x+dx <= 0 => dx <= -x. Since x >=1, -x <= -1. But we also have dx integer. So top excess: dx ≤ -x.
But careful: The top border is row 1. So the valid rows are i >=1. So the forbidden region is i <= 0. So we need points in diamond with i <= 0 (i.e., dx <= -x). Similarly bottom excess: i >= n+1? i > n => i >= n+1 => dx >= n+1 - x => dx >= n-x+1. Left excess: j <= 0 => dy <= -y. Right excess: j >= n+1 => dy >= n-y+1.
So we need to count points in diamond satisfying these inequalities. However, the diamond itself is defined by |dx|+|dy| ≤ t. So for each side we count the number of integer points in diamond that also satisfy the forbidden condition. Then we subtract these counts. But the forbidden sets may overlap (points that are outside both top and left, etc.), so we need to add back the intersections of two forbidden sets (like top and left), then subtract triple intersections, etc., using inclusion-exclusion. Since we have four constraints, but the diamond is symmetric and the intersections might be manageable.
But note that the forbidden conditions are all about dx and dy being beyond certain thresholds. So we can compute counts using geometry: For each side, the region is the part of the diamond that extends beyond a line. For top: dx ≤ -x. That is a half-plane. Intersection of diamond with that half-plane is essentially a truncated diamond. This region is itself a diamond shape truncated at a certain line, and we can count its integer points using formula for number of points with |dx|+|dy| ≤ t and dx ≤ -x (or dx ≥ something). Similarly for other sides.
Since x,y,n can be large, but t is the variable, we need a formula that for given t, we can compute these counts quickly.
Alternative approach: Use the fact that the diamond clipped by rectangle is essentially the diamond minus four "caps" beyond each border. Those caps are like triangles (or parts of diamond) that can be computed.
Another idea: The number of points in rectangle can be computed by summing over rows. For each row i, the number of columns j such that |i-x|+|j-y| ≤ t, and 1 ≤ j ≤ n. The row i runs from max(1, x-t) to min(n, x+t). So the number of rows involved is at most 2t+1, but t could be huge (up to 2e9) but we cannot iterate. However, we can compute the total sum using a formula because for each row i, the allowed j range is an interval: from L(i) = max(1, y - (t - |i-x|)) to R(i) = min(n, y + (t - |i-x|)), and count = max(0, R(i) - L(i) + 1). Sum over i.
We can split into three parts: rows where the diamond interval is not clipped horizontally (i.e., the full diamond interval lies inside [1,n]), and rows near the top/bottom where the interval is clipped on the sides due to the grid edges. However, it's messy because both vertical and horizontal clipping may happen simultaneously.
But there is known formula: The number of points in Manhattan ball of radius t intersected with rectangle can be computed using the inclusion-exclusion principle by considering four quarter-planes. This is similar to problem of "diamond in grid" often solved by computing sum of arithmetic series for each border exceeding. There is a known Codeforces problem "B. Little Pony and Sort by Shift"? Not sure. Actually there is a problem "B. Mr. Kitayuta's Colorful Graph"? Not that. But I recall problem "B. The Meeting Place Cannot Be Changed"? No.
Anyway, we can derive formulas.
Let’s define distances to borders:
d_up = x - 1 (max steps upward before hitting border)
d_down = n - x
d_left = y - 1
d_right = n - y
If t is small such that the diamond is completely inside the grid (i.e., t ≤ min(d_up, d_down, d_left, d_right)), then the count is simply the full diamond size: 2t^2 + 2t + 1. Since t increases, eventually the diamond will hit the borders.
So for given t, we can compute the excess beyond each border: For the top border, the part of diamond that goes beyond that border corresponds to distances beyond d_up. More precisely, for points with dx ≤ - (d_up+1)?? Because the maximum upward without exceeding is d_up: dx can be as low as -d_up (since row index x+dx >= 1 => dx >= - (x-1) = -d_up). So if we have dx = -d_up - k where k >=1, then i = x - d_up - k <= 0? Actually i = 1 - k <= 0, so outside. So the top forbidden region is dx ≤ -d_up - 1. Wait careful: d_up = x-1. The smallest allowed dx is -d_up (i.e., i=1). So forbidden are dx ≤ -d_up - 1. But earlier we said dx ≤ -x, because i <=0 implies x+dx <=0 => dx <= -x. Since -x = -(d_up+1) = -d_up -1. So yes, forbidden condition is dx ≤ -d_up - 1. Good.
Similarly bottom: dx ≥ d_down + 1 (since d_down = n-x, allowed max dx = d_down, so forbidden dx ≥ d_down+1).
Left: dy ≤ -d_left - 1? Actually d_left = y-1, allowed min dy = -d_left, so forbidden dy ≤ -d_left - 1, which is dy ≤ -y.
Right: dy ≥ d_right + 1.
So we can define excess distances:
exceed_up = max(0, t - d_up)? Actually how many layers of points become forbidden? Let's think: The diamond includes points with |dx|+|dy| ≤ t. For the top, we care about points where dx is too negative. For a given dx = -d_up - k (k ≥ 1), the condition for existence of such point also requires that there is some dy such that |dx|+|dy| ≤ t. That is, we need that the distance from the start to the top border and beyond is covered.
We can count the number of points in the top forbidden region (i.e., with dx ≤ -d_up-1) and |dx|+|dy| ≤ t. This region is like a triangle of a diamond cut by a vertical line? Actually it's bounded by dx constraint and the diamond.
Let’s set u = d_up. The top forbidden region is: dx ≤ -u-1 and |dx|+|dy| ≤ t. Since dx is negative, |dx| = -dx. So condition: -dx + |dy| ≤ t, and dx ≤ -u-1. We can define a = -dx, so a ≥ u+1, integer. Then condition: a + |dy| ≤ t, with a ≥ u+1. So essentially points with L1 radius a + |dy| ≤ t, but a is at least u+1. For a fixed a, the number of integer dy with |dy| ≤ t - a is 2*(t - a) + 1? Wait: For a given a (positive), the condition a + |dy| ≤ t => |dy| ≤ t - a. So the number of dy values is: if t - a >= 0, then 2*(t - a) + 1. But also dy must satisfy the left/right constraints? No, for now we are just counting top forbidden points irrespective of other boundaries? But for the inclusion-exclusion we need top forbidden points in the full infinite diamond (without considering other borders). However, these top forbidden points may also be outside left/right etc. But for the subtraction of top excess, we subtract all points that violate the top condition, regardless of whether they also violate left condition. Then later we add back intersections to correct. That's inclusion-exclusion.
Thus, for counting top excess (points with i<1, i.e., dx ≤ -u-1) we count number of integer solutions to:
dx ≤ -u-1,
|dx| + |dy| ≤ t.
Let a = -dx, so a ≥ u+1, integer. Then condition: a + |dy| ≤ t. For each a from u+1 to t (since a ≤ t because a + |dy| ≤ t and |dy| ≥0 => a ≤ t), the number of dy is: for given a, number of integer dy such that |dy| ≤ t - a is: if t - a >= 0, then 2*(t - a) + 1. So top_excess_count = sum_{a = u+1}^{t} (2*(t - a) + 1). When t <= u, there are no a ≥ u+1 <= t, so top_excess = 0.
Similarly, bottom_excess_count: points with dx ≥ d+1 where d = d_down. Let b = dx, b ≥ d+1, condition: |dx| = b (since b positive) + |dy| ≤ t. So b from d+1 to t, number dy = 2*(t - b) + 1. So bottom = sum_{b = d+1}^{t} (2*(t - b) + 1).
Left_excess: points with dy ≤ -l-1, where l = d_left. Let c = -dy, c ≥ l+1, condition: |dx| + c ≤ t. So for each c from l+1 to t, number of dx such that |dx| ≤ t - c is: 2*(t - c) + 1. So left = sum_{c = l+1}^{t} (2*(t - c) + 1).
Right_excess: points with dy ≥ r+1, where r = d_right. Let e = dy, e ≥ r+1, condition: |dx| + e ≤ t. So sum_{e = r+1}^{t} (2*(t - e) + 1).
All these formulas are similar.
Now, we also need pairwise intersections: top & left, top & right, bottom & left, bottom & right, top & bottom? But top & bottom can't both happen unless t is huge and the diamond extends beyond both? Actually a point cannot simultaneously satisfy dx ≤ -u-1 and dx ≥ d+1 because that would require both very negative and very positive, impossible (unless no point). So top & bottom intersection is empty. Similarly left & right empty. But top & left, top & right, bottom & left, bottom & right are possible: points that are outside both top and left borders, i.e., i<1 and j<1; i<1 and j>n; i>n and j<1; i>n and j>n. These are corner regions.
Also triple intersections: top, left, right? That would require i<1 and j<1 and j>n simultaneously impossible. So triple intersections are empty. Similarly any three can't all be true. So only pairwise intersections among these four sets that are non-empty are the four corner combos.
Now, we also might have intersections like top and left? Already considered. Also top and right? Yes. But what about top and bottom? No. left and right? No. So inclusion-exclusion with four sets: Total valid = A(t) - (top+bottom+left+right) + (top_left + top_right + bottom_left + bottom_right). There is no need for triple or quadruple because they are empty.
Now we need to compute top_left: points satisfying dx ≤ -u-1 and dy ≤ -l-1, and |dx|+|dy| ≤ t.
Define a = -dx ≥ u+1, c = -dy ≥ l+1. Condition: a + c ≤ t. So we need to count number of integer pairs (a,c) with a ≥ u+1, c ≥ l+1, a+c ≤ t. Count = number of solutions to a+c ≤ t, a ≥ u+1, c ≥ l+1.
Similarly top_right: dx ≤ -u-1 and dy ≥ r+1. Let a = -dx ≥ u+1, e = dy ≥ r+1. Condition: a + e ≤ t.
Bottom_left: dx ≥ d+1, dy ≤ -l-1. Let b = dx ≥ d+1, c = -dy ≥ l+1. Condition: b + c ≤ t.
Bottom_right: dx ≥ d+1, dy ≥ r+1. Condition: b + e ≤ t.
All are counts of integer pairs with lower bounds and sum constraint.
Thus we can compute each as sum over a from lower bound to t - lower bound of other.
Specifically, top_left = number of (a,c) integers, a ≥ u+1, c ≥ l+1, a+c ≤ t.
Let A_min = u+1, C_min = l+1. For a given a, c can be from C_min to t - a, provided t - a ≥ C_min. So a range from A_min to t - C_min (since a ≤ t - C_min). So top_left = sum_{a = A_min}^{t - C_min} ( (t - a) - C_min + 1 ) = sum_{a = A_min}^{T'} (t - a - C_min + 1), where T' = t - C_min. Compute.
Similarly, for top_right: a ≥ u+1, e ≥ r+1, a+e ≤ t. So a from u+1 to t - (r+1), e from r+1 to t - a. Count = sum_{a = u+1}^{t - (r+1)} (t - a - r - 1 + 1?) Let's define: e_min = r+1. For a given a, e_max = t - a, so number of e = max(0, (t - a) - (r+1) + 1) = t - a - r. So count = sum_{a = u+1}^{t - r - 1} (t - a - r). Provided t - r - 1 >= u+1.
Similarly bottom_left: b ≥ d+1, c ≥ l+1, b + c ≤ t. Count = sum_{b = d+1}^{t - l - 1} (t - b - l).
Bottom_right: b ≥ d+1, e ≥ r+1, b+e ≤ t. Count = sum_{b = d+1}^{t - r - 1} (t - b - r).
All these sums can be expressed in closed form using arithmetic series.
But careful: The inclusion-exclusion formula we derived counts points that are outside the rectangle (violate at least one constraint). However, we want points inside the rectangle. So valid = total - (top+bottom+left+right) + (top_left+top_right+bottom_left+bottom_right).
Now we must also consider that the top, bottom, left, right excess counts as computed might include points that are also outside other borders, but that's intentional for inclusion-exclusion. So we need to ensure our counts are correct for each set independently, considering only the condition for that set and the diamond, without regard to other constraints.
But is there any overlap between top and left? Yes, which we add back. And top and right, etc. This seems correct.
But we must be careful about the condition for a point to be in the diamond: |dx|+|dy| ≤ t. In computing top_excess, we counted points with dx ≤ -u-1 and |dx|+|dy| ≤ t. However, these points might also have dy that puts them outside left or right (i.e., dy ≤ -l-1 or dy ≥ r+1). That's fine; they are included in top_excess, and later we subtract the overlaps. So fine.
Now, does this inclusion-exclusion fully account for all points within the rectangle? Since the rectangle constraints are i between 1 and n inclusive, j between 1 and n inclusive. Our sets are defined as i ≤ 0 (top), i ≥ n+1 (bottom), j ≤ 0 (left), j ≥ n+1 (right). And all integer points in diamond are either inside the rectangle or in at least one of these sets (if they lie outside). There is no other possibility. So inclusion-exclusion works.
One more nuance: Are there points with i = 0? Yes, i can be 0 (if x+dx=0). That's outside because rows start at 1. So it's included in top set (i≤0). Similarly j=0 in left. Good.
Now the formulas for top_excess, etc., must account for integer ranges correctly.
Let's derive closed forms.
Define:
up = x - 1
down = n - x
left = y - 1
right = n - y
Let total(t) = 2t^2 + 2t + 1, for t ≥ 0.
If t < 0? Not considered.
Now define function f(t, d) for counting points beyond a certain distance d in one direction (like top). For a direction, the excess count for a border where the allowed distance is d (max steps without leaving the grid) is:
excess_one_dir(t, d) = number of points with excess in that direction, i.e., points that go beyond the border by at least 1 step. This equals sum_{k = d+1}^{t} (2*(t - k) + 1) = sum_{m = 0}^{t - (d+1)} (2m + 1) with m = t - k. Let L = max(0, t - d). Actually careful: if t ≤ d, then no excess: excess = 0. If t > d, then we sum for k from d+1 to t. Number of terms: t - (d+1) + 1 = t - d. So excess = sum_{i=0}^{t-d-1} (2i + 1) ??? Let's do transformation: Let i = t - k, then when k = d+1, i = t - d - 1; when k = t, i = 0. So i runs from 0 to t-d-1 (inclusive). Then term = 2i + 1. So sum_{i=0}^{t-d-1} (2i + 1) = (t-d)^2? Because sum of first (M) odd numbers = M^2, where M = number of terms. Here number of terms = t - d. Indeed sum_{i=0}^{M-1} (2i+1) = M^2. Check: M = t-d. So excess_one_dir(t,d) = (t - d)^2, provided t > d. If t ≤ d, excess = 0.
Let's verify with an example. Suppose up = d_up = 2, t=3. The top forbidden points: dx ≤ -3? Actually up=2 => d_up=2, allowed dx >= -2. Forbidden dx <= -3. Now diamond radius t=3. Points with dx = -3: then |dx|=3, so need |dy| ≤ 0 => dy=0 only => 1 point. dx = -4? Not possible because |dx| = 4 > 3. So only 1 point. Our formula: t-d = 3-2=1, (1)^2=1. Correct.
Another: up=0 (x=1). d_up=0. t=2. Forbidden dx ≤ -1? Actually up=0 means no rows above, so any dx negative is forbidden. For t=2, points with dx = -1: |dx|=1, |dy| ≤ 1 => dy = -1,0,1 => 3 points. dx = -2: |dx|=2, |dy| ≤ 0 => 1 point. Total = 4. Formula: t-d = 2-0 = 2, square = 4. Correct.
So indeed excess for one side = max(0, t - d)^2.
Wait: For bottom and right it should be same: excess = (t - d)^2, where d is the distance to that border: for bottom d = down, for left d = left, for right d = right.
Check left: left distance = y-1 = d_left. If t > d_left, then points with dy ≤ -d_left -1. For each excess step k = d_left+1 to t, number of dx points = 2*(t - k) + 1. Sum = (t - d_left)^2. So yes.
Thus:
top_excess = max(0, t - up)^2
bottom_excess = max(0, t - down)^2
left_excess = max(0, t - left)^2
right_excess = max(0, t - right)^2
Now the corner (two-side) intersections.
Consider top-left: points with dx ≤ -up-1 and dy ≤ -left-1. Let a = -dx, c = -dy. Conditions: a ≥ up+1, c ≥ left+1, a + c ≤ t.
We need count of integer pairs (a,c) with a >= A0 = up+1, c >= B0 = left+1, and a + c <= t.
If t < (up+1)+(left+1) = up+left+2, then no such points. So let M = t - (up+left+2). If M < 0, count = 0. Otherwise, how many pairs? This is like counting number of integer points in a right triangle.
We can compute: For a from A0 to t - B0, the number of c is from B0 to t - a, so count = (t - a - B0 + 1). So total = sum_{a = A0}^{t - B0} (t - a - B0 + 1). Let’s change variable: let a' = a - A0. Then a = A0 + a', and a' goes from 0 to (t - B0) - A0 = t - (A0 + B0) = t - (up+left+2). So let S = t - (up+left+2). If S < 0, count=0. Otherwise a' = 0..S. For each a', the count = t - (A0 + a') - B0 + 1 = t - (up+1 + a') - (left+1) + 1 = t - up - left - 1 - a'? Let's compute: t - (up+1) - (left+1) + 1 = t - up - left -1. Then subtract a'? Actually: t - (A0 + a') - B0 + 1 = t - A0 - a' - B0 + 1 = (t - A0 - B0 + 1) - a'. And t - A0 - B0 + 1 = t - (up+1) - (left+1) + 1 = t - up - left -1. So count = (t - up - left -1) - a'. So total = sum_{a'=0}^{S} [ (t - up - left -1) - a' ].
Now note that S = t - (up+left+2). So (t - up - left -1) = S + 1? Because S = t - up - left - 2, so S+1 = t - up - left -1. Yes. So count = sum_{a'=0}^{S} (S+1 - a') = sum_{k=1}^{S+1} k = (S+1)(S+2)/2? Wait: when a' runs 0 to S, the terms are (S+1), S, ..., 1. So sum = (S+1)(S+2)/2? Let's compute: sum from 1 to N is N(N+1)/2, where N = S+1. So sum = (S+1)(S+2)/2. Actually check: N = S+1, sum 1..N = N(N+1)/2 = (S+1)(S+2)/2. Yes.
But is that correct? Example: up=0, left=0 (x=1,y=1). t=2. Then top-left region: dx ≤ -1, dy ≤ -1, and |dx|+|dy| ≤2. Points: (-1,-1) only? Let's list: a=1,c=1 => a+c=2 ≤2 => one point. Also (-2,0)? but dy=0 not ≤ -1, so no. So count=1. Our formula: up=0, left=0, t=2 => S = t - (up+left+2)=2-2=0. Then count = (S+1)(S+2)/2 = (1*2)/2=1. Good.
Another: up=0, left=0, t=3. Points: a=1,c=1 =>2; a=1,c=2? c=2≥1, a+c=3 ≤3 => yes, ( -1, -2 )? but dy=-2 is ≤ -1, okay. Also a=2,c=1 => ( -2, -1 ). So total? (1,1):1; (1,2):1; (2,1):1 => total 3. Also (1,3?) a=1,c=3 => sum=4>3 no; (2,2) sum=4>3; (3,1) sum=4>3. So 3 points. Formula: S = t - 2 = 1, count = (2*3)/2 = 3. Good.
Another: up=1, left=0, t=3. up=1 means x-1=1, so up=1. left=0. t=3. Then top-left: dx ≤ -2? because up+1=2, so dx ≤ -2; dy ≤ -1? left+1=1, so dy ≤ -1. Points: a=2,c=1 => sum=3 => one; a=2,c=2 => sum=4>3 no; a=3,c=1 => sum=4>3 no; a=2,c= maybe? Also a=3,c=0 not allowed c>=1. So count=1. Formula: S = t - (up+left+2) = 3 - (1+0+2)=0, count = (0+1)(0+2)/2 = 12/2=1. Good.
Thus top_left_count = if t >= up+left+2 then ( (t - up - left - 1)?? Actually using S = t - (up+left+2). But note that S can be negative. So define:
let exceed = t - (up+left+2). If exceed >= 0, then count = (exceed+1)(exceed+2)/2. Alternatively, count = (t - up - left - 1)?? Let's express directly: Let M = t - up - left - 2; if M < 0, count=0; else count = (M+1)(M+2)/2.
Similarly for other corners:
top_right: conditions: dx ≤ -up-1 (a = -dx ≥ up+1), dy ≥ right+1 (e = dy ≥ right+1), and a + e ≤ t.
Let A0 = up+1, E0 = right+1. Then sum condition a+ e ≤ t. This is analogous: For a from A0 to t - E0, count of e = (t - a) - E0 + 1 = t - a - E0 + 1? Actually e min = E0, e max = t - a, so number = (t - a) - E0 + 1 = t - a - E0 + 1. But careful: if t - a < E0, then 0. So sum over a where a ≤ t - E0. So total = sum_{a = A0}^{t - E0} (t - a - E0 + 1). Let's define variables: Let S_tr = t - (A0 + E0) = t - (up+1 + right+1) = t - up - right - 2. If S_tr >= 0, then similar sum: count = (S_tr +1)*(S_tr+2)/2? Let's derive: set a' = a - A0, a' from 0 to t - E0 - A0 = t - (up+right+2) = S_tr. For each a', count = t - (A0 + a') - E0 + 1 = (t - A0 - E0 + 1) - a' = (t - up - right -1) - a'. And t - up - right -1 = S_tr + 1? Since S_tr = t - up - right -2, so S_tr+1 = t - up - right -1. So count = sum_{a'=0}^{S_tr} (S_tr+1 - a') = (S_tr+1)(S_tr+2)/2. So yes, same formula.
Similarly bottom_left: count = if t >= down+left+2 then ( (t - down - left - 1)? Actually same: let S_bl = t - (down+left+2) >=0 => count = (S_bl+1)*(S_bl+2)/2.
Thus all four corner counts are of the same form: For given two distances d1, d2 (the max allowed steps in those directions), the count of points beyond both borders by at least 1, i.e., exceeding both d1 and d2, is: if t > d1 + d2 + 1? Actually condition for existence is t >= d1 + d2 + 2? Because need at least (d1+1)+(d2+1) to have a point outside both. But the derived condition is t >= d1+d2+2. Then count = C(t - d1 - d2 - 1)??? Let's express cleanly:
But is there any overcount? We should also consider possible triple intersections? As argued, impossible because you cannot be both above top and below bottom simultaneously, etc. But could a point be outside top, left, and right? That would require j<1 and j>n simultaneously, impossible. So indeed only pairwise among orthogonal pairs (top-left, top-right, bottom-left, bottom-right). Also top-bottom impossible, left-right impossible. So inclusion-exclusion stops at pairwise.
Thus V(t) = total(t) - ( (t-up)^2_+ + (t-down)^2_+ + (t-left)^2_+ + (t-right)^2_+ ) + ( corner(up,left) + corner(up,right) + corner(down,left) + corner(down,right) ), where (z)_+ = max(z,0) and corner(d1,d2) defined as above.
But wait: Are we missing something? Let's test with a small example.
Example: n=3, start at (2,2), so up=1, down=1, left=1, right=1. Let's compute V(t) for t=0,1,2,... manually.
t=1: Manhattan distance 1: (2,1),(2,3),(1,2),(3,2) => 4, total 5. All within grid. So V=5.
t=2: Manhattan distance ≤2. All cells? The grid has 9 cells. Maximum distance from center: to (1,1) distance 2, (1,3) 2, (3,1) 2, (3,3) 2. So all 9 cells. V=9.
t=3: Should still be 9 (since grid finite). Our formula: total(t) = 2t^2+2t+1. For t=2: 2*4+4+1=8+4+1=13. But V=9. So we need to subtract excess.
Compute with formula:
up=1, down=1, left=1, right=1.
For t=2:
excess_up = max(0, t-up) = 2-1=1, squared=1.
excess_down=1^2=1.
excess_left=1^2=1.
excess_right=1^2=1.
Sum excess =4.
Corners:
up-left: d1=1,d2=1, t=2 => exceed = t - (1+1+2)=2-4=-2<0 =>0.
up-right: 0.
down-left:0.
down-right:0.
So V = total - sum_excess = 13 - 4 = 9. Good.
For t=3:
total = 2*9+6+1=18+6+1=25.
excess_up = max(0,3-1)=2, squared=4.
excess_down=4.
excess_left=4.
excess_right=4. sum=16.
Corners:
up-left: exceed = t - (1+1+2)=3-4=-1 ->0.
But wait, at t=3, the diamond extends beyond corners. For up-left corner, there will be points outside both top and left. Let's manually see: Points with dx ≤ -2 (since up=1, forbidden dx≤-2) and dy ≤ -2 (left=1, forbidden dy≤-2). At t=3, such points: (-2,-2): |dx|+|dy|=4>3 not allowed; (-2,-1): sum=3? |dx|=2,|dy|=1 sum=3, but dy=-1 is not ≤ -2? Actually left forbidden dy≤-2 because left=1, so dy≤-2. So dy=-1 is allowed (since j= y+(-1)=2-1=1? Actually y=2, so dy=-1 => j=1, which is inside, not forbidden). So left forbidden only if dy ≤ -2 => j<=0. So for up-left, need dy ≤ -2, dx ≤ -2. Points: (-2,-2) sum=4>3 no; (-3,-? ) sum >? No. Actually t=3, a = -dx >=2, c = -dy >=2, condition a+c ≤3. Minimum a+c=4 >3, so no points. So corner counts 0. So sum corners=0.
Thus V=25-16=9. Good.
For t=4: total=216+8+1=32+8+1=41. excess_up = (4-1)=3^2=9, each 9, sum=36. Corners: up-left: exceed = t - (1+1+2)=4-4=0 -> S=0, count=(12)/2=1. Similarly up-right: (up,right) both 1, same count=1. down-left:1, down-right:1. Sum corners=4. So V=41-36+4=9. So remains 9. So formula works.
Now test a non-symmetric case: Example from problem: n=9, x=3, y=8, c=10. Let's compute manually perhaps to confirm answer 2.
First compute distances: up = x-1 = 2, down = n-x = 9-3=6, left = y-1 = 7? Actually y=8 => left=7, right = n-y = 9-8=1.
So up=2, down=6, left=7, right=1.
We need minimal t such that V(t) >= 10.
We'll compute V(t) for t=0,1,2.
t=0: V=1.
t=1: total=2*1+2+1=5. Excesses: up: max(0,1-2)=0; down: max(0,1-6)=0; left: max(0,1-7)=0; right: max(0,1-1)=0; Corners all 0. So V=5.
t=2: total=2*4+4+1=8+4+1=13. Excess: up: max(0,2-2)=0^2? Actually 2-2=0 -> 0? Since t-d=0, square=0. So up excess=0. down: max(0,2-6)=0 =>0. left: max(0,2-7)=0 =>0. right: max(0,2-1)=1 =>1^2=1. So sum_excess =1.
up-right: up=2, right=1 => t - (2+1+2)=2-5=-3 ->0.
down-left: down=6, left=7 => 2-15=-13 ->0.
down-right: down=6, right=1 => t - (6+1+2)=2-9=-7 ->0.
So sum_corners =0.
Thus V =13 -1 =12. That's >=10, so answer t=2. Works.
Check t=1 gave 5, t=2 gave 12, so answer 2.
Now check another scenario to ensure corner counts are correct when they activate.
Consider n=5, start at (1,1) i.e., top-left corner. Then up=0, left=0, down=4, right=4.
We compute V(t). Let's manually:
t=0: V=1
t=1: Points distance 1: (1,2) and (2,1) -> 2, total 3? Actually (1,1) itself + these = 3. V=3.
t=2: distance ≤2: (1,1),(1,2),(2,1),(1,3?) (1,3) distance 2, (2,2) distance 2, (3,1) distance 2. So (1,3),(2,2),(3,1) -> total 6? Actually count: (1,1); (1,2); (2,1); (1,3); (2,2); (3,1) =>6. V=6.
t=3: Add (1,4) dist3, (2,3) dist3, (3,2) dist3, (4,1) dist3. So total 10? Also (1,5?) dist4; Wait, (1,4) valid, (2,3), (3,2), (4,1). So previous 6 +4 =10. V=10.
t=4: Add (1,5) dist4, (2,4) dist4, (3,3) dist4, (4,2) dist4, (5,1) dist4 -> total 15. But grid total is 5x5=25. So continue.
Now formula for t=4: total=2*16+8+1=32+8+1=41. Excesses: up: t-up=4-0=4 ->16; left: 4-0=4 ->16; down: t-down=4-4=0 ->0; right: t-right=4-4=0 ->0. Sum_excess=32.
Corners:
up-left: d1=0,d2=0 => t - (0+0+2)=4-2=2 => S=2 => count = (2+1)(2+2)/2 =34/2=6.
up-right: up=0,right=4 => t - (0+4+2)=4-6=-2 =>0.
down-left: down=4,left=0 => t - (4+0+2)=4-6=-2 =>0.
down-right: down=4,right=4 => t - (4+4+2)=4-10=-6 =>0.
So V = 41 - 32 + 6 = 15. Matches manual.
Check t=5: total=225+10+1=50+10+1=61. Excess: up:5-0=5 ->25; left:25; down:5-4=1->1; right:5-4=1->1. Sum_excess=25+25+1+1=52.
Corners: up-left: t - (0+0+2)=5-2=3 => S=3, count=(45)/2=10. up-right: t - (0+4+2)=5-6=-1 =>0. down-left: t - (4+0+2)=5-6=-1 =>0. down-right: t - (4+4+2)=5-10=-5 =>0. So V=61-52+10=19. Let's manually: t=5 should add points with distance 5: from (1,1) Manhattan distance 5: (1,6) out of grid; (2,5) distance? (2,5) = |1|+|4|=5? Actually (2,5): |1|+|4|=5? Wait starting at (1,1), distance to (2,5) =1+4=5; to (3,4)=2+3=5; to (4,3)=3+2=5; to (5,2)=4+1=5; to (6,1)=5 out; also (1,6) out; (5,2) we have; (2,5) valid? Column 5 is within 5, yes. Also (3,4),(4,3),(5,2) are within grid. Also (1,6) out, (6,1) out. So four new points? Also (2,5), (3,4), (4,3), (5,2). Also maybe (5,2) we already counted? Actually we haven't counted those before. Also (1,5) distance 4 already counted; (5,1) distance 4. So t=5 adds 4 points: total from t=4 (15) +4 =19. Yes matches.
So formula works.
Now consider corner counts when both d1 and d2 are large? Works.
Thus we have a closed form for V(t).
Now we need to binary search t to find minimal t such that V(t) >= c.
t ranges from 0 to something like up+down+max(left,right)? Actually maximum needed t such that entire grid is covered: That would be when V(t) = nn. The maximum Manhattan distance from (x,y) to any corner: max( up+left, up+right, down+left, down+right ). Actually the farthest cell is one of the four corners: (1,1): distance up+left; (1,n): up+right; (n,1): down+left; (n,n): down+right. So maxDist = max(up+left, up+right, down+left, down+right). At t = maxDist, the diamond covers the entire grid? Not necessarily, but if the diamond covers all corners, it covers all points? Because any point inside the rectangle has Manhattan distance <= maxDist? Actually the Manhattan distance from (x,y) to any point (i,j) is at most maxDist? Let's check: For any (i,j), |i-x| ≤ max(up, down) and |j-y| ≤ max(left,right). But the sum |i-x|+|j-y| could be up to max(up+left, up+right, down+left, down+right). Indeed the maximum possible sum over the rectangle is exactly that max. So if t >= maxDist, then for every (i,j), |i-x|+|j-y| ≤ maxDist ≤ t, so all grid points are included. So V(t) = nn for t >= maxDist. So the answer t is at most maxDist. Since n ≤ 1e9, maxDist ≤ 2e9 approximately. So binary search over t from 0 to maxDist (or up+down+left+right? Actually up+down = n-1, left+right = n-1, so maxDist ≤ 2*(n-1) < 2e9). So binary search is feasible with O(log n) steps (~31).
Now we need to compute V(t) efficiently for given t using the formulas. However, careful with overflow: t up to ~2e9, t^2 up to 4e18, which fits in 64-bit signed integer (max ~9e18). 2t^2 is up to 8e18, plus other terms may exceed 2^63-1? 2^63-1 is about 9.22e18. 8e18 is within 9e18, but plus 2t and 1, still within 9e18. However, when we compute corner counts, we have (exceed+1)*(exceed+2)/2, where exceed may be up to t - something. t up to 2e9, exceed up to similar, product up to ~4e18, halved still within range. So we should use 64-bit integers (long long in C++). In Python, big integers are fine.
But also note that we may compute negative values and then clamp to zero. So we need to ensure we treat max(0, ...). In corner formula, we compute S = t - d1 - d2 - 2; if S < 0 then count 0 else count = (S+1)*(S+2)//2.
Now, we need to verify that the inclusion-exclusion correctly counts all points when the diamond is partially outside the grid. But is there any scenario where a point lies outside the rectangle but is not captured by these four half-planes? For a finite grid, the complement is union of four half-planes: i ≤ 0, i ≥ n+1, j ≤ 0, j ≥ n+1. So yes.
Now, is there any double counting that we missed? For instance, a point that is outside top and left is counted in both top and left, but we add back top_left. That's correct. However, is there any region where three half-planes intersect? For a point to be outside top, left, and right simultaneously: would require j ≤ 0 and j ≥ n+1, impossible. Similarly top, bottom, left impossible. So only pairwise intersections exist among non-opposite half-planes. So inclusion-exclusion to second order suffices.
Now confirm that our corner counts as derived indeed count all points that are simultaneously outside top and left, etc., and that the formula is correct when t is large enough to include such points but not too large? We derived top_left = sum_{a = up+1}^{t - (left+1)} (t - a - left)?? Actually we used a = -dx, c = -dy. We had count = sum_{a = up+1}^{t - (left+1)} (t - a - left). Wait earlier we got count = t - a - B0 + 1, with B0 = left+1. So count = t - a - (left+1) + 1 = t - a - left. So we need to verify: For top-left, number of dy for given dx (dx negative) is? Let's derive directly: dx ≤ -up-1, dy ≤ -left-1, and |dx|+|dy| ≤ t. Write a = -dx, c = -dy. Condition: a >= up+1, c >= left+1, a+c ≤ t. For each a, c can be from left+1 to t - a (inclusive). So count = (t - a) - (left+1) + 1 = t - a - left. That matches.
Now sum_{a = up+1}^{t - left - 1} (t - a - left). Note that t - left - 1 is the max a such that t - a >= left+1. So a_max = t - left - 1. So if up+1 > a_max, sum empty =>0.
Now the sum formula gave (S+1)(S+2)/2, where S = t - up - left - 2 (since a_max = t - left - 1, and a_min = up+1, number of terms = (t - left - 1) - (up+1) + 1 = t - up - left - 1? Actually compute: a_max - a_min + 1 = (t - left - 1) - (up+1) + 1 = t - left - up - 1. That's number of a values. And then sum of arithmetic series: first term for a = up+1 gives term = t - (up+1) - left = t - up - left - 1. Last term for a = t - left -1 gives term = t - (t - left -1) - left = 1. So sum of descending sequence from (t - up - left - 1) down to 1. That sum = (t - up - left - 1)(t - up - left)/2? Wait sum of 1 + 2 + ... + K = K(K+1)/2, where K = t - up - left - 1? Because the terms go 1,2,...,K. Actually first term = K, last term = 1, so sum = K(K+1)/2. K = t - up - left - 1. So count = K(K+1)/2, where K = t - up - left - 1. But earlier we got (S+1)(S+2)/2 with S = t - up - left - 2. Let's compare: S = t - up - left - 2 => S+1 = t - up - left - 1 = K. So (S+1)(S+2)/2 = K*(K+1)/2. Yes matches.
So corner = max(0, t - up - left - 1) * max(0, t - up - left) / 2? Actually K = max(0, t - up - left - 1). So if t - up - left - 1 <= 0, then corner=0. But careful: When t - up - left - 1 = 0, then K=0, sum=0? Actually K=0 gives sum=0? But our earlier example with up=0,left=0,t=1: t - up - left -1 = 0, so K=0 -> sum=0. But does top-left exist at t=1? For (1,1) start, up=0,left=0,t=1: points outside top and left: dx≤-1, dy≤-1, and |dx|+|dy| ≤1: only possible (dx=-1,dy=0?) but dy=-1 required, then sum=2 >1. So none. So 0 correct. So formula: if K <=0 then 0. But wait, what about t=2, up=0,left=0: K=2-0-0-1=1 -> sum = 12/2=1. That's correct. So corner = max(0, K) * max(0, K+1) / 2? Since K = t - up - left - 1, and K+1 = t - up - left. So we can compute corner = max(0, t - up - left - 1) * max(0, t - up - left) / 2. But careful: If t - up - left - 1 < 0 but t - up - left could be positive? Example: t=1, up=0,left=0 -> t-up-left=1, t-up-left-1=0 => product 01/2=0. So fine. But if t-up-left-1 negative and t-up-left positive? That would mean t - up - left ∈ (0,1). Since they are integers, t-up-left-1 negative and t-up-left positive means t-up-left = 0? Actually t-up-left-1 negative => t-up-left ≤0? Because if t-up-left = 1, then t-up-left-1=0, not negative. So the only case where one negative and the other positive cannot happen with integers: if K negative, then K+1 ≤0 or maybe K=-1 => K+1=0; product 0. So using max(0,K)*max(0,K+1)/2 works.
However, be careful with overflow: K may be up to ~2e9, product up to 4e18, halved within 64-bit.
Alternatively, use formula: if t < up+left+2 then 0 else let s = t - up - left - 2; count = (s+1)*(s+2)//2. That's fine.
We'll adopt that.
Now, let's double-check the single side excess formula: excess = max(0, t - d)^2. For d = up, etc. We should verify that this counts all points outside the top border (i.e., i ≤ 0). But is it always a perfect square? Let's test with start (1,1): up=0, t=2: excess_up = (2-0)^2=4. Points outside top: i ≤ 0. Which points are those? dx ≤ -1? Actually i = 1+dx ≤ 0 => dx ≤ -1. Diamond radius 2: dx = -1: then |dx|=1, |dy| ≤ 1 => dy ∈{-1,0,1} => points: (-1,-1),(-1,0),(-1,1). That's 3 points, not 4. dx = -2: |dx|=2, |dy| ≤ 0 => dy=0 => point (-2,0). That's 1 point. Total 4. Good. So formula holds.
But note: In the top excess count, we included points with dy possibly also outside left/right, but that's okay.
Now, what about points that are outside top but also have i <=0 and j maybe also outside left/right? They are counted in top_excess, but then also counted in left_excess? Actually they are also in left_excess if j <=0, but left_excess counts points with j ≤ -1? Actually left excess condition: j ≤ 0? j ≤ 0 means j ≤ 0. Since j is integer, j ≤ 0 includes j=0? That's outside because columns start at 1. So yes, points with j <=0 are in left_excess. So a point with dx = -1, dy = -1 is in both top and left. Our inclusion-exclusion subtracts it twice in sum_excess, then adds back once in top_left. Good.
Thus final V(t) = total(t) - (A+B+C+D) + (AB+AC+AD+BC+BD+CD?) Actually we only have four sets, but not all intersections exist. In our case, the four sets are T, B, L, R. Intersections possible: T&L, T&R, B&L, B&R. That's it.
Thus V(t) = total - (T+B+L+R) + (TL+TR+BL+BR).
Now we can compute.
Edge Cases: When t is huge, the excess terms become large, and the corner terms also become large, and the expression should simplify to n^2. Let's verify: For t >= maxDist, we expect V(t) = n^2. Let's check that our formula yields n^2. For t large, total = 2t^2+2t+1. The excess terms: (t-up)^2, (t-down)^2, (t-left)^2, (t-right)^2. The corner terms: each corner formula is roughly (t - d1 - d2)^2/2? Actually for large t, corner ~ (t - d1 - d2)^2/2. But we want the final expression to be exactly n^2 independent of t. Let's test with symmetric case and large t: n, x, y arbitrary. Let's attempt to simplify algebraically to confirm that for t >= maxDist, V(t) = n^2. But perhaps we can trust the formula because inclusion-exclusion is exact. But we must ensure that corner counts are correctly computed and that no other intersections appear. For t large, all four excess terms and corners are non-zero. The sum should cancel out to constant n^2.
Let's check with a simple example: n=5, start (2,2) we already verified for t>=4? Actually maxDist = max(up+left=1+1=2, up+right=1+2=3? Wait (2,2): up=1,down=3,left=1,right=3? Actually n=5: up=1, down=3, left=1, right=3. maxDist = max(1+1=2,1+3=4,3+1=4,3+3=6) =>6? Actually 3+3=6. But grid size 5x5=25. At t=6, total=236+12+1=72+12+1=85. excess: (6-1)^2=25, (6-3)^2=9, (6-1)^2=25, (6-3)^2=9 sum=68. corners: up-left: exceed = 6-1-1-2=2 => count= (34)/2=6; up-right: 6-1-3-2=0 => (12)/2=1? Actually S=0 => (12)/2=1; down-left: 6-3-1-2=0 =>1; down-right: 6-3-3-2=-2=>0. Sum corners=6+1+1=8? Wait down-right 0. So total V=85-68+8=25. Yes matches.
So good.
Now we have to implement the binary search. But we must be careful: c can be as large as n^2 (1e18), which fits in 64-bit? n up to 1e9, n^2 up to 1e18, which fits within 64-bit signed? 2^63-1 ≈9.22e18, so safe. However, in Python it's fine.
Implementation steps:
Read n, x, y, c.
Compute up = x-1, down = n-x, left = y-1, right = n-y.
Define function count(t):
total = 2tt + 2*t + 1 (use 64-bit)
T = max(0, t - up); T = T*T
B = max(0, t - down); B = B*B
L = max(0, t - left); L = L*L
R = max(0, t - right); R = R*R
Corners:
TL = 0 if t < up+left+2 else: s = t - up - left - 2; TL = (s+1)(s+2)//2
TR = 0 if t < up+right+2 else: s = t - up - right - 2; TR = (s+1)(s+2)//2
BL = 0 if t < down+left+2 else: s = t - down - left - 2; BL = (s+1)(s+2)//2
BR = 0 if t < down+right+2 else: s = t - down - right - 2; BR = (s+1)(s+2)//2
result = total - (T+B+L+R) + (TL+TR+BL+BR)
But we must also clamp result to at most nn? Actually for t large, the formula should give exactly nn. However, due to potential double counting corrections? Should we enforce min(result, nn) just in case? But inclusion-exclusion should be exact, but sometimes due to negative contributions from corners when t not large enough? Actually for t not large enough, some corners may be zero. The formula should never exceed nn because it counts exactly the number of points inside rectangle. But let's test with a case: maybe when t is small, the formula could exceed n*n? No, because we are counting points inside rectangle that are within diamond. Since diamond is subset of infinite grid, the intersection cannot exceed rectangle area n^2. So result ≤ n^2. Our formula derived should satisfy that. But due to integer arithmetic, might we get a value slightly above n^2 due to miscalculation? Let's test extreme: n=1e9, start somewhere, t=1e9+some. But our formulas should yield exactly n^2 when t large enough. But there might be off-by-one errors. Let's analytically test: For t very large (>= up+down+left+right+something?), the inclusion-exclusion formula simplifies to n^2. Let's trust.
Also ensure result is non-negative: For t=0, total=1, T=B=L=R=0, corners=0, result=1. Good.
Return result.
Perform binary search for t from 0 to max_t. max_t can be set to something like maxDist: compute distances to four corners: d1 = up+left, d2 = up+right, d3 = down+left, d4 = down+right; maxDist = max(d1,d2,d3,d4). But careful: Could the needed t be larger than maxDist? Since c ≤ n^2, V(t) = n^2 for t >= maxDist, so if c = n^2, answer = maxDist. So we can set high = maxDist. Also high could be 0? But n≥1, so high≥0.
Edge: if c=1, answer=0.
So binary search low=0, high=maxDist. While low < high: mid = (low+high)//2; if count(mid) >= c: high = mid; else low = mid+1. After loop, answer = low.
Now we need to verify with some random tests (maybe small n) to ensure formula is correct.
We'll write a brute force for small n to test.
But before implementing, check corner cases:
Starting cell near border: distances like up=0, left=0, etc. The formula should handle when t - up = t, and up+left+2 may be <= t etc.
When t - up - left - 2 may be negative, we set corner=0.
Potential pitfalls: For large t, the calculations for T, B, L, R involve (t-d)^2. But t could be up to 2e9, (t-d) up to 2e9, square up to 4e18. In Python, it's fine. In C++ we need unsigned long long or long long; but 4e18 fits in signed 64-bit? 2^63-1 = 9.22e18, so safe. However, total = 2t^2+2t+1: 2*(2e9)^2 = 2*4e18 = 8e18, which is close to 9e18, still safe. But sum of several such terms could exceed 9e18 if we compute intermediate values. For example, total may be 8e18, and we subtract sum of four squares each up to 4e18, so intermediate may go negative or positive? But in C++ we need to be careful with overflow when computing expressions like total - (T+B+L+R). If T+B+L+R is larger than total, total - that becomes negative, but then we add corners, and final should be positive. However, intermediate might underflow if using unsigned? Better to use signed 64-bit and rely on the fact that the final value fits within 64-bit (≤ n^2 ≤ 1e18) but intermediate could be larger than 2^63-1? Let's evaluate worst-case magnitude.
We need to consider the maximum absolute value of intermediate expressions. For large t, total ≈ 2t^2. T ≈ (t-up)^2 ≈ t^2, similarly others. So T+B+L+R ≈ 4 t^2. So total - (T+B+L+R) ≈ -2 t^2. That's negative, magnitude up to 2*(2e9)^2 = 8e18. This is within -9e18? 8e18 is within range, but could be -8e18, which is above the minimum for int64 (-9.22e18). So safe. Then adding corners: each corner ≈ (t - d1 - d2)^2/2. For worst-case, d1,d2 small, so corner ≈ t^2/2. So sum of four corners ≈ 2 t^2. So final ≈ -2t^2 + 2t^2 = 0? Actually we expect final = n^2 ≤ 1e18, so cancellation yields a much smaller number. But the intermediate -2t^2 could be as low as -8e18, which is within int64 range. So overall, all intermediate values stay within about ±8e18, which is within 64-bit signed range (about ±9.22e18). So in C++ we can use long long (which is at least 64-bit). However, we must be careful to use (long long) t and perform multiplication with casts to avoid overflow of int (since t may be up to 2e9, which fits in 32-bit signed (2.147e9) but 2e9 is close to limit, but safe? Actually 2e9 < 2^31-1 = 2.147e9, so okay for int32, but t^2 is 4e18 > 2^31, so we need 64-bit. In C++ if we use int64 (long long) for t and all variables, it's fine. We'll store n, x, y, c as long long as well.
Now we can implement.
But we must be cautious: For the corner formula s = t - d1 - d2 - 2; s could be up to about 2e9. (s+1)*(s+2) may be up to ~4e18, again within 9e18. So safe.
Now confirm with extreme case: n=1e9, x=5e8, y=5e8, so distances symmetrical. Then maxDist = max(up+left etc) = (5e8-1)+(5e8-1) approx 1e9-2 ~ 1e9. Actually up = x-1 ≈ 5e8-1? Wait x=5e8 => up = 5e8-1? Actually if n=1e9, x=5e8, then up = 5e8-1, down = n-x = 5e8, left similarly. So maxDist = up+right? let's not bother. t up to ~1e9. So numbers are within range.
Now test more cases manually with brute for small n.
We'll produce a brute function for small n, generate random tests, compare with formula.
But since we are reasoning, we can trust.
One more nuance: The definition of seconds: "Initially there is exactly one switched on cell ... Then each second we switch on the cells that are off but have the side-adjacent cells that are on." So at second 1, the cells at distance 1 become on. So after t seconds, cells with Manhattan distance ≤ t are on. So our mapping is correct: time t corresponds to radius t.
Now, answer should be integer seconds. The binary search finds minimal t such that count(t) >= c.
Now note that the problem statement example: first test: n=6, x=4, y=3, c=1 -> output 0. Our count(0)=1 => >=1 => t=0. Good.
Now we should also consider scenario where n is huge, but t may be negative? No.
Now implement.
But careful: In binary search, high = maxDist may be insufficient if c = n^2 and maxDist is the exact distance to the farthest corner, then count(maxDist) = n^2? Let's verify: For farthest corner, its distance is maxDist. At t = maxDist, does the diamond include all cells? For any cell, its distance ≤ maxDist, so yes, all cells are included. So V(maxDist) = n^2. So high = maxDist is adequate.
Now check if there is any case where maxDist is not enough to cover all cells? Actually, if a cell has distance exactly maxDist, it is included at t = maxDist. So all cells have distance ≤ maxDist, so yes, included. So high = maxDist.
Now test: n=2, start (1,1). Grid:
(1,1) (1,2)
(2,1) (2,2)
Distances: (1,1):0; (1,2):1; (2,1):1; (2,2):2. So maxDist=2. At t=2, V should be 4. So high=2 works.
Now compute using formula: up=0,down=1,left=0,right=1. For t=2: total=24+4+1=13. excess: up: (2)^2=4, down: (2-1)^2=1, left:4, right:1 sum=10. corners: up-left: t-up-left-2=2-0-0-2=0 => (12)/2=1? Wait s=0 => (1*2)/2=1. up-right: t-up-right-2=2-0-1-2=-1 =>0. down-left: 2-1-0-2=-1 =>0. down-right: 2-1-1-2=-2 =>0. So V=13-10+1=4. Good.
Now test scenario where c is less than initial count (1) but maybe 0? c≥1? Actually c ≥ 1? Input says 1 ≤ n, c ≤ 10^9, c ≤ n^2. So c could be 0? No, c ≥ 1 because 1 ≤ c. So at least c=1. So t=0 always possible if c=1.
Now consider case where the starting cell is not inside grid? But it's inside.
Now we should also consider the possibility that the grid is so large that the diamond never hits the borders for the t we care about, i.e., t is less than all distances to borders: t <= min(up,down,left,right). Then excess terms are zero, corners zero, and V(t) = total(t). So we can shortcut by setting high to maybe something like 2e9, but binary search fine.
Now about performance: Binary search on t from 0 to maxDist (<=2e9) will have at most 31-32 iterations. Each count(t) does constant operations. So fine.
Now we must be absolutely sure about the formulas for corners when d1+d2+2 > t. Implementation wise, we'll compute s = t - d1 - d2 - 2, if s < 0 then corner=0 else corner = (s+1)*(s+2)//2.
But we might also need to handle the case where s is large and (s+1)(s+2) could overflow even in Python? Python big ints fine. In C++ we use unsigned long long or long long. But note that (s+1)(s+2) might overflow 64-bit if s is around 2e9? (2e9+1)(2e9+2) ≈ 4e18 + something, still within 9e18? Actually (2e9)^2 = 4e18, plus linear terms. 2e9 * 2e9 = 4e18. The product (s+1)(s+2) is roughly s^2 + 3s + 2, which for s=2e9 is ~4e18 + 6e9 +2, about 4.000000006e18, still less than 9e18. So safe.
Now confirm with s up to maybe 2e9? Actually t max about 2e9, d1 and d2 at least 0, so s max <= t - 2, about 2e9-2, same.
So all good.
Now one more nuance: In the problem, n, x, y, c are up to 1e9, but n could be 1e9, but x and y are within n. So distances up to 1e9-1. So t up to 2e9-2.
Now we need to be careful with the data types in Python (no problem). But if implementing in C++, use long long (64-bit) for all.
Now, potential issues: When computing total = 2tt + 2t + 1, if t is 2e9, 2tt = 24e18 = 8e18, which may exceed 2^63-1? 8e18 < 9.22e18, so okay. But if t is 2.5e9? But t max is about 2e9, so safe.
Now, to avoid any possible overflow in intermediate addition before subtraction, we could compute using unsigned long long for positive values and rely on signed for final. But it's safe.
Now let's double-check the derivation of single side excess formula: count of points with i <= 0 (dx <= -up-1) and |dx|+|dy| ≤ t. We set a = -dx, a from up+1 to t. For each a, number of dy such that |dy| ≤ t - a is 2*(t-a)+1? Wait, earlier we said 2*(t-a)+1. But then we summed that and got sum_{a=up+1}^t (2*(t-a)+1). This sum equals (t - up)^2. Let's verify: sum_{a=up+1}^t (2*(t-a)+1) = sum_{k=0}^{t-up-1} (2*k+1) with k = t-a. Since when a = up+1, k = t-up-1; when a = t, k=0. So sum_{k=0}^{M-1} (2k+1) where M = t-up. That's M^2. So yes, that yields (t-up)^2.
But note: This counts all dy that satisfy the diamond condition, regardless of whether dy also goes out of left/right. That's fine. So T = (t-up)^2 when t>up else 0.
Now confirm that for top excess, we need to consider points with i <= 0. But we used condition dx <= -up-1. However, is it possible that there are points with i <= 0 but also with dx > -up-1? No, because i = x+dx <= 0 => dx <= -x = - (up+1). Since up = x-1, so -x = -(up+1). So yes.
Thus correct.
Now double-check the corner counts: For top-left, we counted points with i <= 0 and j <= 0, i.e., dx <= -up-1 and dy <= -left-1. In our counting, we used a = -dx, c = -dy with a ≥ up+1, c ≥ left+1, a+c ≤ t. The number of dy for given dx? But note: For a fixed dx (negative), the condition |dx|+|dy| ≤ t implies |dy| ≤ t - |dx|. For the top-left region, we also require dy ≤ -left-1, i.e., dy is negative and its absolute value is at least left+1. So the range for dy is from - (t - |dx|) up to - (left+1). The number of integer dy in that range? If left+1 ≤ t - |dx|, then number = (t - |dx|) - (left+1) + 1 = t - |dx| - left. If left+1 > t - |dx|, then 0. So this matches our count: for each a = |dx| = -dx (a from up+1 to t - left - 1), count = t - a - left. And sum we computed gave the triangular number. Good.
Now confirm that for top-right, we have dy ≥ right+1, i.e., dy positive. Then condition: dy ≤ t - a. So number of dy = (t - a) - (right+1) + 1 = t - a - right. So similar.
Thus formulas are consistent.
Now one more check: In the inclusion-exclusion, we added corners back. But is there any intersection like top and bottom? No. So fine.
Now test with a case where one of the distances is zero and t is just enough to have corner but not too large. For example n=2, start (1,2). So grid 2x2, start at top right corner: x=1,y=2. Then up=0, down=1, left=1, right=0. Let's compute manually.
Grid:
(1,1) (1,2)
(2,1) (2,2)
Start (1,2) on.
t=0: only (1,2)
t=1: distance1: (1,1) and (2,2)? Actually (2,2) distance? |2-1|+|2-2|=1+0=1, and (2,1) distance? |2-1|+|1-2|=1+1=2. So t=1: (1,1) and (2,2) become on? Also (1,3) out. So on cells: (1,2), (1,1), (2,2) => 3 cells.
t=2: (2,1) distance 2, also maybe others? All cells on => 4 cells.
Now compute with formula:
up=0, down=1, left=1, right=0.
t=1: total=5. excess: up: (1-0)=1^2=1; down: max(0,1-1)=0; left: max(0,1-1)=0; right: max(0,1-0)=1^2=1; sum=2.
corners:
TL: up=0,left=1 => t-up-left-2 =1-0-1-2=-2 =>0.
TR: up=0,right=0 => 1-0-0-2=-1=>0.
BL: down=1,left=1 => 1-1-1-2=-3=>0.
BR: down=1,right=0 => 1-1-0-2=-2=>0.
V=5-2=3. Good.
t=2: total=13. excess: up: (2)^2=4; down: (2-1)^2=1; left: (2-1)^2=1; right: (2)^2=4; sum=10.
corners:
TL: 2-0-1-2=-1=>0.
TR: 2-0-0-2=0 => (1*2)/2=1.
BL: 2-1-1-2=-2=>0.
BR: 2-1-0-2=-1=>0.
So V=13-10+1=4. Good.
So formula works.
Now test a scenario where two corners both contribute: e.g., start at center of a large grid? Actually for symmetric case, corners all zero until t exceeds some sum. For start at (3,3) in 5x5, we saw only when t large both corners appear.
Now check scenario where t is very large, all four corners might be non-zero. For example n=10, start (5,5). up=4, down=5, left=4, right=5. Let t=20. All distances are less than t. Then all four corners will be positive. Let's compute quickly: TL: s = t-up-left-2 =20-4-4-2=10 => (1112)/2=66. TR:20-4-5-2=9 => (1011)/2=55. BL:20-5-4-2=9 =>55. BR:20-5-5-2=8 => (910)/2=45. Sum corners=66+55+55+45=221. Excess: up:(20-4)=16^2=256; down:(20-5)=15^2=225; left:256; right:225; sum=962. total=2400+40+1=800+40+1=841? Actually t=20: 2400=800, +220=40 => 840? Wait 2t^2+2t+1: 2400=800, 220=40, total 800+40+1=841. So V = 841 - 962 + 221 = 100. Which is n^2=100. So good.
Now we are confident.
Now implement in Python.
But before coding, one more nuance: When computing total(t) = 2tt + 2*t + 1, we need to use t as integer. For t large, it's fine.
But we might also need to guard against negative counts? In count(t), ensure we return at most n*n, but as argued, it should be exact.
Now we also need to be careful about the case where n^2 may exceed 2^63-1? In Python it's fine.
Now let's think about possible off-by-one: In the problem, after t seconds, cells with Manhattan distance <= t are on. At t=0, only the starting cell. So our mapping is correct.
Now let's think about the definition: "each second we switch on the cells that are off but have the side-adjacent cells that are on." This is simultaneous update. That is indeed BFS flood fill with Manhattan distance. So correct.
Now we can implement.
However, there is an alternative method: The number of cells turned on by time t can be computed via area of diamond clipped by rectangle. The inclusion-exclusion approach yields the formula we have.
# corners
def corner(d1, d2):
if t < d1 + d2 + 2:
return 0
s = t - d1 - d2 - 2
return (s+1)*(s+2)//2
tl = corner(up, left)
tr = corner(up, right)
bl = corner(down, left)
br = corner(down, right)
result = total - (e_up + e_down + e_left + e_right) + (tl + tr + bl + br)
# Due to possible miscalc, clamp to [0, n*n] just in case:
if result < 0: result = 0
if result > n*n: result = n*n
return result
Now binary search:
low = 0
high = max(up+left, up+right, down+left, down+right) # maxDist
while low < high:
mid = (low + high) // 2
if count(mid) >= c:
high = mid
else:
low = mid + 1
print(low)
But wait: Is maxDist the correct upper bound? Consider a cell at (1,1) in a 5x5 grid, maxDist = distance to (5,5) = 8? Actually up=0, left=0, down=4, right=4 -> up+left=0, up+right=4, down+left=4, down+right=8 -> maxDist=8. At t=8, V=25? Let's compute quickly: V(8) should be 25. So high=8 works. But if c=25, answer=8. So high = maxDist.
But is it possible that maxDist is not enough for c if c is less than n^2? That's fine; high is an upper bound on answer. For any c ≤ n^2, answer ≤ maxDist because at maxDist we have n^2. So binary search works.
Now edge: if maxDist is 0 (n=1), high=0. Then binary search loop condition low<high? low=0, high=0, loop not entered, answer=0. Good.
Now test with n=2, start (1,1) we had maxDist=2, c=4 => answer 2.
Now test with n=2, start (1,1), c=3 => answer 1? Let's check: count(1)=3, count(0)=1. So answer 1. Binary search: low=0, high=2, mid=1, count=3>=3 -> high=1, low=0, loop: low<high? 0<1 true, mid=0, count=1<3 => low=1, now low=1, high=1 exit, answer 1. Good.
Now we should also test with large values random to ensure no overflow.
Now we should also consider whether the count(t) can exceed n^2 due to our formula? In theory no, but due to integer rounding maybe if we compute total and subtract and add, it might give slightly more? Let's test with some values:
Example: n=3, x=2,y=2, t=1: total=5, excess all 0, corners 0, result=5, n^2=9, ok.
t=2: result=9, equals n^2.
For t larger, result should remain 9, but due to our formula it remains exactly 9? Let's compute t=3: we earlier got 9. Good.
Now we must be careful about the case when t is so large that corners overlap? But inclusion-exclusion already accounted.
Now final step: implement.
But wait: There is also the possibility that some points are counted multiple times in the corner add-backs? Actually we add back each corner once. Could there be points that belong to three corners? That would require i <= 0 and i >= n+1, impossible. So fine.
Now we must verify the formula for corner when d1 + d2 + 2 > t returns 0. But what if t - d1 - d2 - 2 = -1? Then s = -1, we return 0. That's correct because condition t < d1+d2+2.
Now we should also consider if t is negative? No, binary search from 0.
Now we might consider early exit: if c == 1, answer 0. But binary search will handle.
Now we need to test with random small n and all c to ensure correctness.
We can write a brute function for n up to maybe 20 and compare for all c.
But in analysis, we can reason.
Now implement in Python as:
def count(t):
total = 2tt + 2*t + 1
# excess squares
e_up = max(0, t - up)
e_up = e_up * e_up
e_down = max(0, t - down) ** 2 # but careful: max(0, t-down)**2 works but may produce int.
# but simpler: e_up = (max(0, t-up))**2
But note: In Python, we can just compute e_up = max(0, t-up) ** 2.
Now corners:
def corner(a,b):
if t < a + b + 2:
return 0
s = t - a - b - 2
return (s+1)*(s+2)//2
Now compute.
Now, we should also ensure that we don't exceed n^2 artificially. But as argued, fine.
Now, the final answer is integer.
Now let's check example where c is exactly n^2. For n=3, x=2,y=2, c=9 -> answer=2, as computed.
Now test n=3, x=1,y=1, c=9 -> answer=4, as computed.
Now test n=9, x=3,y=8, c=10 -> answer=2, as given.
All good.
Now complexity: O(log maxDist) ~ 31 iterations. Good.
Now potential pitfalls: The expression for total can be simplified? Not needed.
Now we can produce final code.
But before finalizing, let's double-check the reasoning for the excess formula: For bottom, we have condition dx >= down+1, i.e., dx >= d+1 where d = down. But note that for bottom, the forbidden region is i ≥ n+1, which corresponds to dx ≥ n+1-x = down+1. Yes. Then we counted points with dx = down + k, k>=1, and |dx|+|dy| ≤ t. But careful: For dx positive, |dx| = dx. So number of dy for each dx is 2*(t - dx) + 1, provided t - dx >= 0. So sum over dx from down+1 to t of (2*(t - dx) + 1). This sum yields (t - down)^2. Good.
Now for left: dy <= -left-1. So similar.
Now for right: dy >= right+1, sum = (t - right)^2.
Thus all consistent.
Now, confirm that the corners are only these four. Are there intersections like top and bottom? As said, impossible. left and right impossible. But what about top and bottom? Could there be a point that satisfies i ≤ 0 and i ≥ n+1? No. So no.
Now, could there be an intersection like top and something else? Already accounted.
Thus inclusion-exclusion stops at pairwise.
Now about the corner formulas: For top-left we computed (t-up-left-1)*(t-up-left)/2. Let's confirm with an example: up=0, left=0, t=2: (2-0-0-1)=1, (2-0-0)=2, product/2 =1, correct.
For up=1, left=1, t=3: (3-1-1-1)=0? Actually 3-2-1=0? Let's compute: t-up-left-1 = 3-1-1-1=0, t-up-left=3-1-1=1, product/2=0. But earlier we computed for up=1,left=1,t=3, we expected 0? Actually check: n=5, start (2,2) gave up=1,left=1,t=3 => V(3) we computed earlier as? For (2,2) in 5x5, t=3, we can compute manually: This is symmetric, corners not yet. So top-left count should be 0. So okay.
For up=1, left=1, t=4: t-up-left-1=4-1-1-1=1, t-up-left=4-2=2, product/2=1. Indeed we saw top-left contributed? In earlier symmetric 5x5 (2,2) at t=4, we got TL? Let's recompute that case: n=5, start (2,2). up=1,down=3,left=1,right=3. For t=4, we earlier computed? Actually we computed for n=5, (2,2) t=4? We computed for (2,2) in 5x5 at t=4? Let's do: total=41, excess: up (4-1)=3^2=9, down (4-3)=1^2=1, left 9, right 1 sum=20? Actually earlier for 5x5 (2,2) we used up=1,down=3,left=1,right=3. For t=4, up excess = (4-1)^2=9, down=(4-3)^2=1, left=9, right=1 sum=20. corners: TL: t-up-left-2 =4-1-1-2=0 -> (12)/2=1; TR: 4-1-3-2=-2=>0; BL:4-3-1-2=-2=>0; BR:4-3-3-2=-4=>0. So TL=1. So there is a corner contribution of 1. That matches our product/2: (4-1-1-1)=1, (4-1-1)=2 => 12/2=1. Good.
Thus corner formula can be written either using s = t-d1-d2-2 and (s+1)*(s+2)/2, or using max(0, t-d1-d2-1) * max(0, t-d1-d2) // 2.
Now let's proceed.
Now, before final answer, we should also test with extreme values to ensure performance and no overflow.
Now I'll think about potential integer division issues: None.
Now, final code.
But we must consider: The problem states that n, c are up to 10^9, but c ≤ n^2, which could be up to 10^18. However, our count function returns a number that might be up to n^2, which is up to 10^18, fits in Python int.
Now, we might want to early exit if c <= 1, but not necessary.
Now, also consider the possibility of negative result from count(t) if t is very small? No.
Now we are ready.
Now I will produce final answer in Python.
But before writing final solution, I want to test with random small n by mental simulation or maybe we can think of a tricky case.
Case: n=1e9, x=1, y=1, c=1. Then answer 0.
Case: n=1e9, x=1, y=1, c=1e18 (which is n^2). Then maxDist = up+right? Actually up=0, right = n-y = 1e9-1? Actually y=1 => right = n-1 = 1e9-1. So maxDist = down+right = (n-x)+(n-y) = (1e9-1)+(1e9-1) = 2e9-2. So answer = 2e9-2. That's large but binary search will handle.
Now compute count(t) for t = 2e9-2. This will involve large numbers but okay.
Now we should also consider the case where t - d might be exactly 0: then excess = 0, fine.
Now, double-check that for t large, the corner formulas may produce numbers that when added back, result exactly n^2. That's fine.
Now potential subtlety: Are we double-counting points that are outside the grid but counted in corners as we add back? For example, a point that is outside top and left is counted in top and left (subtracted twice) and then added back once, so net subtracted once. That's correct because it should be excluded from the valid set. So final inclusion-exclusion yields points that are not in any forbidden set, i.e., inside rectangle.
Now ensure that our corner functions indeed count all points that are in both forbidden sets. For top-left, we count points with i ≤ 0 and j ≤ 0. But is there any point that is both top and left but not counted by our a,c method? Our method counts all integer (dx,dy) with dx ≤ -up-1 and dy ≤ -left-1 and |dx|+|dy| ≤ t. That's precisely the intersection. So correct.
Now think about whether there is any point that is in top and left but also perhaps in right? Impossible.
Now overall the formula seems robust.
Now I will implement.
But before final answer, I'll also consider an alternative simpler method: sum over rows. But inclusion-exclusion is fine.
Now we might want to micro-optimize: Actually the formula can be simplified to something like:
count = 1 + 2t(t+1) - sum over sides (t-d_i)^2_+ + sum over corners f(t - d_i - d_j) where f(s) = (s+1)*(s+2)/2 for s >=0 else 0.
Now we need to be careful: In some derivations, the corner formula might be (t - d1 - d2 - 1) choose 2? Actually (s+1)(s+2)/2 is triangular number T_{s+1} where T_n = n(n+1)/2. So if we define u = t - d1 - d2 - 1, then if u >=0, count = (u+1)(u+2)/2 = T_{u+2}? Not important.
Now I'll write the code.
Now test with provided examples:
Example 1: n=6,x=4,y=3,c=1 -> up=3,down=2,left=2,right=3. maxDist = max(3+2=5,3+3=6,2+2=4,2+3=5) =6. Binary search: low=0, high=6. mid=3 -> count(3)? Let's compute quickly: count(0)=1. So answer 0. Our search should find 0. We'll test.
Compute count(0): total=1, all excess 0, corners 0, result=1 >=1 => high will become 0? Let's simulate: low=0, high=6, while low<high: mid=3, count(3) >=1? Yes, so high=3; next mid=1, count(1) >=1 => high=1; next mid=0, count(0) >=1 => high=0; now low=0, high=0 exit. So answer 0. Good.
Example 2: we already did, answer 2.
Now let's test some random small n with brute to be absolutely sure.
Now n=3, x=2, y=2, c=8 => t=2 gives 9? Actually t=2 gives 9, but c=8, so t=2 still works. But maybe t=1 gives 5, t=2 gives 9, so answer 2. Because we need at least 8, minimal t is 2? Check if t=1 gives 5 <8, so yes 2. So answer 2.
Now n=3, x=1, y=1, c=8 => t=3 gives 8, t=4 gives 9, but t=3 is enough? Actually t=3 gave 8 as we computed, so answer 3. Good.
Now let's test n=3, x=1, y=1, c=9 => answer 4.
Now test n=4, start (2,2). Grid 4x4=16. up=1,down=2,left=1,right=2. Compute count(t) manually? We can compute using formula.
Let's compute t=2: total=13. excess: up: (2-1)=1^2=1; down: (2-2)=0; left:1; right:0 sum=2. corners: TL: t-up-left-2=2-1-1-2=-2 =>0; others 0. So V=11? Actually total 13-2=11. Does that match expectation? Manhattan distances: For 4x4 center-ish, let's list: coordinates 1..4. Start (2,2). Distance 0: (2,2). Distance 1: (1,2),(2,1),(2,3),(3,2) ->4. Distance 2: (1,1),(1,3),(2,0?) out, (2,4) (3,1),(3,3),(4,2) etc. Let's compute all cells within distance 2: (2,2) dist0; (1,2),(2,1),(2,3),(3,2) dist1; distance 2: (1,1):|1|+|1|=2; (1,3):2; (2,0) out; (2,4):2; (3,1):2; (3,3):2; (4,2):2; (2,0) not in grid; (0,2) out; (1,2?) already counted. So cells: (1,1),(1,3),(2,4),(3,1),(3,3),(4,2) ->6 cells. Also (1,2?) already counted. So total: 1+4+6=11. Good.
t=3: total=2*9+6+1=25. excess: up: (3-1)=2^2=4; down: (3-2)=1^2=1; left:4; right:1 sum=10. corners: TL: 3-1-1-2=-1=>0; TR: 3-1-2-2=-2=>0; BL:3-2-1-2=-2=>0; BR:3-2-2-2=-3=>0. V=15. Let's enumerate distance 3 cells: add those with distance exactly 3. From (2,2), distance 3: (1,4):1+2=3; (2,5) out; (3,4):1+2=3? Actually (3,4):|1|+|2|=3; (4,1):|2|+|1|=3; (4,3):|2|+|1|=3; (4,4):|2|+|2|=4; (1,0) out; (0,2) out; (3,0) out; (5,2) out; (2,-1) out; So new: (1,4),(3,4),(4,1),(4,3). Also (2,5) out, (5,2) out. So 4 cells, total 11+4=15. Good.
t=4: total=216+8+1=41. excess: up: (4-1)=3^2=9; down: (4-2)=2^2=4; left:9; right:4 sum=26. corners: TL: 4-1-1-2=0 => (12)/2=1; TR:4-1-2-2=-1=>0; BL:4-2-1-2=-1=>0; BR:4-2-2-2=-2=>0. So V=41-26+1=16. That's whole grid. Indeed t=4 covers all cells? The farthest corner: (1,1) distance 2? Actually (1,1) distance 2; (4,4) distance |2|+|2|=4; so yes, t=4 covers all. So V=16. Good.
Thus formula works.
Now test scenario where the diamond hits two corners but not the other two. E.g., n=4, start (1,1). up=0, down=3, left=0, right=3. t=3: total=25. excess: up: (3)^2=9; down: (3-3)=0; left:9; right:0 sum=18. corners: TL: t-up-left-2=3-0-0-2=1 => (23)/2=3? Actually s=1 => (23)/2=3; TR: 3-0-3-2=-2=>0; BL:3-3-0-2=-2=>0; BR:3-3-3-2=-5=>0. V=25-18+3=10. Let's enumerate: Grid 4x4, start (1,1). At t=3, what cells are on? Let's compute manually: Cells with distance ≤3 from (1,1). That includes all cells except possibly (4,4) distance 6, (4,3) distance 5, (3,4) distance 5, (4,2) distance? (4,2): |3|+|1|=4, (2,4): |1|+|3|=4, (4,1): |3|+|0|=3, (1,4): |0|+|3|=3, (3,3): |2|+|2|=4, etc. So within distance 3: (1,1)0; (1,2)1; (1,3)2; (1,4)3; (2,1)1; (2,2)2; (2,3)3; (2,4)4>3 no; (3,1)2; (3,2)3; (3,3)4 no; (4,1)3; (4,2)4 no; (4,3)5 no; (4,4)6 no. Also (3,? ) (3,2) yes. So cells: (1,1),(1,2),(1,3),(1,4),(2,1),(2,2),(2,3),(3,1),(3,2),(4,1) => total 10. Yes matches V=10. So corner TL added back 3 points? Which are points outside both top and left? Actually TL counts points that are i<1 and j<1, i.e., outside. But we added back TL because they were subtracted twice? Wait inclusion-exclusion: total includes all diamond points, including those outside grid. We subtract top and left excess, which removes points outside top and outside left. But points outside both top and left are subtracted twice, so we add back once. That's TL count = 3. But those points are not in the grid, so they should not be in final count. Let's check: total diamond points within infinite grid at t=3: total=25. Which points outside grid are we counting? Top region: points with i ≤ 0. Left region: j ≤ 0. TL region: both. Our subtract top and left: we remove all points with i ≤ 0 and all points with j ≤ 0. That removes TL twice. Then we add back TL once. Net: points with i ≤ 0 or j ≤ 0 are excluded exactly once. So final count includes only points with i>=1 and j>=1, but also must satisfy i<=n and j<=n? Wait we haven't considered bottom and right. In this case, bottom and right excess are zero because t=3, down=3, right=3, so t-down=0, t-right=0. So we haven't subtracted bottom or right. But our grid constraints also require i <= n and j <= n. However, points with i > n or j > n are included in total and not subtracted because bottom/right excess are zero? But for t=3, down=3, t-down=0, so bottom excess = 0. But are there points with i > 4? i > n? Since n=4, i=5 requires dx = 4, which with |dx|+|dy| ≤ 3 cannot happen because |dx|≥4. So indeed no points beyond bottom or right. Similarly no points beyond right. So the only boundaries relevant are top and left. So after subtraction of top and left and adding back TL, we have exactly points with i>=1 and j>=1. That matches the grid because i<=n and j<=n automatically satisfied. So final count is 10. Good.
Thus TL addition is necessary to avoid over-subtraction.
Now for scenario where bottom and right are also relevant, the formula handles.
Now I think we are ready.
One more check: In the corner function, for TL we used d1 = up, d2 = left. But what about the corner that is top and right? That uses up and right. Good.
Now implement.
We'll also add a safeguard: if count(mid) might be > n^2 due to some off-by-one, we cap at nn, but that's not needed. However, I'll add a min(result, nn) just to be safe, but it shouldn't happen.
Now test with some random values (I can simulate mentally few).
We've tested many.
Now I'll provide final solution.
But I want to thoroughly test with a random small n and all possible x,y,c using brute. Since I'm not running code here, I'll reason. But let's design a mental test for n=3, all start positions, and compute count(t) using our formula and compare with brute enumeration. We can do a few.
n=3, start positions:
Case (1,1): up=0,down=2,left=0,right=2.
We already computed t=0..4 values: 1,3,6,8,9. That matches enumeration.
Case (1,2): up=0,down=2,left=1,right=1.
Let's compute manually: Grid points: (1,1)(1,2)(1,3)(2,1)(2,2)(2,3)(3,1)(3,2)(3,3).
Start (1,2). Distances:
(1,2):0
(1,1):1; (1,3):1; (2,2):1 ->3
(2,1): |1|+|1|=2; (2,3):2; (3,2):2 -> +3 total 7? Actually also (1,2) already counted, plus dist1 gives 1+3=4? Let's list: t=0:1
t=1: add (1,1),(1,3),(2,2) -> total 4.
t=2: add (2,1),(2,3),(3,2) -> total 7.
t=3: add (3,1): |2|+|1|=3? Actually (3,1): |2|+|1|=3; (3,3): |2|+|1|=3; also (2,2) already, (1,0) out, etc. So add (3,1),(3,3) -> total 9.
t=4: all 9? Actually t=3 already gave all? Wait check (2,2) already at t=1, (3,2) at t=2, (1,2) at t=0, (1,1),(1,3) at t=1, (2,1),(2,3) at t=2, (3,1),(3,3) at t=3. So at t=3, all 9 cells are covered. So V(3)=9. So the sequence: t=0:1, t=1:4, t=2:7, t=3:9.
Now case (2,1): up=1,down=1,left=0,right=2. Symmetric to (1,2) essentially, so should match.
Now case (2,2): we did.
Thus all good.
Now we should also test a case where start is not near corners but grid is not square? But n is square? n is side length, grid is square. So all distances symmetrical in shape but not necessarily in values. Our formulas handle any distances.
Now test a case with n=5, start (3,5): x=3,y=5 => up=2,down=2,left=4,right=0. Let's compute manually quickly? Not necessary.
Now consider edge: up=0, down=0? That would mean n=1? Actually if n=1, up=0,down=0,left=0,right=0. Then t=0: count=1; t>=0: total=2t^2+2t+1. But for t>=1, diamond points exceed grid, but our formula should yield 1 for all t? Let's test: n=1, x=1,y=1, t=1: total=5. excess: up: (1-0)=1^2=1; down: (1-0)=1; left:1; right:1 sum=4. corners: TL: 1-0-0-2=-1 =>0; TR: 1-0-0-2=-1=>0; BL:1-0-0-2=-1=>0; BR:1-0-0-2=-1=>0. V=1. Good. t=2: total=13, excess: up:4, down:4, left:4, right:4 sum=16, corners: TL:2-0-0-2=0 =>1? Wait s=0 => (1*2)/2=1; similarly TR,BL,BR all s=0 =>1 each? Actually for t=2, s = t - up - left -2 = 2-0-0-2=0 => corner=1. So all four corners have 1? Let's check: TL, TR, BL, BR all have d1,d2 = (0,0) or (0,0)? For TL: up=0,left=0 => s=0 =>1; TR: up=0,right=0 => s=0 =>1; BL: down=0,left=0 => s=0 =>1; BR: down=0,right=0 => s=0 =>1. So sum corners=4. Then V = total - sum_excess + sum_corners = 13 - 16 + 4 = 1. Good. So works.
Now another extreme: n very large, start at center, t small, all excess zero, corners zero, V = total. Good.
Now we can implement.
Now about binary search boundaries: We set high = maxDist. But if n=1, maxDist = 0. So high=0 works.
Now consider if c is 0? Not possible as per constraints.
Now finalize.
But let's think about potential overflow in C++ when computing 2tt. t up to 2e9, 2tt = 2 * (4e18) = 8e18. In C++ long long (64-bit) max is 9.22e18, so safe. But if we used 2tt, note that t is int maybe 32-bit, but t*t may overflow 32-bit, so must cast to long long. In Python, it's fine.
Now we should also check that the corner formula (s+1)*(s+2)//2 is correct when s is negative? We guard with if.
Now the final code is simple.
One more thing: We could also use while low < high, typical binary search.
Now finalize answer.
I'll produce final solution in Python.
Potential improvements: Use integer arithmetic and avoid floating.
Now write the solution.
Time complexity: O(log n).
Now I'll produce final answer.
But I recall a similar problem from Codeforces (Problem 371C? Actually not). Anyway.
Now final answer. ```python
import sys
def solve() -> None:
data = sys.stdin.read().strip().split()
if not data:
return
n, x, y, c = map(int, data)
# distances to the borders
up = x - 1
down = n - x
left = y - 1
right = n - y
code
# maximum Manhattan distance to a corner of the grid
maxDist = max(up + left, up + right, down + left, down + right)
# helper to compute corner contribution
def corner(d1: int, d2: int, t: int) -> int:
if t < d1 + d2 + 2:
return 0
s = t - d1 - d2 - 2 # s >= 0
return (s + 1) * (s + 2) // 2
# number of switched on cells after t seconds
def count(t: int) -> int:
total = 2 * t * t + 2 * t + 1
# points outside each side
e_up = max(0, t - up) ** 2
e_down = max(0, t - down) ** 2
e_left = max(0, t - left) ** 2
e_right = max(0, t - right) ** 2
# corner corrections
tl = corner(up, left, t)
tr = corner(up, right, t)
bl = corner(down, left, t)
br = corner(down, right, t)
ans = total - (e_up + e_down + e_left + e_right) + (tl + tr + bl + br)
# the result cannot exceed the total number of cells
if ans > n * n:
ans = n * n
if ans < 0:
ans = 0
return ans
# binary search for minimal t
lo, hi = 0, maxDist
while lo < hi:
mid = (lo + hi) // 2
if count(mid) >= c:
hi = mid
else:
lo = mid + 1
print(lo)