#K13221. Max Fields Irrigated
Max Fields Irrigated
Max Fields Irrigated
You are given a reservoir with a total water capacity \( W \) and \( F \) fields, each requiring a certain amount of water \( w_i \) to be fully irrigated. Your task is to determine the maximum number of fields that can be completely irrigated without exceeding the available water.
The optimal strategy is to irrigate the fields that require the least water first. In other words, if you sort the water requirements in non-decreasing order \( (w_1 \leq w_2 \leq \cdots \leq w_F) \), you should then choose fields sequentially until the cumulative sum exceeds \( W \). Formally, you need to find the maximum integer \( k \) such that \( \sum_{i=1}^{k} w_i \leq W \).
inputFormat
The input is given via standard input (stdin) and consists of multiple test cases. Each test case is described as follows:
- The first line contains two integers \( W \) (the reservoir's water capacity, \(1 \leq W \leq 1000\)) and \( F \) (the number of fields, \(1 \leq F \leq 100\)).
- The second line contains \( F \) integers \( w_1, w_2, \dots, w_F \) (each \(1 \leq w_i \leq 100\)), representing the water required to irrigate each field.
The input terminates with a test case where \( W = 0 \) and \( F = 0 \) (this test case should not be processed).
outputFormat
For each test case, output a single integer on a new line to standard output (stdout), representing the maximum number of fields that can be fully irrigated.
## sample100 5
30 20 50 10 40
0 0
4
</p>