#P3503. Maximizing Consecutive Piles
Maximizing Consecutive Piles
Maximizing Consecutive Piles
Bytie received a set of identical wooden blocks and arranged them in n piles in a row. His father then challenged him with the following puzzle:
Given an integer threshold h, rearrange the blocks using only the following move so that the number of consecutive piles having at least h blocks is maximized.
The allowed move is: from any pile i with number of blocks a[i]
satisfying
$$a_i > h,$$
remove the top block (thus setting a[i] := a[i] - 1
) and place it atop one of the piles immediately adjacent to it (either i-1 or i+1, if such a pile exists) so that a[j] := a[j] + 1
.
Restrictions:
- You are not allowed to form any new piles; blocks may only be moved among the existing ones.
- You can perform as many moves as you wish, in any order.
Your task is to compute the maximum length of a contiguous segment of piles that can be made to have at least h blocks each after some sequence of allowed moves.
Note: Since moves are allowed only from piles with more than h blocks, once a pile reaches exactly h it cannot donate any block further. For simplicity, assume that the number of piles is small.
inputFormat
The input consists of two lines:
- The first line contains two integers
n
andh
where1 ≤ n ≤ 10
. Here,n
is the number of piles andh
is the threshold number of blocks. - The second line contains
n
integersa[1], a[2], ..., a[n]
(each at most 10) representing the initial numbers of blocks in the piles.
outputFormat
Output a single integer — the maximum possible length of a contiguous segment of piles that can be made to have at least h blocks each after performing any sequence of allowed moves.
sample
5 3
2 4 1 3 3
2