#K82182. Maximum Number of Filled Buildings

    ID: 35919 Type: Default 1000ms 256MiB

Maximum Number of Filled Buildings

Maximum Number of Filled Buildings

You are given the heights of n buildings and an integer representing the total water units available. Each building requires an amount of water equal to its height to be completely filled.

Your task is to determine the maximum number of buildings that can be completely filled if you allocate water to the buildings with the smallest heights first. This greedy strategy maximizes the number of buildings filled.

For instance, if the building heights are \(2, 1, 1, 3, 4\) and the available water units is 4, then the answer is 3 because the buildings with heights 1, 1, and 2 can all be filled.

inputFormat

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

  1. The first line contains an integer \(n\), the number of buildings.
  2. The second line contains \(n\) space-separated integers representing the heights of the buildings. (If \(n = 0\), this line will be empty.)
  3. The third line contains an integer representing the total water units available.

outputFormat

Output a single integer to standard output (stdout) — the maximum number of buildings that can be completely filled with water.

## sample
5
2 1 1 3 4
4
3