#K43842. Maximizing Books on a Shelf
Maximizing Books on a Shelf
Maximizing Books on a Shelf
You are given n books and a bookshelf of width W. Each book has a specified width. Your task is to select a subset of books such that the total width does not exceed W while maximizing the number of books placed. If multiple selections result in the maximum number of books, choose the one that minimizes the unused width.
Formally, let the widths of the books be \(a_1,a_2,\dots,a_n\). You need to choose a subset \(S\subseteq \{1,2,\dots,n\}\) such that:
[ \sum_{i\in S} a_i \leq W, ]
and the number of books \(|S|\) is maximized. If several subsets yield the same \(|S|\), choose the one that minimizes \(W - \sum_{i\in S} a_i\).
You are to output two integers: the maximum number of books that can be placed and the corresponding minimum unused width.
inputFormat
The input is read from standard input and consists of two lines.
The first line contains two integers \(n\) and \(W\) where \(n\) is the number of books and \(W\) is the width of the bookshelf.
The second line contains \(n\) space-separated integers representing the widths of the books.
outputFormat
Output two space-separated integers on a single line: the maximum number of books that can be placed on the shelf and the minimum unused width after placing those books.
## sample5 10
2 3 5 6 1
3 0
</p>