tree = [20, 15, 10, 17]
K = 7

left = 0
right = max(tree)
answer = 0

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

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

    if total >= K:
        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: