A team has a fixed amount of focused time B this sprint and a list of n candidate tasks. Task i costs cost[i] units of time (a positive integer). The team may complete any subset of tasks whose total time cost is at most B. The goal is to finish as MANY tasks as possible (their individual value is irrelevant — only the count matters).
Print the maximum number of tasks that can be completed within the budget.
Line 1: two integers n and B.
Line 2: n space-separated positive integers, the task time costs.
A single integer: the maximum number of tasks that fit within total time B.
Example 1
Input
4 7 3 1 4 2
Expected
3
Explanation
Sorted costs 1,2,3,4. Take 1 (sum 1), 2 (sum 3), 3 (sum 6); adding 4 would reach 10 > 7. Three tasks fit.
Example 2
Input
3 2 5 5 5
Expected
0
Explanation
Every task costs 5, which already exceeds the budget of 2, so zero tasks fit.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →