Split a token budget fairly across subagents
Your task
An orchestrator agent fans a task out to several subagents, and each one asks for a number of tokens to do its part. When the asks add up to more than the budget, the orchestrator has to split it so that no subagent is starved to feed a greedy one.
Write split_budget(budget, asks) that returns how many tokens each subagent gets.
budget: a whole number, 0 or more.asks: a list of whole numbers, 0 or more, one per subagent.- Return a list of whole numbers, one per subagent, in the same order.
Rules:
- No subagent gets more than it asked for.
- Hand out as many tokens as possible: the shares add up to
budgetor to the total ofasks, whichever is smaller. - Among the splits that keep rules 1 and 2, the smallest share is as large as possible. Among those, the second smallest share is as large as possible, and so on up to the largest.
- If rules 1 to 3 still allow several splits, favor the subagents earlier in the list. Compare the splits share by share from the start: the first larger share wins.
Examples
split_budget(budget: int, asks: list[int]) -> list[int]Example 1
Input
split_budget(100, [30, 20, 10])Output
[30, 20, 10]Example 2
Input
split_budget(100, [10, 80, 60])Output
[10, 45, 45]Example 3
Input
split_budget(10, [5, 5, 5])Output
[4, 3, 3]