Day 4: Cabin Compression 🍫

hardsliding windowPrefix Sum

A train has nn cabins arranged in a line. Some cabins are occupied by passengers, and some are empty. You are given an array cabinscabins of length nn where:

  • cabinsi=1cabins_i = 1 if cabin ii has a passenger,
  • cabinsi=0cabins_i = 0 if it is empty.

Passengers can switch places with passengers in adjacent cabins in one move.

For example, in [1,0,0,1,0,1][1, 0, 0, 1, 0, 1] with k=2k = 2, moving the 4th passenger one step to the right gives two consecutive passengers in 1 move.

Your task is to compute the minimum number of moves required so that kk passengers end up sitting in kk consecutive cabins.

Input Format:

The first line contains two integers nn and kk (1kn1051 \leq k \leq n \leq 10^5) — the number of cabins and the number of passengers to be grouped together.

The second line contains nn integers cabins1,cabins2,,cabinsncabins_1, cabins_2, \dots, cabins_n (cabinsi{0,1}cabins_i \in \lbrace 0, 1 \rbrace) — the occupancy state of each cabin.

It is guaranteed that the array contains at least kk passengers in total.

Output Format:

Print the minimum number of moves required so that there exists a group of kk passengers sitting together in kk consecutive cabins.

Examples:

Example 1:

Input:

6 2
1 0 0 1 0 1

Output:

1

Example 2:

Input:

7 3
1 1 0 0 0 0 1

Output:

4

Code

Loading Editor...