#C6661. Organize Books on Shelves

    ID: 50446 Type: Default 1000ms 256MiB

Organize Books on Shelves

Organize Books on Shelves

Sarah is an avid reader who recently purchased several books. She wants to arrange her new collection on a few shelves. Each shelf has a maximum capacity, meaning it can hold only a limited number of books. Although each book also has a height, the placement is not based on the actual book heights, but rather on the capacity of each shelf.

You are given two groups of numbers. The first group represents n books along with their heights. The second group provides m shelves and their respective capacities. A shelf with capacity c can hold at most c books. The goal is to determine the maximum number of books that can be organized on the shelves such that no shelf exceeds its capacity. In mathematical terms, if you denote the capacities of the shelves by \(c_1, c_2, \dots, c_m\), the maximum number of books you can place is:

[ \text{result} = \min\left(n, \sum_{i=1}^{m} c_i\right). ]

Note that even though every book has an associated height, the only important property in this problem is the total number of books available (n), and the capacities of the shelves.

inputFormat

The input is given via standard input (stdin) and is structured as follows:

  • The first line contains a single integer n representing the number of books.
  • The second line contains n space-separated integers denoting the heights of the books.
  • The third line contains a single integer m representing the number of shelves.
  • The fourth line contains m space-separated integers representing the capacities of each shelf.

outputFormat

The output is a single integer printed to standard output (stdout) representing the maximum number of books that can be placed on the shelves without violating any shelf's capacity.

In formula, the answer is \(\min(n, \sum_{i=1}^{m} c_i)\).

## sample
5
4 5 3 2 8
2
3 2
5