cswork / OAOpen practice workspace

AmazonOA #296

Minimum Cost to Convert Products to Variant A

Estimated HardJudge-ready

Topics (AI-labelled)GreedyTwo Pointers

products is a 0/1 array. Each time, you must choose exactly k consecutive positions, pay the current number of 1s in that window, and then change one 1 in the window to 0. Find the minimum total cost to clear all 1s; if everything is already 0, no operation is needed.

Input

The first line contains n k and the second line contains products. The original statement specifies binary values and 1≤k≤n, with no upper bound on n; this site adds 1≤n≤200000.

Output

Output a single integer, the answer.

Example 1

Input:

3 3
1 1 1

Output:

6

Example 2

Input:

5 2
1 0 1 0 1

Output:

3

Example 3

Input:

2 1
0 0

Output:

0

Example notes

The answers are 6, 3 and 0. In the first example you must choose all three positions every time, paying 3, 2 and 1 in turn.

Editorial

Loading solution…