<pre>import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.StringTokenizer;

/**
 * CTU Open 2015: Frog
 * @author Martin Kacer
 */
public class jump {
	StringTokenizer st = new StringTokenizer(&quot;&quot;);
	BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
	boolean hasNextToken() throws Exception {
		while (!st.hasMoreTokens()) {
			String line = input.readLine();
			if (line == null) return false;
			st = new StringTokenizer(line);
		}
		return true;
	}
	String nextToken() throws Exception {
		return (!hasNextToken()) ? null : st.nextToken();
	}
	int nextInt() throws Exception {
		return Integer.parseInt(nextToken());
	}
	public static void main(String[] args) throws Exception {
		jump instance = new jump();
		while (instance.run()) {/*empty*/}
	}

	int n;
	List&lt;Integer&gt;[] next;
	boolean[] vis;
	int[] q;
	int qb, qe;
	
	void add(int from, int to) {
		if (next[from] == null) next[from] = new LinkedList&lt;Integer&gt;();
		next[from].add(Integer.valueOf(to));
	}
	List&lt;Integer&gt; get(int from) {
		return (next[from]!=null) ? next[from] : Collections.&lt;Integer&gt;emptyList();
	}
	void enqueue(int idx) {
		if (vis[idx]) return;
		vis[idx] = true; q[qe++] = idx;
	}
	
	@SuppressWarnings(&quot;unchecked&quot;)
	boolean run() throws Exception {
		if ((n = nextInt()) == 0) return false;
		next = (List&lt;Integer&gt;[]) new List&lt;?&gt;[3*n]; vis = new boolean[3*n]; q = new int[3*n];
		for (int i = 0; i &lt; n; ++i) {
			int x = nextInt();
			if (x &lt;= i) { add(i, n + i-x); add(2*n + i-x, i); }
			if (i+x &lt; n) { add(i, 2*n + i+x); add(n + i+x, i); }
		}
		qb = qe = 0; enqueue(0);
		while (qb &lt; qe) {
			for (Integer x : get(q[qb++]))
				enqueue(x.intValue());
		}
		for (int i = n-1;; --i) if (vis[i]) {
			System.out.println(i); return true;
		}
	}
}
</pre>
