You are given an array a of n integers, where n is odd. You can make the following operation with it:
Choose one of the elements of the array (for example a_i) and increase it by 1 (that is, replace it with a_i + 1).
You want to make the median of the array the largest possible using at most k operations.
The median of the odd-sized array is the middle element after the array is sorted in non-decreasing order. For example, the median of the array [1, 5, 2, 3, 5] is 3.
Input
The first line contains two integers n and k (1 ≤ n ≤ 2 ⋅ 10^5, n is odd, 1 ≤ k ≤ 10^9) — the number of elements in the array and the largest number of operations you can make.
The second line contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^9).
Output
Print a single integer — the maximum possible median after the operations.
Examples
Input
3 2
1 3 5
Output
5
Input
5 5
1 2 1 1 1
Output
3
Input
7 7
4 1 2 4 3 4 4
Output
5
Note
In the first example, you can increase the second element twice. Than array will be [1, 5, 5] and it's median is 5.
In the second example, it is optimal to increase the second number and than increase third and fifth. This way the answer is 3.
In the third example, you can make four operations: increase first, fourth, sixth, seventh element. This way the array will be [5, 1, 2, 5, 3, 5, 5] and the median will be 5.
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 find maximum possible median after at most k increment operations. We can only increase elements, not decrease. n is odd. The median is the element at position (n+1)/2 after sorting. We want to maximize its value.
Classic problem: we can sort the array. Let m = (n+1)//2 - 1 index (0-indexed). The median initially is a[m] after sorting. To maximize median, we need to increase a[m] and also possibly some elements after it? Actually to raise median, we need to raise the element at position m. But if we raise a[m] too much, we might need to also raise some elements before it? No, because median is defined after sorting. We can arbitrarily increase any elements. Since we only increase, we can make the median larger by focusing on the median element and the ones to its left? Wait: consider sorted array a[0] <= a[1] <= ... <= a[n-1]. Median is a[m]. If we increase a[m] by some amount, the array may no longer be sorted, but we can re-sort. However, since we can only increase, the elements that are smaller than the new median may become less than the median; that's fine because median is the middle after sorting. But if we increase the median, we might also need to increase some elements to the right? Actually, after increasing a[m] by x, the element a[m] becomes a[m]+x. It could become larger than some elements to its right (i.e., a[m+1], a[m+2], ...). But after sorting, the median would be the (m+1)th smallest. If we increase a[m] and perhaps other elements, we need to ensure that after all operations, when we sort, the element that ends up in the median position is as high as possible. Since we can increase any elements, we could also increase elements to the right to keep them above the median. But that is wasteful because we want to maximize the median, we can just increase the median and the elements after it don't matter because they are already >= original a[m]. However, if we increase a[m] to be larger than some elements originally to its right, then after sorting those elements will be smaller than the increased a[m] and might shift left, potentially becoming the median? Actually, let's simulate: sorted array: a0 <= a1 <= ... <= a_{m-1} <= a_m <= a_{m+1} <= ... <= a_{n-1}. Suppose we increase a_m by x, so it becomes a_m + x. If a_m + x > a_{m+1}, then after sorting, the array will be reordered. The increased a_m may move to a later position. In the final sorted order, the median (position m) might be a_{m+1} (or some other) if the increased a_m moves beyond m. That could reduce the median value if we aren't careful. To ensure that the median becomes the increased value, we need to make sure that after increasing, there are at least m+1 elements <= a_m + x (including the increased a_m itself). But we have control: we can also increase some of the elements after m to keep them >= a_m+x, but we don't have to; alternatively we can increase a_m and also increase enough of the smaller elements (the ones before m) to keep them below? Actually, the median is the (m+1)th smallest. To have the median value equal to some target T, we need at least m+1 elements with value <= T. But also we need that the (m+1)th element after sorting is T (or at most T). However, we can try to achieve that the final median value is some number M. Is it possible? We can perform operations to increase any elements. So we want to maximize M such that we can perform at most k increments to make the median at least M.
We can think of the sorted array. Let the current median be a[m]. Since we can only increase, any element can only become larger. To achieve median M, we need that after operations, the sorted array has its (m+1)th element >= M. Since we can increase elements, we can try to raise the (m+1)th element directly by increasing the element at position m and possibly those to its right. But we must ensure that there are at least m+1 elements that are <= some value? Actually, after increasing, the sorted order may change. Let's formalize:
Given we can increase any elements, we can transform the array into any array b where b_i >= a_i for all i, and sum(b_i - a_i) <= k. We want to maximize median(b) (where median is the (n/2+1)th smallest after sorting b). So we need to maximize the value v such that there exists b with b_i >= a_i, sum(diffs) <= k, and median(b) >= v.
Since we want to maximize the median, we can aim to make at least m+1 elements (the "lower half" including median) be at least some value? Actually to have median >= v, we need that the (m+1)th smallest element is >= v. Equivalent: at least n - m elements can be anything, but there should be at least m+1 elements with value >= v? No: The (m+1)th smallest >= v means that there are at least m+1 elements that are >= v? Wait: If the (m+1)th smallest is >= v, then the smallest m elements can be less than v, but the (m+1)th element is >= v, so there are at least n - m elements that are >= v? Let's think: In sorted order, b[0] <= b[1] <= ... <= b[n-1]. Condition b[m] >= v (median index m). Since there are m elements before it (indices 0..m-1) which could be less than v, that's fine. So we just need b[m] >= v. But b[m] is the element at position m after sorting. This element could be any original element that we increased. So we need to ensure that after increases, there is some element that becomes at least v and that after sorting, that element is in position m. However, we have control to raise some elements. A typical greedy approach: sort the array. Focus on the elements from index m to n-1 (including median and to the right). To increase the median, we essentially need to increase the elements from m to some index so that they are at least some target, ensuring that the element at index m is at least target. One known solution: We can try to raise a[m] to some value, and also raise a[m+1], a[m+2], ... as needed to keep them not less than the new median? Actually, consider we want median = M. We can achieve this by making sure that at least m+1 elements are >= M? Wait: The median is the middle element, so if we have many elements >= M, the median could be >= M if there are enough such elements. Let's derive conditions.
We can increase any elements. We want b[m] >= M. Since b is sorted, the m-th element is at least M. That implies that at most m elements can be less than M (because if there were m+1 elements less than M, then the (m+1)th smallest would be less than M, contradicting b[m] >= M). So we need that after operations, at most m elements are < M. Equivalently, at least n - m elements are >= M? Actually n - m = m+1? Since n is odd, n = 2m+1, so m = (n-1)/2, and n - m = m+1. So indeed, we need at least m+1 elements >= M. So to have median >= M, we need that the number of elements that are at least M after operations is at least m+1.
But note: We can increase elements to be >= M. We need to ensure we have enough capacity (k) to raise enough elements to at least M. But also we must not decrease any. So the minimal cost to achieve at least m+1 elements >= M is: we take the m+1 smallest elements among those that are currently below M? Actually we need to select some elements to increase to M. Since we want to minimize total increments (to stay within k), we should pick the elements that are easiest to raise, i.e., those with the largest initial values (closest to M). To get at least m+1 elements >= M, we can consider the current sorted array. Let's denote the sorted a. To have at least m+1 elements >= M, we need to ensure that for the (m+1) largest elements, after raising some of them if they are below M, they become >= M. But note that we cannot decrease the other elements; they might already be >= M, or we might also raise them but it's not necessary. Since we want to minimize cost, we should only raise the ones that are below M, and among those, we should raise the ones that are largest initially because they require less increment. However, we are constrained by which elements we can raise: we can raise any elements. But the "m+1 elements" we need to be >= M can be any subset of size m+1. Ideally, we want the subset of size m+1 with the highest initial values, because that minimizes total increments needed to bring them to M. But we must also consider that we may also need to ensure that the median is exactly the (m+1)th smallest, but if we raise some elements, the ordering could change. However, if we ensure that at least m+1 elements are >= M, then the (m+1)th smallest is at least M (since the smallest m+1 elements include at most m that are < M, but wait: if at least m+1 are >= M, then the (m+1)th smallest (which is the median) is at least M, because even if all elements less than M are placed in the smallest positions, there could be more than m elements less than M? Actually, if we have at least m+1 elements >= M, then the count of elements < M is at most n - (m+1) = m. So the number of elements strictly less than M is at most m. In sorted order, the first m elements could be < M, but the (m+1)th must be >= M because there are at most m elements less than M. Yes, that's correct. So the condition for median >= M is: after operations, at most m elements are < M, i.e., at least m+1 elements are >= M.
Thus we can binary search M. For a given M, compute the minimal number of operations needed to make at least m+1 elements >= M. We need to check if that minimal ops <= k.
How to compute minimal ops? Since we can increase any elements, we should select the m+1 elements with the largest initial values and increase each of those that are below M up to M. But is it always optimal to select the m+1 largest? Suppose we have some elements that are already >= M, they automatically count. For the ones below M, we must raise some to M. We need at least m+1 total elements >= M. So we can count how many are already >= M. If that count >= m+1, cost=0. Else we need to raise some of the remaining elements to M. Which ones to raise? The ones requiring the smallest increments, i.e., the highest among those below M. So we should take the largest elements among those currently below M, raise them to M. Since we need to raise exactly (m+1 - count_ge) elements, we pick the (m+1 - count_ge) largest below M. But is there any nuance about ordering? Could raising an element cause it to surpass other elements and affect the count? No, count of >=M is simply based on final values. So this greedy works.
But we must also ensure that we are not raising elements that are already >= M (waste). So minimal cost is sum over selected elements (M - a_i) for those that are below M. However, note that the selected elements might be any indices; we don't care about their original positions relative to median. So we can sort the array descending, take the top m+1 elements? Actually we need m+1 elements with the largest initial values, but those are exactly the largest m+1 elements overall. Let's verify: If we take the m+1 largest elements (by value) from the original array, and raise those that are below M to M, we will achieve at least m+1 elements >= M? But careful: Suppose we raise some of the largest elements, they might already be >= M or become >= M. But what about an element that is not among the largest m+1? Could it be more efficient to raise a smaller element instead of a larger one? No, because raising a smaller element requires more increments. So to minimize cost, we should always raise the ones that are closest to M, which are the largest among those below M. Those are exactly the top elements. However, consider this: The largest m+1 elements may include some that are already >= M; we don't need to raise those. The remaining needed count is from the rest. But if we restrict ourselves to the largest m+1 elements, are we guaranteed that the ones we raise are the largest below M? Not necessarily: The largest m+1 elements could all be >= M, then cost 0. If some of them are below M, then they are the highest below M because any element outside the largest m+1 is smaller (or equal) to the smallest of the largest m+1? Actually by definition, the largest m+1 elements are the top m+1 in sorted order descending. Among elements below M, the largest ones would be those with highest values, which are exactly the ones that appear in the sorted list before we encounter many >=M. But it's possible that some element among the largest m+1 is below M, but there might be an element outside the largest m+1 that is also below M but with same value? Since the largest m+1 are the top values, any element not in that set has value <= the (m+1)th largest. So if the (m+1)th largest is below M, then all elements outside the top m+1 are <= that value, so they are also below M and require at least as much increment (or more) than the (m+1)th largest. So it is optimal to raise the largest m+1 elements that are below M, specifically the ones we need to raise among the top m+1. However, we might not need to raise all of the top m+1; we only need enough to reach m+1 >= M. So we can take the array sorted in non-decreasing order. Let's define sorted a[0..n-1]. m = (n-1)/2. We need at least m+1 elements >= M. Since there are n elements, the number of elements already >= M is count_ge = number of i such that a[i] >= M. If count_ge >= m+1, then cost 0.
Otherwise, we need to raise some of the elements that are < M. Which ones? The ones that are closest to M, i.e., the largest among those < M. In the sorted order, the largest elements are at the end. So we can consider the last m+1 elements? Actually we need exactly m+1 elements total to be >= M. So we can consider the (m+1) elements that are "candidates" for being >= M after operations. Since we can raise any, we can aim to make the (m+1) largest after operations be >= M. That suggests we can look at the elements from index m to n-1? Because after sorting, the indices from m to n-1 are exactly the largest m+1 elements (since n = 2m+1). Indeed, because n = 2m+1, the median index is m. The elements a[m], a[m+1], ..., a[n-1] are the top m+1 elements. So if we can make these m+1 elements all >= M, then we have at least m+1 elements >= M. Is it necessary to raise all of them? Not exactly: If some of these are already >= M, that's fine. But if some are below M, we need to raise enough among them such that overall we have m+1 >= M. However, note that these are the largest m+1 elements initially, so any element not in this set is smaller. So if we need to raise additional elements beyond this set, we would have to raise an element that is smaller than the smallest in this set, which would require more increments. So it's optimal to only raise elements from this set. So the minimal cost is: sum_{i=m}^{n-1} max(0, M - a[i]), but we only need to raise enough until we have m+1 >= M. Actually we need exactly m+1 elements >= M. Among the set S = {a[m], a[m+1], ..., a[n-1]}, some count c = number of elements in S that are >= M. If c >= m+1, cost 0. Else we need to raise some of the other elements in S, which are below M, to M. But we have exactly |S| = m+1 elements. So to have at least m+1 elements >= M, we need all elements of S to be >= M? Because S has size m+1. If any element in S is < M, then the number of elements >= M in S is < m+1. Could we possibly achieve m+1 >= M by raising an element outside S (which is smaller) and leaving an element in S below M? That would give at most m+1 >= M? Let's see: Suppose we have S size m+1. If we have an element outside S (say a[m-1]) that we raise to M, and we leave one element in S below M. Then the total number >= M would be: from S, those already >= M (c) plus maybe the raised outside element, but we have one less in S because we left one below M. So total >= M would be c (from S, excluding the one we left below) + 1 (the raised outside) = c + 1. But originally we needed (m+1) - c more elements. If we leave one in S below, we need to compensate with one from outside. But raising an outside element may cost more than raising that S element. So it's never beneficial to skip raising an element in S if it's below M, because raising the smallest element in S (which is a[m]) is cheaper than raising any element outside S, since a[m] is the smallest in S and all outside are <= a[m] (since sorted). Actually in sorted order, a[0] <= a[1] <= ... <= a[n-1]. The set S = indices m..n-1. So the smallest element in S is a[m]. Any element outside S (indices 0..m-1) is <= a[m]. So to bring an outside element up to M, we need at least (M - a[i]) >= (M - a[m]) because a[i] <= a[m]. So raising a[m] costs (M - a[m]), which is <= cost of raising any element outside. So it is always cheaper or equal to raise a[m] rather than any outside element. Similarly, raising other S elements (which are >= a[m]) also costs less than or equal to raising outside ones. Therefore, the minimal cost to achieve m+1 elements >= M is indeed to raise the lowest elements in S first? Actually we need to cover the deficit d = (m+1) - c, where c = count of S elements already >= M. Since there are exactly |S| = m+1 elements in S, the deficit equals the number of elements in S that are < M. So we must raise all those S elements that are below M to at least M. Because if we don't raise a particular S element that is below M, we would need to raise some outside element to compensate, which would cost at least as much (since outside elements are lower or equal). But could it be cheaper to not raise some S element that is very low? Wait S elements are the largest. Actually a[m] is the smallest of S, but still may be low. If we skip raising a[m] and raise a[m-1] instead, a[m-1] <= a[m], so raising a[m-1] costs at least as much as raising a[m] (since M - a[m-1] >= M - a[m]). So it's worse. So yes, we must raise all S elements that are below M. Therefore, minimal cost = sum_{i=m}^{n-1} max(0, M - a[i]).
Thus the condition is: sum_{i=m}^{n-1} max(0, M - a[i]) <= k.
We can binary search M from a[m] up to a[m] + k (or up to a[m] + k, but we could go higher if we also raise other elements? Actually if we raise S elements, the sum of increases is at most k. The maximum M cannot exceed a[n-1] + k? But more precisely, since we only need to raise S elements, the maximum M is bounded by a[m] + k because we can put all k into raising a[m]? But consider we might also need to raise other elements? Actually the sum of increments over S is at least (M - a[m]) for each element if all are below M. But we could raise a[m] by a lot and also raise others. The maximum M we can achieve is limited by the total k. But a binary search upper bound can be a[m] + k, because we can increase a[m] by at most k to reach a[m]+k; however, if we also need to raise other elements, that would reduce the possible M. So the true maximum M might be less than a[m] + k. So we can set high = a[m] + k (or even larger, but it's safe to set high = a[m] + k, because a[m] + k is achievable if we only raise a[m] and ignore others? But if we only raise a[m] to a[m] + k, but then the median would be at most a[m] + k only if after sorting, that element remains the median. However, if a[m] + k > a[m+1], then after raising only a[m], it might move to a later position and the median might be a[m+1] which could be lower. So just raising a[m] alone might not result in median = a[m]+k. But we can also raise others to keep them >= the new value. So the condition sum_{i=m}^{n-1} max(0, M - a[i]) <= k ensures we can raise all of S to at least M. That sum is at least (M - a[m]), so M cannot exceed a[m] + k, because (M - a[m]) <= sum <= k, so M <= a[m] + k. So indeed upper bound a[m] + k is safe. But could M be larger than a[m] + k if we raise other elements? No, because sum includes (M - a[m]) >= M - a[m], so M - a[m] <= k => M <= a[m] + k. So upper bound is a[m] + k.
Thus we can binary search M from low = a[m] to high = a[m] + k, checking the sum condition.
But careful: The initial a[m] is the median after sorting. But we might also consider M larger than a[m] + k? Not possible. So binary search works.
Now check examples:
Example 1: n=3, m=1. a = [1,3,5] sorted. a[m]=3. k=2. Condition: sum_{i=1}^{2} max(0, M- a[i]) = max(0,M-3)+max(0,M-5) <= 2. For M=5: M-3=2, M-5=0 sum=2 <=2, ok. M=6: sum=3+1=4 >2. So maximum M=5.
Example 2: n=5, m=2. a = [1,2,1,1,1] sorted => [1,1,1,1,2]. m=2 => a[2]=1. k=5. Check M=3: sum i=2..4: a[2]=1 =>2, a[3]=1=>2, a[4]=2=>1, total=5 <=5 ok. M=4: 3+3+2=8 >5, so max=3.
Example 3: n=7, m=3. a = [4,1,2,4,3,4,4] sorted => [1,2,3,4,4,4,4]? Let's sort: 1,2,3,4,4,4,4. m=3 => a[3]=4. k=7. M=5: sum i=3..6: a[3]=4->1, a[4]=4->1, a[5]=4->1, a[6]=4->1 sum=4 <=7, ok. M=6: sum 2+2+2+2=8 >7, so max=5. Works.
Thus solution: sort array, set m = n//2 (0-indexed). Binary search L = a[m], R = a[m] + k (maybe bigger for safety? but we can also set R = 2e9? but binary search within long). While L < R, use mid = (L+R+1)//2 for upper bound. Compute total = sum(max(0,mid - a[i]) for i from m to n-1). If total <= k, L=mid else R=mid-1. Output L.
But need to ensure that we handle large numbers: k up to 1e9, a[i] up to 1e9, mid up to a[m]+k <= 2e9. Sum can be up to (m+1)*k, which could be up to (1e5+1)*1e9 = 1e14, so use 64-bit.
Complexities: O(n log k). n up to 2e5, fine.
But is there any nuance? Could there be a scenario where raising all S elements to M is not necessary? Let's test edge cases. Suppose we have a scenario where some elements in S are already > M, raising all of them to M would cost 0 for those. So sum includes only those below M. That is fine. What about if we don't raise all S elements, but raise some outside and keep one S element below M, maybe because raising that S element is very expensive (if M is huge) but raising outside is cheaper? But outside are smaller, so raising outside is more expensive. So no. What about if some elements in S are already far above M, and raising the lowest ones might be cheaper than raising some other elements? Already we cover.
But consider if we raise some elements in S to different values, maybe not all to exactly M, to achieve median >= M? For median >= M, we need at least m+1 elements >= M. If we raise some S elements to above M, and some not, we might still achieve >= M count. The minimal cost to get at least m+1 elements >= M is not necessarily to raise all S elements below M to exactly M. Because maybe we can raise some S elements to above M and some to below M but still achieve count? Actually if we have deficit d, we need to raise d elements to at least M. The cheapest elements to raise to M are the largest among those below M. Since S are the largest overall, the d largest below M will be among S. So we need to raise d elements from S to M. But we might also raise some of them to more than M, but that would only increase cost, not beneficial. So minimal cost is sum over the d elements with largest values below M of (M - value). But note that among S, there are exactly |S| = m+1 elements. If we count how many of S are already >= M, say c. Then deficit d = (m+1) - c. The elements in S that are below M are exactly |S| - c = m+1 - c = d. So indeed we need to raise all those d elements to M. So the sum over all S elements below M (which are exactly d elements) of (M - a[i]) is the cost. So the formula holds.
Thus binary search is correct.
Alternative solution: We can also think incrementally: Start from median value and increase it stepwise, but binary search is efficient.
We should also consider that operations can be distributed among many elements, but binary search works.
Now implement in Python:
Read n,k.
Read list a.
Sort a.
m = n // 2 (since 0-indexed).
Binary search: lo = a[m], hi = a[m] + k (but maybe we can set hi = a[m] + k + something? Actually if k is large and a[m] is large, hi could be up to 2e9, but that's fine).
While lo < hi:
mid = (lo + hi + 1) // 2
total = sum(max(0, mid - a[i]) for i in range(m, n))
if total <= k:
lo = mid
else:
hi = mid - 1
Print lo.
But we must ensure that total does not overflow in Python (Python big ints fine). However, we can early break if total exceeds k to avoid summing all if k is small, but not necessary.
But complexity O(n log k) with each check summing up to n/2 elements. n=2e5, log2(1e9)~30, total operations ~3e6, fine.
Let's test with some edge cases.
Edge case: n=1. Then m=0. Condition: sum i from 0 to 0: max(0,M - a[0]) <= k. So we can increase the only element up to a[0]+k. So answer = a[0]+k. Our binary search: lo = a[0], hi = a[0]+k, works.
Edge case: k=0, then answer = a[m]. Check: total for M=a[m] is 0 <=0, okay; for M=a[m]+1, total >=1 >0, so lo stays a[m].
Edge case: large values, a sorted descending? Actually sorted ascending: we need to ensure we pick m correctly: median is at index n//2 in 0-index (since n odd). Eg n=5, indices 0,1,2,3,4, median index = 2 which is n//2. So that's correct.
Thus solution appears correct.
But wait: there is a nuance: The operations can be applied to any element, not necessarily only the ones from m to n-1. The reasoning that we must raise all elements in S to at least M is based on minimal cost. However, what if some elements outside S are initially very close to M, maybe even greater than some in S? But since array is sorted ascending, elements outside S are the smaller half. The largest element outside S is a[m-1] which is <= a[m]. So any outside element is <= a[m]. So if an outside element is very close to M, that would imply M is close to a[m-1] but then a[m] is at least a[m-1] and thus also close. Actually a[m] >= a[m-1], so a[m] is at least as large as a[m-1]. So raising a[m] is never more expensive than raising a[m-1]. So indeed focusing on S is optimal.
But consider the possibility of raising elements outside S to be > M while leaving some inside S below M. Could that reduce cost? For median >= M, we need at least m+1 elements >= M. S has m+1 elements. If we leave an element in S below M, we need to have another element from outside S that is >= M to maintain count. That outside element would be at most a[m-1] initially. To make it >= M, we need to raise it at least M - a[m-1] which is >= M - a[m] because a[m-1] <= a[m]. So cost for raising outside is at least as much as raising a[m] (the smallest in S). So it cannot be cheaper. If we raise more than one from outside, similar reasoning. So raising all S below M is minimal.
Thus binary search is correct.
Now, we need to ensure that M can be as high as a[m] + k, but sometimes k may be huge and a[m] + k may exceed 1e18? But a[m] up to 1e9, k up to 1e9, so a[m]+k up to 2e9, safe. However, if we use hi = a[m] + k, but what if we can achieve M > a[m] + k by also raising other elements? As argued, because (M - a[m]) <= total cost <= k, M cannot exceed a[m] + k. So hi = a[m] + k is an upper bound.
One more nuance: The sum over i from m to n-1 of max(0, M - a[i]) could be computed quickly. Could there be a scenario where M is large and the sum becomes > k quickly. That's fine.
Thus solution is straightforward.
But let's double-check with a more complex example to ensure no oversight: n=5, a = [1,100,101,102,103]. Sorted: [1,100,101,102,103], m=2, a[m]=101. Suppose k=10. We can raise median. Sum for M=111: need to raise a[2]=101 to 111 =>10, a[3]=102=>9? Actually 111-102=9, a[4]=103=>8, sum=27 >10. But we don't necessarily need to raise all? If we raise only a[2] to 111, then the array becomes [1,100,111,102,103]. After sorting, we have [1,100,102,103,111], median=102. So median=102 not 111. To have median 111, we need at least 3 elements >=111. Currently only raising a[2] gives one element 111. a[3]=102, a[4]=103, both <111. So need to raise either a[3] or a[4] as well. Actually we need two more elements >=111? Count of elements >=111 after raising: if we raise a[2] to 111, then a[3]=102, a[4]=103; only one element >=111 (the raised one). So we need at least two more >=111 from among the rest. The only candidates are a[3] and a[4] because they are the largest. Raising them both to 111 would cost 9+8=17, plus 10 =27. So indeed sum_{i=m}^{n-1} max(0,M - a[i]) = 10+9+8=27. So M=111 not possible. Let's try M=106: sum = (106-101)=5, (106-102)=4, (106-103)=3 => total 12 >10. M=105: 4+3+2=9 <=10? 4+3+2=9. Actually 105-101=4, 105-102=3, 105-103=2 total=9 <=10. So median 105 is possible? Check: raise a[2] by4 ->105, a[3] by3->105, a[4] by2->105. Total 9. After operations, array: [1,100,105,105,105] sorted median =105. Yes. So works.
Thus algorithm passes.
Now implement. Use Python:
def solve():
import sys
input = sys.stdin.readline
n,k = map(int,input().split())
a = list(map(int,input().split()))
a.sort()
m = n//2
lo = a[m]
hi = a[m] + k # inclusive
while lo < hi:
mid = (lo+hi+1)//2
total = 0
# sum from m to n-1
for i in range(m, n):
if a[i] < mid:
total += mid - a[i]
# early break if total > k? optional
if total > k:
break
if total <= k:
lo = mid
else:
hi = mid-1
print(lo)
Edge: hi might be less than lo initially? No, k>=1, so hi >= lo.
But note: a[m] could be huge, hi might overflow? Not in Python.
Testing with examples.
Time complexity O(n log k). Fine.
However, there is an alternative approach without binary search: we can simulate increasing median by "leveling up" the median and the elements to its right. But binary search is simpler.
Let's ensure that we haven't misinterpreted the median definition: The median is the middle element after sorting in non-decreasing order. So yes.
Thus final answer.
But wait: Could there be scenario where we don't need to raise all S elements to exactly M, but raising some to M and others to a higher value to reduce cost? No, raising higher increases cost.
Thus solution is correct.
Now, produce final answer.
```python
import sys
def solve() -> None:
data = sys.stdin.read().strip().split()
if not data:
return
it = iter(data)
n = int(next(it))
k = int(next(it))
a = [int(next(it)) for _ in range(n)]
a.sort()
m = n // 2 # index of the median (0‑based)
code
lo = a[m]
hi = a[m] + k # maximal theoretically reachable value
while lo < hi:
mid = (lo + hi + 1) // 2
# total operations needed to make all a[m..n-1] at least mid
total = 0
for i in range(m, n):
if a[i] < mid:
total += mid - a[i]
if total > k: # early stop to save time
break
if total <= k:
lo = mid
else:
hi = mid - 1
print(lo)