Under construction

Still being built. Things may change

Building Blocks

Time limit1sMemory limit128 MB

Summary
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 nn columns. The ii-th column has height hih_i, meaning it is made of hih_i blocks.

He picks an integer kk and wants to make some kk 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 kk consecutive columns all have equal height.

Input

The first line contains two integers nn and kk separated by a single space (1≤k≤n≤100 0001 \le k \le n \le 100\,000).

Each of the next nn lines contains one integer: the ii-th of them is hih_i, the height of the ii-th column (0≤hi≤1 000 0000 \le h_i \le 1\,000\,000).

Output

Print a single integer: the minimum number of moves needed so that some kk consecutive columns all have equal height.

Examples1

  1. Example 1

    Input
    5 3
    3
    9
    2
    3
    1
    
    Expected output
    2