#include <bits/stdc++.h>
using namespace std;
int main() {
cin.tie(0)->sync_with_stdio(0);
int n, d; cin >> n >> d;
vector<int> a(n); for (int i = 0; i < n; i++) cin >> a[i];
vector<int> f(n, INT_MAX), r(n, INT_MAX);
f[0] = r[n-1] = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < i; j++) {
if (abs(j-i) > d) continue;
f[i] = min(f[i], a[i] ? f[j] : f[j] + 1);
}
}
for (int i = n-1; i >= 0; i--) {
for (int j = i+1; j < n; j++) {
if (abs(j-i) > d) continue;
r[i] = min(r[i], a[i] ? r[j] : r[j] + 1);
}
}
int x = INT_MAX; for (int i = 0; i < n; i++) x = min(x, a[i] ? f[i] + r[i] : f[i] + r[i] - 1);
cout << x << "\n";
return 0;
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |