-
Notifications
You must be signed in to change notification settings - Fork 0
/
Codeforces Round #515 (Div. 3), problem: (B) Heaters
84 lines (79 loc) · 1.7 KB
/
Codeforces Round #515 (Div. 3), problem: (B) Heaters
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization ("unroll-loops")
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define pii pair<int,int>
#define pli pair<long long int,int>
#define pbi pair<bool,int>
#define pob pop_back
#define all(a) a.begin(),a.end()
#define fio ios_base::sync_with_stdio(false); cin.tie(0);cout.tie(0);
#define i64 long long int
#define mem(x,y) memset(x,y,sizeof(x))
#define fill(arr,b) fill(arr, arr+sizeof(arr)/sizeof(arr[0]), b)
#define maxarr(n,arr,m) m =-1e9;for(int i=0;i<n;i++)if(arr[i]>m) m=arr[i];
#define read freopen("input.txt","r",stdin);
const double pi = acos(-1.0);
const int maxn = (i64)1e9+7;
bool arr[1005];
int l[1005],m[1005],r[1005];
int n;
int left(int now)
{
for(int i = n ; i>=1 ; i--)
{
if(now>=l[i]&&now<=m[i]) return m[i];
}
return -1;
}
int right(int now)
{
for(int i = n ; i>=1 ; i--)
{
if(now>=m[i]&&now<=r[i]) return m[i];
}
return -1;
}
int main()
{
//read;
int p;
cin >> n >> p;
mem(l,-1);
mem(r,-1);
mem(m,-1);
for(int i = 1 ; i<=n ; i++) cin >> arr[i];
for(int i = 1 ; i<=n ; i++)
{
if(arr[i]==1)
{
l[i] = i-p+1;
r[i] = i+p-1;
m[i] = i;
}
}
int now=1,cnt=0;
bool fg = true;
for(int i = 1 ; i<=n ; i++)
{
if(now>n) break;
int x = left(now);
int y = right(now);
if(x!=-1)
{
now=r[x]+1;
cnt++;
}
else if(y!=-1)
{
now=r[y]+1;
cnt++;
}
else {fg=false;break;}
}
if(fg) cout<<cnt;
else cout << "-1";
return 0;
}