#C11019. Maximum Items Purchase
Maximum Items Purchase
Maximum Items Purchase
You are given a list of item prices and a budget. Your task is to determine the maximum number of items you can purchase without exceeding the given budget. You should buy items in increasing order of price to maximize the quantity.
Note: The answer is computed by sorting the items by price and then summing them until the budget is exceeded.
The problem can be formally described as follows:
Given a list \( \textbf{prices} = [p_1, p_2, \dots, p_n] \) and an integer \( B \) representing the budget, find the maximum number \( k \) such that \( \sum_{i=1}^{k} p_{(i)} \leq B \) where \( p_{(i)} \) are the prices sorted in non-decreasing order.
inputFormat
The first line of input contains an integer \( T \) (the number of test cases). Each test case consists of two lines:
- The first line contains two integers: \( n \) (the number of items) and \( B \) (the available budget).
- The second line contains \( n \) space-separated integers representing the prices of the items.
All input is read from stdin
.
outputFormat
For each test case, output a single line containing an integer representing the maximum number of items that can be purchased without exceeding the budget. The output should be written to stdout
.
5
7 50
1 12 5 111 200 1000 10
5 35
20 10 5 30 100
5 0
1 2 3 4 5
5 25
5 5 5 5 5
5 45
50 60 70 80 90
4
3
0
5
0
</p>