#C6923. Maximum Puzzles Solved Under Difficulty Constraint
Maximum Puzzles Solved Under Difficulty Constraint
Maximum Puzzles Solved Under Difficulty Constraint
You are given a sequence of puzzle difficulties and a maximum total difficulty limit. Starting from the first puzzle, you need to determine how many puzzles can be solved sequentially without the cumulative difficulty exceeding the given maximum limit.
Formally, given an array \(a_1, a_2, \ldots, a_n\) representing the difficulties of puzzles in order and an integer \(M\) representing the maximum total difficulty allowed, find the largest integer \(k\) such that \(\sum_{i=1}^{k} a_i \le M\). If no puzzle can be solved without exceeding the limit, the answer is 0.
Note: You must process the puzzles in the given order. The problem can be solved with a simple greedy approach by accumulating difficulties until the sum exceeds \(M\).
inputFormat
The input is given via standard input (stdin) and consists of two lines:
- The first line contains two integers \(n\) and \(M\) (the number of puzzles and the maximum total difficulty), separated by a space.
- The second line contains \(n\) integers representing the difficulty levels of each puzzle in order.
If \(n = 0\), the second line may be empty.
outputFormat
Output a single integer representing the maximum number of puzzles that can be solved sequentially without exceeding the total difficulty \(M\). The output should be written to standard output (stdout).
## sample5 10
1 2 3 4 5
4
</p>