C++#include <bits/stdc++.h>
using namespace std;
struct node {
int pos;
int step;
};
queue<node> q;
int visited[20005];
int main() {
int n;
cin >> n;
int a[20005];
for (int i = 0; i < n; i++) {
cin >> a[i];
}
q.push({0, 0});
visited[0] = 1;
while (!q.empty()) {
node f = q.front();
q.pop();
int c = f.pos;
int step = f.step;
if (c >= n) {
cout << step << endl;
return 0;
}
if (a[c] > 0) {
for (int k = 1; k <= a[c]; k++) {
int next = c + k;
if (next >= n || (next < n && !visited[next])) {
visited[next] = 1;
q.push({next, step + 1});
}
}
} else {
for (int k = 0; k <= c + a[c]; k++) {
if (!visited[k]) {
visited[k] = 1;
q.push({k, step + 1});
}
}
}
}
cout << -1 << endl;
return 0;
}