문제 주소: https://www.acmicpc.net/problem/2805
난이도: silver 3
while low <= high:
total = 0
mid = (low + high) // 2
for tree in trees:
if tree > mid:
total += tree-mid
if total < M:
high = mid - 1
else:
ans = mid #밑의 과정을 통해 while문이 끝날수도 있기때문에 업데이트 해준다.
low = mid + 1
N, M = map(int, input().split())
trees = list(map(int, input().split()))
low = 0
high = max(trees)
ans = 0
while low <= high:
total = 0
mid = (low + high) // 2
for tree in trees:
if tree > mid:
total += tree-mid
if total < M:
high = mid - 1
else:
ans = mid
low = mid + 1
print(ans)