Building Blocks
Time limit1sMemory limit128 MB
Find the minimum number of add/remove block moves so that some k consecutive columns end up with equal height.
- Level
Medium5 of 10
- Topics
- Sliding window, Prefix sum, Sorting, Math
- Solved
- No attempts yet
Problem
Byteasar has arranged blocks into columns. The -th column has height , meaning it is made of blocks.
He picks an integer and wants to make some consecutive columns all have the same height. He may only use the following two kinds of moves:
- place one block on top of any column (there is always an unlimited supply of spare blocks), or
- remove the topmost block from any column.
Each move adds or removes exactly one block and counts as one move. Find the minimum number of moves needed so that some consecutive columns all have equal height.
Input
The first line contains two integers and separated by a single space ().
Each of the next lines contains one integer: the -th of them is , the height of the -th column ().
Output
Print a single integer: the minimum number of moves needed so that some consecutive columns all have equal height.