#K43062. Maximum Team Formation under Skill Cap
Maximum Team Formation under Skill Cap
Maximum Team Formation under Skill Cap
You are given N participants with their respective skill levels. Your task is to form a team by selecting a subset of participants such that the total sum of their skill levels does not exceed a given cap C. In order to maximize the number of participants in the team, you should choose the participants with the lowest skills first. The problem can be formalized as follows: find the maximum integer x such that
$$\sum_{i=1}^{x} a_i \le C$$
where the sequence \(a_1, a_2, \dots, a_N\) is the sorted list of skill levels. There are Q queries, each providing a different cap C, and for each query you need to determine the maximum number of participants that can be included in the team.
inputFormat
The first line contains an integer T representing the number of test cases. For each test case, the first line contains two space-separated integers N and Q — the number of participants and the number of queries respectively. The second line contains N space-separated integers representing the skill levels of the participants. The third line contains Q space-separated integers where each integer is a query cap C.
Input Format:
T N Q skill_1 skill_2 ... skill_N C1 C2 ... CQ
outputFormat
For each test case, output a single line containing Q space-separated integers. Each integer is the maximum number of participants that can form a valid team under the corresponding cap C.
Output Format:
result1 result2 ... resultQ## sample
4
5 3
3 1 4 2 5
6 7 15
4 2
2 3 1 4
5 10
1 1
1
1
3 2
5 6 7
1 2
3 3 5
2 4
1
0 0
</p>