#K96237. Maximum Shade
Maximum Shade
Maximum Shade
Given an array of integers representing the shade produced by each tree, determine the maximum sum of any ( k ) consecutive trees. The sum is calculated using a sliding window technique.
Mathematically, the problem is formulated as follows:
[
\text{maxShade}(A,k)=\begin{cases}
\max_{1 \leq i \leq n-k+1} \sum_{j=i}^{i+k-1}A_j, & \text{if } k \leq n \
0, & \text{if } k > n
\end{cases}
]
If the array is empty or the number of trees is less than ( k ), the output should be 0.
inputFormat
The input is given via standard input (stdin).
The first line contains two integers ( n ) and ( k ), where ( n ) is the number of trees (the length of the array) and ( k ) is the number of consecutive trees to consider.
The second line contains ( n ) space-separated integers representing the shade of each tree. If ( n = 0 ), the second line will be empty.
outputFormat
Output a single integer to standard output (stdout): the maximum sum of the shades from any ( k ) consecutive trees. If there is no valid segment (i.e., if ( k > n ) or the array is empty), output 0.## sample
6 3
2 1 5 1 3 2
9