#K72747. Optimal Page Reading

    ID: 33822 Type: Default 1000ms 256MiB

Optimal Page Reading

Optimal Page Reading

You are given a collection of books, each with a specific number of pages, and a maximum page limit (P) for each participant. Your task is to select a subset of these books so that the total number of pages read is maximized without exceeding (P). This problem is a classic 0/1 knapsack variation where each book can be chosen at most once.

For each test case, the first line of input contains two integers (N) and (P): (N) denotes the number of books available and (P) is the maximum total pages that can be read. The next line contains (N) space-separated integers representing the page counts of the books. Your goal is to determine the maximum sum of pages (not exceeding (P)) that can be accumulated by choosing an appropriate subset of books.

inputFormat

The input starts with an integer (T) representing the number of test cases. For each test case, there are two lines:

  1. The first line contains two space-separated integers (N) and (P), where (N) is the number of books and (P) is the page limit.
  2. The second line contains (N) space-separated integers denoting the number of pages in each book. If (N = 0), the second line will be empty.

outputFormat

For each test case, output a single line containing one integer: the maximum total number of pages that can be read without exceeding the page limit (P).## sample

3
3 50
10 20 30
4 100
40 15 25 20
5 60
10 10 10 10 10
50

100 50

</p>