Starting from the first pebble, follow jumps allowed when two spot counts sum to their distance and report the farthest reachable pebble.
Medium6GraphBFSHash mapNo attempts yetTime limit2sMemory limit256 MBYoshi is a frog. He lives in the zoo under a log that was carried there whole from a distant equatorial rainforest. The log is big and wet, so it attracts flies, and that is what keeps Yoshi happy.
A line of pebbles runs through the wetland in front of the log. The pebbles carry dark spots, and Yoshi sometimes looks at them and pretends they are very big flies.
Yesterday his friend Addawser the camel came by and suggested a game.
"Do you see those spots on the pebbles?" asked Addawser. "Start on the leftmost pebble and jump from one pebble to another, with one restriction. You may jump between two pebbles only if the sum of the numbers of spots on them equals the distance between them. And you have to get to a pebble as far away as you can."
"All right, but you know that I can count at most to twenty three," hesitated Yoshi.
"No problem, I will help you with the bigger numbers," said Addawser.
The pebbles lie on a straight line and the distance between two neighbouring pebbles is exactly 1. A jump may go to the left or to the right. Starting from the first pebble, find the distance of the most distant pebble that can be reached by a sequence of jumps that follow the rule.
The input holds several test cases. Each case starts with a line containing one integer N (1≤N≤106), the number of pebbles. The second line holds N integers in the same order as the pebbles lie in the wetland, and the i-th integer is the number of spots on the i-th pebble. No pebble carries fewer than 0 or more than 109 spots. The number of different pairs of pebbles that Yoshi can jump between never exceeds 106.
The input ends with a line holding a single 0, which is not processed.
For each test case, print one line with the distance of the pebble that is reachable by successive jumps under the rule and that is the most distant from the first pebble. Print 0 if no jump from the first pebble is possible.