1. 程式人生 > >Codeforces Round #515 (Div. 3)B. Heaters【模擬,思維】

Codeforces Round #515 (Div. 3)B. Heaters【模擬,思維】

B. Heaters

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Vova's house is an array consisting of nn elements (yeah, this is the first problem, I think, where someone lives in the array). There are heaters in some positions of the array. The ii-th element of the array is 11 if there is a heater in the position ii, otherwise the ii-th element of the array is 00.

Each heater has a value rr (rr is the same for all heaters). This value means that the heater at the position pospos can warm up all the elements in range [pos−r+1;pos+r−1][pos−r+1;pos+r−1].

Vova likes to walk through his house while he thinks, and he hates cold positions of his house. Vova wants to switch some of his heaters on in such a way that each element of his house will be warmed up by at least one heater.

Vova's target is to warm up the whole house (all the elements of the array), i.e. if n=6n=6, r=2r=2 and heaters are at positions 22 and 55, then Vova can warm up the whole house if he switches all the heaters in the house on (then the first 33 elements will be warmed up by the first heater and the last 33 elements will be warmed up by the second heater).

Initially, all the heaters are off.

But from the other hand, Vova didn't like to pay much for the electricity. So he wants to switch the minimum number of heaters on in such a way that each element of his house is warmed up by at least one heater.

Your task is to find this number of heaters or say that it is impossible to warm up the whole house.

Input

The first line of the input contains two integers nn and rr (1≤n,r≤10001≤n,r≤1000) — the number of elements in the array and the value of heaters.

The second line contains nn integers a1,a2,…,ana1,a2,…,an (0≤ai≤10≤ai≤1) — the Vova's house description.

Output

Print one integer — the minimum number of heaters needed to warm up the whole house or -1 if it is impossible to do it.

Examples

input

Copy

6 2
0 1 1 0 0 1

output

Copy

3

input

Copy

5 3
1 0 0 0 1

output

Copy

2

input

Copy

5 10
0 0 0 0 0

output

Copy

-1

input

Copy

10 3
0 0 1 1 0 1 0 0 0 1

output

Copy

3

Note

In the first example the heater at the position 22 warms up elements [1;3][1;3], the heater at the position 33 warms up elements [2,4][2,4] and the heater at the position 66 warms up elements [5;6][5;6] so the answer is 33.

In the second example the heater at the position 11 warms up elements [1;3][1;3] and the heater at the position 55 warms up elements [3;5][3;5] so the answer is 22.

In the third example there are no heaters so the answer is -1.

In the fourth example the heater at the position 33 warms up elements [1;5][1;5], the heater at the position 66 warms up elements [4;8][4;8] and the heater at the position 1010 warms up elements [8;10][8;10] so the answer is 33.

題目大意:有n個房子排成一排,部分房子有著火點,有著火點的房子用1表示,沒有的用0表示。著火的房子的覆蓋範圍為[pos - r + 1) ,(pos + r - 1)].,最少點燃多少個房子可以把所有的房子點燃。

題目大致思路:我們每遇到一個著火的房子我們就把他所能覆蓋的範圍的房子的值更新一下,更新的值為將其點燃的房子的覆蓋距離r,然後在處理每一個房子的值,看有沒有為0的點。

#include <bits/stdc++.h>
using namespace std;

const int MAXN = 1000010;
int a[MAXN];

int main(){
	int n,r;
	while(~scanf("%d %d",&n,&r)){
		int k;
		memset(a,0,sizeof(a));
		for(int i = 1; i <= n; i++){
			scanf("%d",&k);
			if(k == 1){
				for(int j = max(1,i - r + 1); j <= min(n,i + r - 1); j++){
					a[j] = max(a[j],i);
				}
			}
		}
		int i = 1,ans = 0;
		bool flag = false;
		while(i <= n){
			if(a[i] == 0){
				printf("-1\n");
				flag = true;
                break;
			}
			ans++;
			i = a[i] + r;
		}
        if(!flag) printf("%d\n",ans);
	}
	return 0;
}