def max_departments(d_list, budget):
    answer = 0
    d_list.sort()

    for i in range(len(d_list)):
        if budget < d_list[i]:
            break
        budget -= d_list[i]
        answer += 1
    return answer

d_list = [1, 3, 2, 5, 4]
budget=9 
ret = max_departments(d_list, budget)
print(ret)

Embed on website

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