#K60747. Stacking Books Challenge

    ID: 31155 Type: Default 1000ms 256MiB

Stacking Books Challenge

Stacking Books Challenge

You are given N books with varying thicknesses. Your goal is to stack as many books as possible on a shelf without exceeding the total allowed width W. The books can be placed either horizontally or vertically. When placed horizontally, each book occupies its full thickness. If placing a book horizontally would exceed the limit, you may opt to place it vertically, which always occupies exactly 1 unit of width, regardless of its thickness.

Given the list of book thicknesses, determine the maximum number of books you can stack under the given width constraint. To achieve an optimal solution, you should consider stacking the thinner books first.

The width used accumulates as follows:

$$\text{If } current + t_i \leq W \text{ then add } t_i \text{, else if } current + 1 \leq W \text{ then add } 1. $$

inputFormat

The first line contains a single integer T representing the number of test cases.

Each test case is described by the following:

  • The first line contains two integers N and W, where N is the number of books and W is the maximum allowed width.
  • The second line contains N space-separated integers, each representing the thickness of a book.

You need to process all test cases.

outputFormat

For each test case, output a single integer on a new line, representing the maximum number of books that can be stacked without exceeding the width W.

## sample
5
5 10
2 3 4 5 6
3 3
2 2 2
4 5
1 1 1 1
4 3
3 3 3 3
6 15
1 2 3 4 5 6
4

2 4 1 5

</p>