Skip to content

Instantly share code, notes, and snippets.

@Gloveman
Last active July 11, 2025 08:07
Show Gist options
  • Save Gloveman/1900f2e39d3d4fc1502a2be86f3e3519 to your computer and use it in GitHub Desktop.
Save Gloveman/1900f2e39d3d4fc1502a2be86f3e3519 to your computer and use it in GitHub Desktop.
Basic binary search #python #algorithm
import sys
input=sys.stdin.readline
n,m = map(int,input().split())
trees=list(map(int,input().split()))
start, end = 0, max(trees)
result = 0
while start <= end:
mid = (start + end) // 2
cur_m = sum(x - mid for x in trees if x > mid)
if cur_m >= m:
result = mid
start = mid + 1 #'조건을 만족한 경우에만 값을 별도 저장'
else:
end = mid - 1
print(result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment