#K43977. Maximum Consecutive Fence Panels
Maximum Consecutive Fence Panels
Maximum Consecutive Fence Panels
You are given a fence consisting of n panels, each with a certain height. Painting a panel costs an amount equal to its height. Given a budget m, your task is to determine the maximum number of consecutive panels you can paint such that the sum of their heights does not exceed m.
Formally, if the heights of the panels are given by h1, h2, ..., hn, find the largest integer k so that there exists an index i satisfying:
$$\sum_{j=i}^{i+k-1} h_j \le m$$
Note: The panels must be consecutive. Use efficient methods as n can be large.
inputFormat
The input is provided via standard input (stdin) and consists of two lines:
- The first line contains two integers n and m separated by a space, where n is the number of fence panels and m is the maximum allowed sum of heights.
- The second line contains n space-separated integers representing the heights of the panels.
For example: 6 5
in the first line and 2 1 1 2 1 1
in the second line.
outputFormat
Output a single integer to standard output (stdout) representing the maximum number of consecutive panels that can be painted without the total cost exceeding m.
## sample6 5
2 1 1 2 1 1
4
</p>