#C11451. Maximum Books on Shelf

    ID: 40769 Type: Default 1000ms 256MiB

Maximum Books on Shelf

Maximum Books on Shelf

You are given a shelf of length \(L\) and \(N\) books with individual widths. The task is to determine the maximum number of books that can be placed on the shelf such that the sum of their widths does not exceed \(L\). You may choose the books in any order, and the goal is to maximize the count.

The strategy is to sort the list of book widths in ascending order and then add them one by one until the next book would exceed the shelf length \(L\). Formally, if the sorted widths are \(w_1, w_2, \dots, w_N\), you need to find the maximum \(k\) such that \(\sum_{i=1}^{k} w_i \le L\).

inputFormat

The first line of input contains two integers \(L\) and \(N\) representing the length of the shelf and the number of books, respectively.

The second line contains \(N\) space-separated integers, each representing the width of a book.

outputFormat

Output a single integer that is the maximum number of books that can fit on the shelf.

## sample
10 4
4 3 5 7
2

</p>