trees = [20, 15, 10, 17]
M = 7

left, right = 0, max(trees)
answer = 0

while left <= right:
    mid = (left + right) // 2

    total = 0
    for tree in trees:
        if tree > mid:
            total += tree - mid

    if total >= M:
        answer = mid
        left = mid + 1
    else:
        right = mid - 1

print(answer)

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: