CF BUDDY
← Problems·

1216F · Wi-Fi

2100 · data structures, dp, greedy

Problem: You work as a system administrator in a dormitory, which has nn rooms one after another along a straight hallway. Rooms are numbered from 11 to nn.

You have to connect all nn rooms to the Internet.

You can connect each room to the Internet directly, the cost of such connection for the ii-th room is ii coins.

Some rooms also have a spot for a router. The cost of placing a router in the ii-th room is also ii coins. You cannot place a router in a room which does not have a spot for it. When you place a router in the room ii, you connect all rooms with the numbers from max(1, ik)max(1,~i - k) to min(n, i+k)min(n,~i + k) inclusive to the Internet, where kk is the range of router. The value of kk is the same for all routers.

Calculate the minimum total cost of connecting all nn rooms to the Internet. You can assume that the number of rooms which have a spot for a router is not greater than the number of routers you have.

Input Format: The first line of the input contains two integers nn and kk (1n,k21051 \le n, k \le 2 \cdot 10^5) — the number of rooms and the range of each router.

The second line of the input contains one string ss of length nn, consisting only of zeros and ones. If the ii-th character of the string equals to '1' then there is a spot for a router in the ii-th room. If the ii-th character of the string equals to '0' then you cannot place a router in the ii-th room.

Output Format: Print one integer — the minimum total cost of connecting all nn rooms to the Internet.

Note: In the first example it is enough to place the router in the room 33, then all rooms will be connected to the Internet. The total cost of connection is 33.

In the second example you can place routers nowhere, so you need to connect all rooms directly. Thus, the total cost of connection of all rooms is 1+2+3+4+5+6=211 + 2 + 3 + 4 + 5 + 6 = 21.

In the third example you need to connect the room 11 directly and place the router in the room 33. Thus, the total cost of connection of all rooms is 1+3=41 + 3 = 4.

In the fourth example you need to place routers in rooms 55 and 1010. Then all rooms will be connected to the Internet. The total cost of connection is 5+10=155 + 10 = 15.

Sample Cases

Case 1

Input

5 2
00100

Output

3

Case 2

Input

6 1
000000

Output

21

Case 3

Input

4 1
0011

Output

4

Case 4

Input

12 6
000010000100

Output

15

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback