#C8547. Maximum Books on a Shelf
Maximum Books on a Shelf
Maximum Books on a Shelf
You are given a shelf with a maximum capacity C and N books, each having a specific thickness. The task is to determine the maximum number of books that can be placed on the shelf in such a way that the total thickness does not exceed the capacity.
You can formalize the capacity constraint as:
\( \sum_{i=1}^{k} t_i \le C \)
where \( t_i \) denotes the thickness of the \( i^{th} \) book, and \( k \) is the number of books selected. To maximize the count, one should try to fit the thinner books first, which motivates sorting them in non-decreasing order.
inputFormat
The input is given in two lines:
- The first line contains two integers \(N\) and \(C\), where \(N\) is the number of books, and \(C\) is the maximum capacity of the shelf.
- The second line contains \(N\) space-separated integers representing the thicknesses of the books.
outputFormat
Output a single integer representing the maximum number of books that can be placed on the shelf without exceeding the capacity.
## sample5 10
1 2 3 4 5
4