We need to solve: given n benches with initial numbers a_i (i=1..n). m new people come, each chooses a bench to sit on. After they all sit, consider the maximum number on any bench, call that k. We want the minimum possible k and the maximum possible k.
Constraints: n up to 100, m up to 10000, a_i up to 100.
This is essentially: we have n piles with initial sizes a_i. We add m items distributed among the piles arbitrarily. Let max_i (a_i + x_i) = k, where x_i are nonnegative integers sum to m. The max possible k occurs when we try to maximize the maximum final pile. That would be achieved by putting all m new people onto the bench that already has the largest initial number? But maybe there is nuance: if we want to maximize the maximum, we want to increase one pile as much as possible, so we put all m onto a bench. But which bench? The one with the largest a_i? Yes, because if we put all onto the bench with the largest initial a_i, we get a_i_max + m. If we put onto a smaller bench, then the maximum would be max(original max, that bench + m). Since that bench + m might be less than original max + m? Actually original max might be large, but if we add m to a smaller bench, then the maximum becomes max(original max, smaller + m). If smaller + m > original max, then that could be higher? Possibly, if the original max is not too high and we add m to a smaller bench, the sum might exceed original max + m? That can't happen because if we add m to bench j, its final is a_j + m. Meanwhile other benches remain at a_i. So the maximum is max( max_i != j (a_i), a_j + m ). Compare to putting all m on the bench with maximum a_max: final max = a_max + m. For any other bench j, a_j + m <= a_max + m (since a_j <= a_max). Also max_i != j (a_i) <= a_max. So the overall max is max(a_max, a_j + m). But a_j + m <= a_max + m, so the max from that distribution is at most a_max + m. So the maximum possible max is indeed a_max + m, achieved by putting all new people on the bench with the current maximum.
But wait: what if there are multiple benches with same a_max? We can put all m on any of them and get a_max + m. So maximum k = max(a_i) + m.
Now minimum possible k: We want to distribute m people among benches to minimize the maximum final occupancy. This is like load balancing: we have initial loads, we want to add m units to bins to minimize the maximum final load. This is a classic problem: minimize the makespan when adding tasks to machines with initial loads. Since we can assign arbitrarily, the optimal is to try to keep the loads as balanced as possible. The minimum possible maximum (the optimal makespan) is at least the average final total per bench? But since the load can only be added in integer amounts, the lower bound is ceil((sum_{i} a_i + m)/ n). But also it must be at least max_i a_i, because even after distribution, the maximum cannot be less than the initial maximum? Actually it can be if we could remove people, but we only add, so the final max must be at least max_i a_i. So the minimum possible k is at least max( max_i a_i, ceil((total + m)/ n) ). However, we need to check if we can always achieve that bound. In load balancing with adding tasks, we can always achieve the lower bound? Not necessarily, because tasks are indivisible and we cannot reduce any bin below its initial value. But we can add to bins arbitrarily. The problem reduces to: we want to see if we can distribute m items among n bins such that each bin's final amount <= some target T, given that initial amounts are a_i. The necessary and sufficient condition is that sum_i max(0, T - a_i) >= m, i.e., the total capacity we can add without exceeding T is at least m. So the minimal possible max k is the smallest T such that sum_i max(0, T - a_i) >= m, with T being integer, and also T >= max(a_i) (since if T < some a_i, you can't reduce it, so condition fails). So we need to find the smallest integer T satisfying sum_{i=1}^n max(0, T - a_i) >= m.
That's a straightforward solution: we can binary search T or compute directly: T from max_initial upward until the sum of deficits reaches m.
Constraints are small: n <= 100, a_i <= 100, m <= 10000. So we can just loop T from max_a upwards, compute needed additional capacity and see when sum >= m.
But careful: we need the minimum possible k, which is that smallest T satisfying the condition. However, we also need to verify if there's any nuance: The new people each choose a bench and sit; they cannot be split fractionally. So indeed, condition is sum max(0, T - a_i) >= m. And if that holds, we can assign m people to benches without exceeding T. The assignment can be done greedily: fill deficits up to T.
Thus algorithm:
- Read n, m, a_i list.
- Compute max_a = max(a_i)
- Maximum possible k = max_a + m
- For minimum possible k:
Let low = max_a
For T from low upward:
total_capacity = sum(max(0, T - a_i) for i in range(n))
if total_capacity >= m:
min_k = T
break
- Output min_k and max_k.
Let's test with examples:
Example 1:
n=4,m=6,a=[1,1,1,1] max_a=1, max_k=1+6=7.
Start T=1: capacity sum(max(0,0))=0 <6
T=2: capacities: (2-1)=1 each, total=4 <6
T=3: each gets 2, total=8 >=6 -> min_k=3. matches output 3 7.
Example 2:
n=1,m=10,a=[5], max_a=5, max_k=15. T from 5: capacity=0? Actually T=5: max(0,5-5)=0 <10. T=6: capacity=1 <10; ... T=15: capacity=10 >=10 => min_k=15. matches.
Example 3:
n=3,m=6,a=[1,6,5], max_a=6, max_k=12.
Compute min: T from 6:
T=6: capacities: max(0,6-1)=5, max(0,6-6)=0, max(0,6-5)=1 => total=6 >=6, so min_k=6. Output 6 12 matches.
Example 4:
n=3,m=7,a=[1,6,5], max_a=6, max_k=13.
T=6: total capacity =5+0+1=6 <7.
T=7: capacities: 6,1,2? Actually: 7-1=6, 7-6=1, 7-5=2 => total=9 >=7, min_k=7. Output 7 13 matches.
So solution seems correct.
But we must also consider that m can be 0? The problem says m (1 ≤ m ≤ 10000), so m>=1, but maybe if m=0? Not in constraints, but even if, we could handle: min_k = max_a, max_k = max_a. But not needed.
Thus answer is straightforward.
However, note that the problem might be from Codeforces, typical problem "Park Benches" or "Benches". Indeed, it's CF 1042A? I recall something like that. The solution is exactly as described.
Edge cases: n large up to 100, a_i up to 100, m up to 10000, T may go up to max_a + m (in worst case if m is huge and n=1, but we loop from max_a upward, but T can become large if n=1? Example n=1, a1=100, m=10000 => max_k=10100, min_k would be 100 + 10000 = 10100 (since only one bench). In that case loop from 100 to 10100: that's 10000 iterations, fine. But if n=100, m=10000, a_i small, T might become around max_a + ceil(m/n) ~ maybe up to 100+100=200? Actually worst case: all a_i =1, m=10000, n=100 => total final =100+10000=10100, average=101, so T~101? Actually capacity needed: T from 1 upward. But initial max_a=1, but we need capacity sum(T-1)*100 >=10000 => 100(T-1)>=10000 => T>=101. So T=101. So T stays small. In the worst-case scenario where n=1, T goes up to max_a+m. That's 100+10000=10100. Loop 10100 times is fine (since n small, sum operations cheap). So we can just linear search.
Alternatively, we could compute min_k by: min_k = max( max_a, ceil((sum(a_i)+m)/n ) ) but that might not be sufficient? As seen in example 3, max_a=6, average (1+6+5+6)/3? Actually sum a_i=12, +6=18, average=6. ceil=6, which matches. Example 1: sum=4, +6=10, average=2.5 ceil=3, matches. Example 2: average = (5+10)/1=15, matches. Example 4: sum=12, +7=19, average=6.333 ceil=7, matches. So seems the lower bound is always achievable? Is it always possible to achieve exactly ceil((total+m)/n) provided it's >= max_a? In other words, does the condition sum max(0, T - a_i) >= m hold when T = ceil((total+m)/n) and T >= max_a? Let's verify. Let S = sum a_i, and let T0 = ceil((S+m)/n). We need to check if sum_i max(0, T0 - a_i) >= m. This is equivalent to sum_i (T0 - a_i) over those with a_i < T0? Actually sum_i max(0, T0 - a_i) = nT0 - sum_i a_i - sum_i (T0 - a_i) negative? More precisely, sum_i max(0, T0 - a_i) = sum_i (T0 - a_i) for all i with a_i < T0? Another way: nT0 - sum_{i} a_i - sum_{i: a_i > T0} (a_i - T0)? Actually nT0 - sum_i a_i = sum_i (T0 - a_i). But for bins where a_i > T0, the term (T0 - a_i) is negative, while max(0, T0 - a_i) is 0. So the sum of deficits is nT0 - sum_i a_i - sum_{i: a_i > T0} (T0 - a_i) (since those are negative, subtracting them adds back). Actually simpler: sum_i max(0, T0 - a_i) = sum_i (T0 - a_i) + sum_{i: a_i > T0} (a_i - T0) (because max(0, T0 - a_i) = (T0 - a_i) + (a_i - T0) if a_i > T0? No, that's not correct. Let's derive: For each i, if a_i <= T0, max(0, T0 - a_i) = T0 - a_i. If a_i > T0, max(0, T0 - a_i) = 0. So the sum of deficits = ∑_{i: a_i ≤ T0} (T0 - a_i). This is at most ∑i (T0 - a_i) because the latter includes negative contributions from a_i > T0, which reduce the sum. So sum deficits ≤ n*T0 - S. Equality holds only when no a_i > T0. Since T0 ≥ max_a? Wait, T0 = ceil((S+m)/n). Could T0 be less than max_a? If max_a > ceil((S+m)/n) then T0 would be less than max_a, but then the lower bound would be max_a, not T0. In the condition, we take T >= max_a. So we consider T = max(max_a, ceil((S+m)/n)). Actually the lower bound is max of those two. So if max_a is larger, then T = max_a, we need to check capacity: if T = max_a, then sum deficits = sum{i: a_i < max_a} (max_a - a_i). This might be less than m? Possibly. For example, n=3, a=[10,0,0], m=5. max_a=10, average = (10+0+0+5)/3=5, ceil=5. So lower bound would be max(10,5)=10. But can we achieve maximum final of 10? Check T=10: deficits: (10-10)=0, (10-0)=10, (10-0)=10 => total=20 >=5, so yes T=10 works. But is there any case where T = max(max_a, ceil(average)) is not sufficient? Let's think: suppose we have initial loads very high on one bench, and m small. Then the minimal max cannot be less than that high bench, and we can distribute the new people to other benches to avoid increasing max beyond max_a. That is possible if the total capacity to add to other benches up to max_a is enough to accommodate m. That capacity is sum_i (max_a - a_i) for a_i < max_a. If that capacity is >= m, then min_k = max_a. If not, then we need to increase T above max_a. So the minimal T is exactly the smallest integer >= max_a such that ∑ max(0, T - a_i) >= m. This is exactly the condition. And the bound ceil((S+m)/n) is another lower bound, but it's not always sufficient alone. So the direct search is correct.
Could we compute min_k by formula? We can compute: sort a_i. Then we can think of raising the smallest ones to some level. But binary search is fine.
Given small constraints, linear search from max_a to max_a + m (since adding all m to one bench gives T = max_a + m, but that is obviously enough) is acceptable. But to be safe, we could compute upper bound as max_a + m and loop.
Complexities: O((max_a+m)n) worst-case 10100100 = 1e6, fine.
Alternatively, binary search from low to high: low = max_a, high = max_a + m. Each step compute sum. O(log m * n) = even better. But not needed.
Thus solution: read input, compute max_a, compute max_k, then compute min_k by loop.
Implementation details:
We'll read n, m, then list a. For min_k, we can start T = max_a, while True: compute total = 0; for each a_i: if T > a_i: total += T - a_i; if total >= m: break; else: T += 1.
Edge: if m == 0 (not given) break immediately with T = max_a.
Print min_k max_k.
Now, let's double-check: in examples, min_k matches.
Potential pitfalls: reading input lines, need to handle n lines each with integer.
Also note: The input format: first line n, second line m, next n lines each a_i. That's what the problem says.
But sometimes Codeforces problems have n and m on same line? However, given the description, it's separate lines.
We'll implement accordingly.
We'll produce final answer.
But the user asked: "There are n benches ... Calculate the minimum possible k and the maximum possible k." So we output two numbers.
Thus final solution code in Python (or C++). Since this is a ChatGPT answer, we can provide explanation and code.
Now, I will produce final answer. The minimum possible maximum occupancy (k) is found by distributing the (m) new people to balance the benches as much as possible. The smallest integer (k) such that the total “free space” up to (k) on all benches is at least (m) gives the answer. The maximum possible (k) is simply obtained by placing all new people on the bench that already has the most people.
Algorithm
- Read (n), (m), and the list (a) of initial occupancies.
- Compute (max_a = \max(a)).
- Maximum possible (k = max_a + m).
- Minimum possible (k):
- Start with (k = max_a).
- While (\sum_{i=1}^{n} \max(0, k - a_i) < m): increase (k) by 1.
- The first (k) satisfying the inequality is the answer.
- Output the two values.
Complexity: at most (max_a + m) iterations (≤ 10100), each doing (O(n)) work → easily fits the constraints.
def solve():
import sys
data = sys.stdin.read().strip().split()
if not data:
return
it = iter(data)
n = int(next(it))
m = int(next(it))
a = [int(next(it)) for _ in range(n)]
max_a = max(a)
max_k = max_a + m
k = max_a
while True:
need = 0
for x in a:
if k > x:
need += k - x
if need >= m:
break
k += 1
print(k, max_k)
if __name__ == "__main__":
solve()