#K10456. Maximal Elements Selection with Sum Constraint
Maximal Elements Selection with Sum Constraint
Maximal Elements Selection with Sum Constraint
You are given an array of N positive integers and an integer K. Your task is to choose the maximum number of elements from the array such that their total sum is less than or equal to K. In mathematical notation, find the largest possible m such that there exists a subset of indices \(\{i_1, i_2, \dots, i_m\}\) satisfying
\[\sum_{j=1}^{m} a_{i_j} \le K\]
The optimal strategy is to select the smallest elements first. This problem needs to be solved for multiple test cases.
inputFormat
The input is given via standard input (stdin) and has the following format:
- The first line contains a single integer T, the number of test cases.
- For each test case, the first line contains two integers N and K: the number of elements and the maximum allowed sum, respectively.
- The second line of each test case contains N space-separated integers representing the array elements.
outputFormat
For each test case, output a single line to standard output (stdout) containing one integer — the maximum number of elements that can be selected such that their sum is less than or equal to K.
## sample2
5 9
1 2 3 4 5
4 7
2 2 2 2
3
3
</p>