#K87977. Bookshelf Book Arrangement

    ID: 37206 Type: Default 1000ms 256MiB

Bookshelf Book Arrangement

Bookshelf Book Arrangement

You are given a number of books and their corresponding thicknesses. Your task is to determine the maximum number of books that can be arranged on a shelf without exceeding the given maximum thickness.

To achieve this, you should sort the book thicknesses in ascending order, and then add the books one by one until adding another book would exceed the maximum shelf thickness.

Mathematical Formulation: Let \(a_1, a_2, \dots, a_n\) be the sorted thicknesses of the books, and let \(T\) be the maximum allowed thickness. Find the largest integer \(k\) such that

\[ a_1 + a_2 + \dots + a_k \leq T \]

Output the value of \(k\).

inputFormat

The input is given via standard input (stdin) and consists of three parts:

  1. An integer \(n\) representing the number of books.
  2. A line with \(n\) space-separated integers representing the thicknesses of each book.
  3. An integer representing the maximum total thickness allowed on the shelf.

For example:

5
10 20 30 40 50
100

outputFormat

Output a single integer on standard output (stdout) representing the maximum number of books that can be placed on the shelf without exceeding the thickness limit.

For the above sample input, the output should be:

4
## sample
5
10 20 30 40 50
100
4