Last active
July 11, 2025 08:07
-
-
Save Gloveman/1900f2e39d3d4fc1502a2be86f3e3519 to your computer and use it in GitHub Desktop.
Basic binary search #python #algorithm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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