Wi-Fi
Contest: Codeforces Round 587 (Div. 3)
Difficulty rating: 2100
Tags: data structures, dp, greedy

You work as a system administrator in a dormitory, which has $$$n$$$ rooms one after another along a straight hallway. Rooms are numbered from $$$1$$$ to $$$n$$$.

You have to connect all $$$n$$$ rooms to the Internet.

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

Some rooms also have a spot for a router. The cost of placing a router in the $$$i$$$-th room is also $$$i$$$ 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 $$$i$$$, you connect all rooms with the numbers from $$$max(1,~i - k)$$$ to $$$min(n,~i + k)$$$ inclusive to the Internet, where $$$k$$$ is the range of router. The value of $$$k$$$ is the same for all routers.

Calculate the minimum total cost of connecting all $$$n$$$ 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 $$$n$$$ and $$$k$$$ ($$$1 \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 $$$s$$$ of length $$$n$$$, consisting only of zeros and ones. If the $$$i$$$-th character of the string equals to '1' then there is a spot for a router in the $$$i$$$-th room. If the $$$i$$$-th character of the string equals to '0' then you cannot place a router in the $$$i$$$-th room.

Output format:
Print one integer — the minimum total cost of connecting all $$$n$$$ rooms to the Internet.

Constraints:
Time limit: 2.0 s
Memory limit: 256.0 MB

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

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 = 21$$$.

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

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

Example input:
5 2
00100

Example output:
3

Example input:
6 1
000000

Example output:
21

Editorial:
Firstly, I know that this problem has very pretty linear solution and its author can describe it if he wants. I'll describe my own solution without any data structures but std::set.
Let $$$dp_i$$$ be the total cost to connect all rooms from $$$i$$$ to $$$n-1$$$ to the Internet ($$$0$$$-indexed). Initially $$$dp_{n} = 0$$$, all other values are $$$+\infty$$$. Let's iterate over all $$$i$$$ from $$$n-1$$$ to $$$0$$$ and make some transitions. After all the answer will be $$$dp_0$$$.
The first transition is the easier: update $$$dp_i$$$ with $$$dp_{i + 1} + i + 1$$$ (just connect the current room directly). To do other transitions, let's carry two sets $$$mins$$$ and $$$vals$$$ and one array of vectors $$$del$$$ of length $$$n$$$. Set $$$mins$$$ carries all values $$$dp_{i + 1}, dp_{i + 2}, \dots, dp_{i + k + 1}$$$. Initially it carries $$$dp_n = 0$$$. Set $$$vals$$$ carries the minimum cost to cover some suffix of rooms that also covers the room $$$i$$$. Array of vectors $$$rem$$$ helps us to carry the set $$$vals$$$ efficiently. First of all, if $$$i + k + 2 \le n$$$ then let's remove $$$dp_{i + k + 2}$$$ from $$$mins$$$. Then let's remove all values of $$$del_i$$$ from $$$vals$$$. Then if $$$vals$$$ is not empty, let's update $$$dp_i$$$ with the minimum value of $$$vals$$$. Then if $$$s_i = $$$ '1' then let $$$val$$$ be the minimum value of $$$mins$$$ plus $$$i + 1$$$. Update $$$dp_i$$$ with $$$val$$$ and insert $$$val$$$ into $$$vals$$$. Also let's add $$$val$$$ into $$$del_{i - k - 1}$$$ if $$$i - k - 1 \ge 0$$$. And after we make all we need with the current $$$i$$$, add the value $$$dp_i$$$ to the set $$$mins$$$.
Time complexity: $$$O(n \log n)$$$.
It can be optimized to $$$O(n)$$$ solution with some advanced data structures as a queue with minimums.