#K7881. Max Books on Shelves
Max Books on Shelves
Max Books on Shelves
You are given T test cases. In each test case, there are N books and M shelves. Each book has a thickness, and each shelf has a capacity. Your task is to determine the maximum number of books that can be placed on the shelves such that the sum of the thicknesses of the books on any shelf does not exceed its capacity.
Formally, if a shelf holds books with thicknesses \( t_1, t_2, \dots, t_k \), then they must satisfy \( \sum_{i=1}^{k} t_i \leq capacity \). Once a book is placed on a shelf, it cannot be moved or used again.
Use a greedy strategy to maximize the number of books placed across all shelves.
inputFormat
The input is read from standard input (stdin) and has the following format:
- The first line contains an integer T, the number of test cases.
- For each test case, the first line contains two integers N and M separated by a space, representing the number of books and the number of shelves respectively.
- The second line contains N space-separated integers, each representing the thickness of a book.
- The third line contains M space-separated integers, each representing the capacity of a shelf.
outputFormat
For each test case, output a single integer on a new line indicating the maximum number of books that can be placed on the shelves without exceeding the respective capacities.
## sample5
5 3
2 3 4 5 6
10 10 10
4 2
1 2 2 3
3 5
3 1
2 4 6
100
1 1
2
5
3 1
4 5 6
1
5
4
3
1
0
</p>