#C1407. Maximum Books on Shelf
Maximum Books on Shelf
Maximum Books on Shelf
You are given a shelf with a maximum thickness capacity \(m\) (in millimeters) and \(n\) books, each with a specified thickness. Your task is to determine the maximum number of books that can be arranged on the shelf such that the sum of their thicknesses does not exceed the capacity \(m\).
You may choose the books in any order, but a greedy strategy works best: by selecting the thinner books first, you can maximize the count of books placed on the shelf. Formally, given \(n\) books with thicknesses \(t_1, t_2, \ldots, t_n\), find the largest integer \(k\) such that there exists a subset of \(k\) books satisfying:
\[ \sum_{i=1}^{k} t_i \le m \]
Input is provided via standard input and output should be printed to standard output.
inputFormat
The first line contains two integers (n) and (m) separated by a space, where (n) is the number of books and (m) is the maximum total thickness the shelf can hold. The second line contains (n) integers representing the thickness of each book.
outputFormat
Output a single integer representing the maximum number of books that can be placed on the shelf without exceeding the total thickness (m).## sample
5 10
1 2 3 4 5
4