#K34632. Maximum Continuous Fence Length
Maximum Continuous Fence Length
Maximum Continuous Fence Length
You are given n blocks with specified lengths. Your task is to determine the maximum possible length of a continuous fence that can be built by using exactly m consecutive blocks.
This is a classic sliding window problem: if the blocks are represented as an array a of length n, you need to find the maximum sum of any contiguous subarray of length m. The sum for a subarray starting at index i is given by:
Your goal is to output the maximum value of S(i) for all valid i (i.e. where the subarray lies completely within the array). Make sure to use exactly m blocks!
inputFormat
The input is provided via stdin in the following format:
- The first line contains two integers n and m, where n is the number of blocks and m is the number of consecutive blocks to be used.
- The second line contains n space-separated integers representing the lengths of the blocks.
outputFormat
Output a single integer via stdout, which is the maximum total length of the continuous fence built using exactly m consecutive blocks.
## sample5 3
1 2 3 4 5
12