PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#PS0000264. Số lần lật K bit liên tiếp ít nhất (Minimum Number of K Consecutive Bit Flips)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Số lần lật K bit liên tiếp ít nhất (Minimum Number of K Consecutive Bit Flips)

Minimum Number of K Consecutive Bit Flips

Source: LeetCode

Version: Phuoc Hung OJ Extended

Problem Statement

Given NN binary values and an integer KK, one operation toggles all bits of a contiguous block of exactly KK elements. Find the minimum number of operations to obtain all ones, or −1-1 if impossible.

Input

First line: N,KN,K. Second line: exactly NN integers aia_i equal to 0 or 1.

Output

Print the minimum operation count, or -1.

Subtasks

  • Subtask 1 (20 points): 1≤N≤101\le N\le 10; 1≤K≤N1\le K\le N; ai∈{0,1}a_i\in\{0,1\}.
  • Subtask 2 (30 points): 1≤N≤1000001\le N\le 100000; 1≤K≤N1\le K\le N; ai∈{0,1}a_i\in\{0,1\}; K=1K=1.
  • Subtask 3 (50 points): 1≤N≤1000001\le N\le 100000; 1≤K≤N1\le K\le N; ai∈{0,1}a_i\in\{0,1\}.

Example 1

Input

3 1
0 1 0

Output

2

Explanation

Flip the first and third bits separately.

Example 2

Input

3 2
1 1 0

Output

-1

Explanation

The first two bits force no flips starting there; the final zero cannot start a length-two flip.

Example 3

Input

8 3
0 0 0 1 0 1 1 0

Output

3

Explanation

Flip zero-based blocks 0..2, 4..6, 5..7; successive arrays are 11110110, 11111000, 11111111.