#K62852. Maximum Lego Tower Height
Maximum Lego Tower Height
Maximum Lego Tower Height
In this problem, you are given a sequence of lego block heights and an integer (k) representing the maximum number of consecutive blocks that can be used to build a tower. Your task is to determine the maximum height obtainable by selecting a contiguous segment of blocks, subject to the constraint that at most (k) blocks can be used. Formally, given a sequence (H = [h_1, h_2, \dots, h_n]) and an integer (k), find the maximum sum (S) such that (S = \sum_{i=j}^{j+\ell-1} h_i) for some (j) and (1 \leq \ell \leq \min(k, n-j+1)).
Note: If there are no blocks or if (k = 0), the maximum height is defined to be 0.
inputFormat
The input is given via standard input (stdin) and has the following format:
- The first line contains an integer (n) which is the number of lego blocks.
- If (n > 0), the second line contains (n) space-separated integers representing the heights of the blocks.
- The last line contains an integer (k), the maximum number of consecutive blocks that can be used.
Note: If (n = 0), the block height list is omitted.
outputFormat
Output a single integer representing the maximum height of the lego tower that can be built under the given constraints. The answer should be printed to standard output (stdout).## sample
5
1 3 2 4 5
3
11