We need to understand problem: A plurality of trampolines are arranged in a line at 10 m intervals. Each trampoline has its own maximum horizontal distance within which the jumper can jump safely. Starting from the left-most trampoline, the jumper jumps to another trampoline within the allowed jumping range. The jumper wants to repeat jumping until he/she reaches the right-most trampoline, and then tries to return to the left-most trampoline only through jumping. Can the jumper complete the roundtrip without a single stepping-down from a trampoline?
Write a program to report if the jumper can complete the journey using the list of maximum horizontal reaches of these trampolines. Assume that the trampolines are points without spatial extent.
Input: N and then N lines d_i.
Interpretation: There are N trampolines equally spaced 10 meters apart. They are points. The left-most is index 1, then 2, ..., N. The distances between consecutive trampolines are 10m. The jumper can jump from trampoline i to trampoline j (i != j) if the absolute distance (|i - j| * 10) is <= d_i (the maximum allowable jumping distance of the starting trampoline). Actually "within the allowed jumping range" likely means that the jump length (horizontal distance) does not exceed d_i. Since trampolines are points, the jump distance is the Euclidean distance which equals the number of intervals * 10 meters. So from i you can jump to any j such that 10*|i-j| <= d_i.
We need to determine if there exists a sequence of jumps from trampoline 1 to N and then back from N to 1, using only jumps allowed by the trampoline you're on, without ever stepping down (i.e., without ever failing to find a next jump?). Actually "without a single stepping-down from a trampoline" means that the jumper never has to step off the trampoline onto the ground; i.e., can complete the entire roundtrip using only jumps from trampoline to trampoline.
Thus, we need to check if there is a directed path from 1 to N (using the allowed jumps: from i you can go to j if |i-j|*10 <= d_i) and also a directed path from N back to 1 (using the same rule but with d_i for each starting point). The roundtrip is completed by starting at 1, going to N, then returning to 1. It does not necessarily require that the path from N to 1 is the reverse of the forward path; can be different.
But is it required that the jumper must only use each trampoline at most once? Not specified; can revisit? Typically, the jumper can jump any number of times; but we must ensure that a sequence exists. Since the trampolines are points without spatial extent, and the jumps are based only on distances, we can possibly reuse trampolines. However, for feasibility, we just need existence of some sequence of jumps that starts at 1, ends at N, and then from N to 1, without leaving the network. Essentially, we need that 1 can reach N in the directed graph defined by edges i->j for all j such that 10*|i-j| <= d_i, and also that N can reach 1 in that directed graph. So we need to check if there is a directed path from 1 to N and also from N to 1.
Given that N up to 300k, we cannot build all edges (which could be O(N^2)). We need an efficient way.
Observations: The edges from i are to all indices j within a certain distance: i can jump to any trampoline within distance floor(d_i/10) steps (since 10 m intervals). So the reachable set from i is the interval [i - L_i, i + L_i] where L_i = floor(d_i/10) (integer). But careful: d_i is given in meters; maximum jumping distance is integer. Since spacing is exactly 10 m, we can compute max number of trampolines away: max_k such that 10*k <= d_i => k = d_i//10 (integer division). So from trampoline i, you can jump to any j satisfying |i - j| <= d_i//10. So the graph is like a directed interval graph where each node has outgoing edges to all nodes within a certain range.
Thus, we need to determine if node 1 can reach node N and node N can reach node 1 in this directed graph.
This is reminiscent of problems about forward/backward reachability in a directed graph with intervals. Since edges are symmetric in terms of reachable indices (they go both left and right within range). Actually outgoing edges from i cover a symmetric interval around i. However, the graph is not necessarily symmetric because d_i might be different; from i you can go to j but from j you may not be able to go back to i unless d_j is large enough.
We need to test reachability from 1 to N. Because each node can reach a contiguous interval? Let's think: If from i you can go to any j in [i - L_i, i + L_i] (within 1..N), then the set of nodes reachable from 1 might not be contiguous? Actually if you can jump to any node within an interval, then the reachable region could be expanded by jumping to farther nodes which then have their own intervals. This is similar to the classic "jump game" but with jumps allowed both directions and distances are multiples of 10. However, typical "jump game" where you can jump from i to j if j-i <= something (only forward) leads to checking if you can reach end. Here you can also jump backward, but since we just need to reach N from 1, we might only need forward jumps; but backward jumps could help by moving back to a node that has a larger forward reach. So we need to consider both directions.
Nevertheless, because edges are defined as intervals, the reachable set from a starting node may be a contiguous interval of indices. Let's examine: Starting from i, you can directly reach any node within [i - L_i, i + L_i]. If you then go to a node j in that interval, you can from j reach any node within [j - L_j, j + L_j]. The union of these intervals may combine to create a larger contiguous interval. In many similar problems (like gas stations with teleporters), the reachable set from a starting point is indeed a contiguous interval because if you can reach two points, you can reach everything between them (since you can jump between them? Not necessarily directly, but if you have nodes that can be reached, the connectivity might be such that you can also reach intermediate points. But is it guaranteed? Let's check: Suppose we have nodes 1,2,3,4. From node 1, we can reach nodes 2 and 4 but not 3 directly (maybe due to range). Then from node 2, you might not be able to reach 3, and from 4, you might not be able to reach 3. So it's possible to have reachable set non-contiguous? But note: If you can reach node 4, you must have taken a path from 1 to 4. The intermediate nodes visited might allow reaching 3? Not necessarily. However, consider the nature of jumps: each jump goes from a node to any node within a distance bound. If you can jump from some node a to node b, you are effectively moving to b. Along the way, you could have jumped to any intermediate node as well if they are within the same distance bound? Actually if the distance bound allows jumping to b, it also allows jumping to any node between a and b because those are closer. Since the condition is based on absolute difference, if |a-b| <= L_a, then for any c between a and b, |a-c| <= |a-b| <= L_a, so you can jump directly from a to c as well. So from any node, its outgoing edges are to all nodes within the interval. Thus, if you can go from a to b, you can also go directly to any intermediate node. So in a path from 1 to 4, say 1 -> 2? But if 1 can jump to 4 directly, then it can also jump to 3. However, maybe the path uses intermediate nodes to extend reach. But the reachable set from a starting node is the set of nodes that can be reached via any sequence. If we can reach node 4, we can directly from the starting node? Not necessarily; we might need to go through intermediate nodes to get a larger range. But consider: If we can reach node 4 via some path, then at the last step we jump from some node k to 4. Since k can jump to 4, it can also jump to any node between k and 4. But does that guarantee that node 3 is reachable from the start? Possibly if k is to the left of 3? But if k > 3, then the interval from k to 3 might be larger than allowed? Actually if k > 4? That's impossible because we are jumping from k to 4, so k can be left or right of 4. But typical path to rightmost would involve moving generally rightwards. However, it's plausible that we can reach node 4 but not node 3 if 3 lies outside the intervals of visited nodes. Let's try to construct a counterexample: positions 1,2,3,4,5, etc. L_i values: Suppose N=5. d_i//10. Let's set L_1 = 0? No minimum is 1? d_i >=1, but floor(d_i/10) can be 0 if d_i<10. So L_i can be 0 meaning you can only stay? But you must jump to another trampoline? The problem says "jumps to another trampoline". So you cannot stay; you must jump to a different trampoline. So if L_i=0, then you cannot jump to any other trampoline because distance >=10 > d_i. But is that allowed? d_i can be as low as 1, so L_i=0, then you cannot jump to any other trampoline. So such a trampoline is a dead end except maybe if it's the starting point? But if starting at 1 with L_1=0, you cannot even start. So we assume there is at least some path.
Now, can reachable set be non-contiguous? Suppose we have nodes 1,2,3. L_1 = 1 (can jump to 2). L_2 = 0 (cannot jump anywhere). L_3 = 1 (can jump to 2? Actually from 3 can jump to 2 if distance 10, L_3=1, yes can jump to 2). Starting from 1: direct reachable: {2}. From 2, cannot go anywhere else. So reachable set = {1,2}. That's contiguous (1 and 2). Not a counterexample.
Let's try more: nodes 1,2,3,4. L_1 = 2 (can jump to 2,3). L_2 = 0. L_3 = 0. L_4 = 2 (can jump to 2,3). Starting from 1: can go to 2 and 3. From 2 cannot go; from 3 cannot go. So reachable = {1,2,3}. Not including 4. To reach 4, need path. Could we go from 1 to 3, then from 3 cannot go to 4. So not reachable. If L_1 = 2 (includes 4? Actually distance from 1 to 4 is 30, L_1=2 would only up to 20, so not reachable). To reach 4, we could have L_1 = 3 (can reach 4). Then reachable includes 4 and also 2,3. So contiguous.
It seems plausible that reachable set from a node in such an interval graph is always a contiguous interval. Let's try to prove: Define from a start s, let R be set of reachable indices. Consider any two reachable indices a and b with a < b. We want to show that any c with a < c < b is also reachable. Since a is reachable, there is a path from s to a. Similarly, there is a path to b. If the path to a and b share some node, maybe we can use that to get to c? Not obviously direct. But we might argue using the property that from any node you can jump to any point within its interval. The set of nodes that can be reached in exactly one jump from any node is an interval. The transitive closure of such intervals might preserve contiguity. Actually there is a known result: For a directed graph where each node has outgoing edges to all nodes in a contiguous interval (including possibly both directions), the reachable set from a single source is a contiguous interval. This can be proven by considering the leftmost and rightmost reachable nodes and showing that everything in between can be reached. Suppose we have a reachable node L (leftmost) and R (rightmost). For any c in (L,R), we need to show c is reachable. Since L and R are reachable, there exist paths. The union of intervals visited might cover c. But need formal: Because from any node we can go to any node within its interval, we can effectively "fill in" gaps.
Consider the process of exploring reachable nodes: Start with interval I0 = {s}. Then from each node in the current set, we can add its interval. So the reachable set is the limit of applying the operation: expand set S to S ∪ (∪_{i∈S} [i - L_i, i + L_i] ∩ [1,N]). This is like repeatedly taking union of intervals. Starting from a point, after one step we get an interval (maybe with gaps? Actually union of intervals centered at points in S. If S is not contiguous, the union could be non-contiguous. But if S starts as a single point, after one step it's an interval. After two steps, if we have an interval, the union of intervals from all points in that interval: For each i in interval I, we add [i - L_i, i + L_i]. The union of such intervals over a contiguous interval might not be contiguous if L_i vary? Let's test: Suppose we have positions 1,2,3. L_1 = 10 (covers all), L_2 = 0 (covers only itself), L_3 = 10 (covers all). Starting from 1: reachable after 1 step: [1-10,1+10] ∩ [1,3] = {1,2,3}. So it's contiguous. Starting from 2: reachable after 1 step: {2} (since L_2=0, only itself? But need to jump to another trampoline; can't stay; so actually you cannot stay, so from 2 you cannot jump to any other, but you are already at 2; reachable set includes 2 itself (starting point). The definition of reachable includes the start node. But can we consider 2 itself as reachable? Yes. Then from 2, we cannot expand because its outgoing edges are none? Actually if L_2=0, then from 2 you cannot jump to any other trampoline, because the distance to any other is at least 10 > d_2. So reachable set remains {2}. That's contiguous.
What about a scenario: positions 1,2,3,4. L_1 = 2 (covers 1,2,3), L_2 = 0, L_3 = 0, L_4 = 2 (covers 2,3,4). Start at 1: after 1 step, reachable = {1,2,3}. That's interval [1,3]. Now from interval [1,3], we consider adding intervals for each i in [1,3]. For i=1, add [1-2,1+2] = [ -1,3] -> {1,2,3}. For i=2, add nothing beyond 2? L_2=0, so only {2} (but 2 already in set). For i=3, L_3=0, add {3}. So union remains [1,3]. Cannot reach 4 because L_2 and L_3 are 0, so cannot extend. So reachable set is contiguous.
Now consider a more complex: L_i values such that from an interval, the union might not cover a gap inside the interval? But if the interval is contiguous, then the union of intervals from points in that interval should be contiguous? Not necessarily; think about points with L_i that are "holey". For example, if we have points at 1,2,3,4. L_1 = 1 (covers 1,2). L_2 = 0. L_3 = 0. L_4 = 1 (covers 3,4). Starting from interval [1,2] (from 1's reach), can we reach 3 or 4? From 2, no; from 1, only up to 2. So reachable remains [1,2], not covering 3,4. But that's fine; it's contiguous. To get a non-contiguous reachable set, we would need that we can reach a point far away but not some intermediate point. For that to happen, there must be a jump that skips over a point that cannot be reached from any node in the path. But if we can jump from a to b where b > a+1, then from a we could also jump to a+1 directly (since it's closer). So if we have a path that goes from s to a (which can be reached) and then from a to b (skipping over some indices), then those intermediate indices are also reachable because we could have chosen to jump to them directly from a. But we need to ensure that we have a path that reaches a; then from a, we can directly go to any intermediate point between a and b. So those intermediate points become reachable as well. However, the path we used to get from s to a might have restrictions: But once we are at a, we can jump to any point within its interval. So if we can reach a, then all points in the interval around a are reachable. So if we can reach b via a, we can also reach any point between a and b as long as they are within the interval of a. But what if the intermediate point c is not within a's interval? For b to be within a's interval, we have |a-b| <= L_a. For c between a and b, we have |a-c| <= |a-b| <= L_a, so c is within a's interval. So if a can jump to b, it can also jump to any c between a and b. So indeed, if b is reachable via a jump from a, then all points between a and b are also reachable from a. Thus, they become reachable as well.
Now consider a multi-step path: s -> ... -> x -> y -> ... -> b. If y is reachable, then from y we can reach all points between y and b. But what about points between x and y? From x we could reach those if they are within its interval. But does the fact that we can reach y guarantee we can reach some point between x and y? Possibly not, but we may not need: we just need to show that if we can reach two points L and R, then any point between them is reachable. Suppose we have reachable nodes a and b with a < b. There exists a path from s to a and a path from s to b. Consider the union of intervals visited in these paths. Could there be a gap? Let's try to produce a counterexample: Use three nodes 1,2,3. Suppose L_1 = 2 (can reach 1,2,3). L_2 = 0 (cannot jump anywhere). L_3 = 0. Starting from 1, reachable = {1,2,3}. So contiguous.
Now consider nodes 1,2,3,4. Let L_1 = 1 (can reach 2). L_2 = 2 (can reach 1,3,4). L_3 = 0. L_4 = 0. Starting from 1: direct reachable {1,2}. From 2, we can reach 1,3,4. So from 1 we can reach 4 via 1->2->4? Check: from 2, can it jump to 4? distance 20 => L_2 must be >=2 to cover 4. With L_2=2, yes. So reachable set = {1,2,3,4}? Actually from 2, interval [0,4] (since index 2 ±2 = [0,4] => {1,2,3,4}) So reachable = {1,2,3,4}. Contiguous.
What about something like: L_1 = 2 (covers 1,2,3). L_3 = 2 (covers 1,2,3,4,5? Actually index 3±2 = [1,5] so covers 1,2,3,4,5). L_2 = 0. Start from 1: can reach 3 directly? Yes, since |1-3|=2 <= L_1=2, so 3 reachable. From 3, can reach 5. So reachable set includes 5. Now what about 4? From 3, can reach 4 because |3-4|=1 <=2, so 4 reachable. So contiguous.
It seems hard to have non-contiguous reachable set because the ability to jump from any node to all nodes within an interval implies that if you can reach a node, you can also reach all nodes within its interval. Moreover, intervals can combine to cover gaps. I think it's true that the reachable set from any source is a contiguous interval of indices. This is a known property for such "interval reachability" or "jump game with both directions". For example, in problems like "frog jumping" where you can jump forward up to some distance, the reachable set is a contiguous prefix. But here you can jump backward, so it's a contiguous interval.
Thus, we can compute the reachable interval from 1 efficiently by expanding the interval using a kind of BFS or iterative expansion: Initially left = right = 1. Then repeatedly, for all i in current interval, we can expand the interval to min(left, i - L_i) and max(right, i + L_i). However, this is not straightforward because when we expand, we include new nodes, which themselves might further expand the interval. This is akin to computing the closure of union of intervals. This process can be done with a sweep line: Keep track of the maximum rightmost extension we can achieve from any point within the current reachable interval, and similarly the leftmost.
Actually, this is similar to the problem of determining the reachable set in a directed graph where each node points to a contiguous interval. It can be solved by a linear scan: Starting from the source, we can keep track of the farthest right we can reach so far, but we also need to consider that we can go left. However, since we can also go left, the reachable interval expands both ways. We can compute the reachable interval by repeatedly updating left and right until no change, but that would be O(N^2) in worst-case if naive.
But we can apply a technique similar to BFS with segment tree or using a "maximum reach" precomputation and performing something like a two-pointer expansion. However, N is up to 300k, which is manageable for O(N log N) or even O(N) with clever method.
We can think of it as a graph problem: For each node i, there are edges to all nodes in [i - L_i, i + L_i]. We need to test if node N is reachable from node 1, and if node 1 is reachable from node N. This is essentially checking strong connectivity? Not necessarily; we only need a path from 1 to N and from N to 1, not necessarily that all nodes are strongly connected.
But we can treat each direction separately: compute the set of nodes reachable from 1 (which we believe is an interval). Similarly, compute the set of nodes reachable from N (maybe also an interval). Then check if N is in reachable set from 1 and 1 is in reachable set from N.
But careful: The reachable set from 1 might not be a contiguous interval if my earlier reasoning fails. Let's attempt to construct a counterexample deliberately. We need a node that can be reached from 1, but some node between them cannot be reached. Suppose positions 1,2,3,4,5. Let L_1 = 2 (covers 1-3). So from 1 we can reach 3 directly. But can we reach 2? From 1, we can also go to 2 directly. So 2 is reachable. So if we can reach 3, we can also reach 2 because it's within the same interval. So any node that is within the interval of the first jump is reachable. To skip an interior node, the interior node must not be within the interval of any node we can reach along the path. But if we have a path 1 -> a -> b where a and b are such that b is far away but the interior between a and b includes some node c that is not within a's interval? That's impossible because if b is within a's interval, then all nodes between a and b are also within a's interval. So c would be reachable directly from a. So that can't happen.
But what if the path involves multiple jumps that go left and right, and the interior node c is not covered by any node's interval that we have visited? However, if we visited node a and node b, and c lies between them, it's not necessary that either a or b's interval covers c if c is not between them in a straight line? Actually if a < c < b, then from a, does it cover c? Since b is reachable from a via a path but not necessarily directly. It could be that a cannot directly reach b; instead, the path goes a -> x -> ... -> b. So c might lie between a and b, but not necessarily between a and x. For example, a=1, b=5, c=3. Suppose path: 1 -> 2 -> 4 -> 5. From 1, can we reach 3? Possibly not if L_1 only covers 2. From 2, maybe covers 4, but does 2 cover 3? If L_2 is at least 1, then 2 can reach 3. So maybe 3 reachable from 2. But if L_2 = 2 (covers 4, but maybe covers 3? Distance to 3 is 10, so L_2 must be >=1 to cover 3). So if L_2 is exactly 2, it covers 3. Actually any L>=1 covers distance 10, so any L>=1 includes immediate neighbors. To skip 3, we would need that no node on the path can jump to 3. Could we have path 1 -> 2 -> 4 -> 5 where L_1=1 (covers only 2), L_2=2 (covers 4,5, and also 3? Distance 2->3 is 10, need L_2>=1, so it would cover 3 as well if L_2>=1). If L_2 is exactly 2, it certainly covers 3. So to exclude 3, we need L_2=0? But then 2 cannot jump to 4 either because 4 is distance 20, so L_2 must be at least 2 to reach 4. So cannot both exclude 3 and include 4. More generally, if a node can jump to a node at distance d, it can also jump to any node at distance less than d (since they are closer). So the set of reachable nodes from a node is an interval centered at that node. So if a node can jump to a far node, it can jump to all nodes in between. Therefore, if we have a node in the path that can jump to some node to the right, all intermediate nodes are also reachable from that node. So along any path, if we ever make a jump from i to j that skips over some indices, those indices become reachable from i directly. But we might not have visited them if we didn't choose to; but they are reachable. So if we can reach j, then we can also reach those intermediate indices by simply from i jumping to them instead of j. So they are reachable from the start as well, because we already have a path to i, and from i we can directly go to those intermediate nodes. So indeed, if j is reachable, then all nodes between i and j are reachable. By induction, the entire interval between the leftmost and rightmost reachable nodes should be reachable. So reachable set is contiguous.
Formal proof: Let L = min reachable, R = max reachable. Suppose there exists c with L < c < R not reachable. Since L and R are reachable, there exist paths. Consider the smallest index > L that is not reachable. But from the node that reaches the first index after L, it must cover that index? I think it's solid.
Thus, we can characterize reachable set from source s as an interval [L_s, R_s] that can be computed by repeatedly expanding using the maximum left and right extents of nodes within the current interval. This process is similar to "interval expansion" which can be done with a segment tree or using a two-pointer like algorithm where we keep track of the furthest left and right we can reach, and then we need to consider the intervals of newly added nodes. A standard approach: We can precompute arrays of max_right and min_left reachable from each index? Actually from a node i, the interval it can directly reach is [i - L_i, i + L_i]. So if we have a current interval [l, r], we can compute the new l' = min{ i - L_i for i in [l, r] } and new r' = max{ i + L_i for i in [l, r] }. Then the reachable interval becomes [min(l, l'), max(r, r')]. This process repeats until it stabilizes. Since the interval only expands outward, we can iterate until no more expansion. However, naive recomputing min and max over the interval each time could be O(N^2). But we can accelerate by noticing that we only need to know, for any current interval, the minimum of (i - L_i) over i in that interval, and maximum of (i + L_i). As we expand the interval, we can maintain prefix/suffix data structures to query these min/max over any interval quickly. But we need to update the interval iteratively. However, the number of expansions is at most O(N) because each expansion adds at least one new index (unless we expand left or right beyond current bounds). In worst case, we might expand left from 1 to N step by step, causing O(N) iterations. Each iteration we need to compute min over current interval of (i - L_i) and max of (i + L_i). If we do a linear scan each time, total could be O(N^2). But we can do better.
Alternate perspective: The reachable interval from s can be computed via BFS on a graph with edges defined by intervals. Since each node connects to an interval, we can use a technique similar to "minimum jumps to reach end" but with both directions. Actually, we can model it as: we want to know if N is reachable from 1. This is similar to "can we jump from 1 to N using jumps that are symmetric"? Might be simpler: Since you can always jump to any node within your interval, you can essentially "teleport" to the farthest reachable from any node you can get to. So the problem reduces to checking whether 1 can eventually reach N through a chain of intervals. This is like the classic "merge intervals" problem: Starting from 1, you can reach up to R1 = 1 + L_1 (and down to L1 = 1 - L_1). But to extend further right, you need to have a node within your reachable interval that can jump farther right. So the maximum right you can achieve is the maximum of (i + L_i) for i in the interval you can currently reach. Similarly for left. So this is like computing the union of intervals in a sequential manner.
We can compute the maximum right reachable from 1 by scanning from left to right, maintaining a variable "furthest" that denotes the furthest index we can reach so far, but that is for forward-only jumps. However, we have ability to go left and then right, which could allow us to access nodes with larger rightwards extension that are to the left of the current rightmost reachable. But because we can go left arbitrarily within the reachable interval, we can always access any node within the interval. So the process is: initially, reachable set = [1, 1]. Then we compute max_right = max_{i in [1,1]} (i + L_i) = 1 + L_1. And min_left = min_{i in [1,1]} (i - L_i) = 1 - L_1. So reachable becomes [min(1,1-L_1), max(1,1+L_1)] = [l, r] with l = max(1, 1-L_1? Actually indices are 1..N, so we clamp to 1 and N. So l = max(1, 1-L_1) but if L_1>0, l could be 1 if L_1=0? Actually 1-L_1 could be 0 or negative, then leftmost is 1. So effective l = max(1, 1-L_1) but we can also consider that we can't go below 1. So after first step, reachable is [1, r1] where r1 = min(N, 1+L_1). But we may also extend left if L_1 large enough to go left, but since leftmost is 1, no left extension.
Now, within this new interval [1, r1], we can consider any i. So the new max_right = max_{i in [1, r1]} (i + L_i). If that exceeds r1, we expand to that new r. Similarly, we might expand left if any i in [1, r1] has i - L_i < 1, but left is already 1, so no further left (since we cannot go below 1). So essentially from source 1, we only need to expand rightwards (since we start at leftmost). However, it's possible that to expand further right, we might need to go left first to access a node that has a larger rightward extension, but that node would have to be to the left of the current left bound. But our left bound is 1, the smallest index; we cannot go left of 1. So we can't go left beyond 1. So any node that could provide a larger rightward extension must have index <= r1 (the current reachable interval). Because we can only reach nodes within [1, r1]. So expanding rightwards is sufficient: we keep updating r = max_{i in [1, r]} (i + L_i). This is exactly the process of the classic "jump game" where you can jump forward any distance up to a certain maximum from each position, but you can also jump backward? Actually in classic jump game, you can only jump forward from i to j>i with j-i <= some a_i. Here we can jump both directions, but since we start at leftmost and want to reach rightmost, going left wouldn't help because it would take us to indices that are already reachable (they are left of current rightmost). However, consider the possibility that to reach a far right index, you might need to first jump left to a node that is not currently in the interval because it is to the left of 1? That's impossible. So left extension doesn't help to go right, because leftmost is 1, already the extreme left. So the reachable set from 1 will indeed be a contiguous interval [1, R] where R can be computed by repeatedly taking the maximum of (i + L_i) for i in [1, currentR]. This is similar to "can you reach beyond N?" but here we also allow jumping to any node, not just forward. However, note: The ability to jump both directions might allow us to "skip" some nodes that we haven't reached because we could jump backward to a node that is not in our current interval? Wait, if we start at 1, we can only reach nodes within [1, currentR]. If we jump backward, we go to a node with smaller index, but that is still within [1, currentR] because any backward jump from a node i in [1, currentR] will land at a node >= i - L_i, which could be less than 1? Actually it could be less than 1? i - L_i could be 0 or negative, but indices start at 1. So the jump would be to max(1, i-L_i). So if i-L_i < 1, you'd land at 1? Actually you can't jump to a non-existent trampoline; you can only jump to existing trampolines. So the allowed j must be between 1 and N. So the reachable set is constrained to indices 1..N. So from i, you can only jump to j where 1 <= j <= N and |i-j| <= L_i. So if i - L_i < 1, you cannot jump to indices less than 1; you can only jump to indices >= 1. So effectively, the interval is clipped. So the backward jump from i could take you to index 1 if 1 is within range? Actually if i - L_i < 1, then the reachable indices on the left side start from 1. So indeed you can only go to indices >=1. Since we are already at leftmost, there is no node to the left of 1. So left expansion is limited to 1.
Thus, from 1, the reachable set will be [1, R] for some R. Similarly, from N, because of symmetry, the reachable set will be [L, N] for some L (since rightmost is N, you cannot go beyond N). So we can compute R as the maximum index reachable from 1, and L as the minimum index reachable from N. Then check if R >= N (i.e., N is reachable from 1) and L <= 1 (i.e., 1 is reachable from N). But careful: For N to be reachable from 1, we need N ∈ reachable set, which is equivalent to R >= N. For 1 to be reachable from N, we need the minimum reachable index from N (call it L_N) <= 1. Since indices are between 1 and N, L_N <= 1 means L_N must be 1 (since minimum index is 1). So we need L_N = 1. But is it sufficient that the minimum reachable from N is 1? Because reachable set from N is an interval [L_N, N]. So if L_N = 1, then 1 is in that interval, so reachable. So yes.
Thus, the problem reduces to computing the rightmost reachable index from 1, and the leftmost reachable index from N. If from 1 we can reach N (R >= N) and from N we can reach 1 (L_N <= 1), answer yes; else no.
But wait: Example 1: N=4, d=[20,5,10,1]. Compute L_i = d_i//10: [2,0,1,0] (since 5//10=0, 10//10=1). From 1: initial reachable interval [1,1]. max_right = 1+L_1=3 => reachable [1,3]. Within [1,3], compute max(i+L_i): i=1 ->3, i=2 ->2+0=2, i=3->3+1=4 => max=4 => reachable expands to [1,4]. Now includes 4. So R=4, so N reachable. Now from N=4: L_4=0, so initially reachable [4,4]. min_left = 4 - L_4 =4 => reachable [4,4]. No left expansion. So L_N=4 >1, so 1 not reachable from 4. Thus answer no. That matches example output "no". Good.
Example 2: N=3, d=[10,5,10] => L=[1,0,1]. From 1: initial [1,1], max_right=1+1=2 => [1,2]; within [1,2], max i+L_i: i=1->2, i=2->2+0=2, so max=2 => cannot expand to 3. So R=2 < N=3, thus cannot reach N. So answer no. Example output no. Good.
Example 3: N=4, d=[20,30,1,20] => L=[2,3,0,2]. From 1: [1,1] -> max_right=3 => [1,3]; within [1,3], max i+L_i: i=1->3, i=2->5 (but N=4, so capped at 4? Actually i+L_i can be >N, but we can only reach indices up to N. So effectively we consider min(N, i+L_i) for max? But max_right we want the maximum index we can reach directly from some i. However, i+L_i may exceed N, but that means we can reach N directly if N <= i+L_i. So we take max_i (min(N, i+L_i)). But when computing max_right, we can treat the effective reach as min(N, i+L_i). But careful: If i+L_i > N, we can reach N, and that's the farthest we can go because there's no index beyond N. So we can cap at N. So in the interval expansion, we consider the maximum of min(N, i+L_i). So for i=2: i+L_i=5, but max index is 4, so effective 4. So max = max(3,2,4)=4 => reachable expands to [1,4]. So from 1, can reach N. Now from N=4: L_4=2 => initial [4,4]; min_left = 4 - 2 =2, so reachable [2,4] (since we clamp to at least 1). Then within [2,4], compute min(i-L_i): i=2->2-3=-1 => but clamp to 1? Actually we need the minimum index we can reach directly from any i in [2,4]. The leftmost reachable index from i is max(1, i-L_i)? Wait, from i we can reach any j such that |i-j| <= L_i, i.e., j >= i - L_i and j <= i + L_i, but also j must be between 1 and N. So the leftmost index reachable from i is max(1, i - L_i). But in terms of expanding the interval to the left, we want the minimum over i of (i - L_i) but then clamp to 1. Actually, if we have current interval [l,r] (reachable), we can compute the new leftmost index reachable as min_{i in [l,r]} (i - L_i) but then we need to consider that we cannot go below 1, so the actual new left boundary would be max(1, min_{i in [l,r]} (i - L_i)? Not exactly: Suppose from some i we can reach a j < 1? No, we can't. So the reachable set after considering jumps is the union over i in [l,r] of {j in [1,N] : |i-j| <= L_i}. This union's leftmost index is max(1, min_{i in [l,r]} (i - L_i))? Actually if min_{i} (i - L_i) is less than 1, that doesn't mean we can reach indices below 1. The smallest index we can reach is 1 if any jump can reach 1. But if min(i - L_i) < 1, then from that i we can reach index 1 (since 1 is within the interval if i - L_i < 1 <= i + L_i). So the new left boundary will be 1 if any i can reach 1. More formally, the union's minimum index is max(1, min_{i in [l,r]} (i - L_i))? That would be 1 if min(i - L_i) <= 1. But if min(i - L_i) > 1, then the minimum is that value. So the new left boundary = min(l, max(1, min_{i in [l,r]} (i - L_i)))? Actually l is already the current left boundary, which is >=1. We want the smallest index that becomes reachable. That is min over i of max(1, i - L_i). But since i - L_i could be less than 1, the effective leftmost from i is max(1, i - L_i). So the overall new leftmost is min_{i in [l,r]} max(1, i - L_i). But note that if any i has i - L_i <= 1, then max(1, i-L_i)=1, so leftmost could become 1. So to compute the expansion leftwards, we need the minimum over i of max(1, i - L_i). However, this is equivalent to taking min_{i} (i - L_i) and then clamping: if min(i - L_i) <= 1, then new leftmost becomes 1; else it becomes that minimum (since then all i - L_i > 1, and max(1, i-L_i) = i-L_i). So we can compute min_i (i - L_i) over the interval, and then the new left boundary candidate = min_i (i - L_i). But we need to clamp to 1: actually the new left boundary would be max(1, min_i (i - L_i))? No, if min_i (i - L_i) = -2, then max(1, -2) = 1, correct. If min_i (i - L_i) = 3, then max(1,3)=3, correct. So new left boundary = max(1, min_{i in [l,r]} (i - L_i)). But wait, is that correct? Suppose we have two nodes: i=3, L_i=1 => i-L_i=2; i=5, L_i=0 => i-L_i=5. min =2, max(1,2)=2. But from i=3, we can reach index 2, yes. So left boundary becomes 2. That seems correct. But what about i=3, L_i=5 => i-L_i=-2, min = -2 => new left boundary = max(1, -2) =1. But from i=3, we can reach index 1 (since distance 2), yes. So new left boundary 1. So formula holds: new_left = max(1, min_{i in [l,r]} (i - L_i)). Similarly, new_right = min(N, max_{i in [l,r]} (i + L_i)). Then the reachable interval becomes [min(l, new_left), max(r, new_right)]. Since l is already >=1, min(l, new_left) effectively is new_left if new_left < l else l. So we can iterate.
In the case from N, we start at N and want to expand leftwards, but we might also expand rightwards (but rightmost is N). So we only need to compute the leftmost index reachable from N. However, as we expand leftwards, we may also expand rightwards which might allow further leftwards expansion via nodes that are to the right of N? No, there is no right of N. So right expansion does nothing. But could we expand leftwards further by first expanding rightwards to some node that has a larger leftward reach? But rightwards beyond N is impossible. So from N, the process is symmetric: we compute the leftmost reachable index L_N by iteratively updating left = min_{i in [left, N]} (i - L_i) clamped to 1. Actually we need to handle that when we expand leftwards, we include new nodes to the left, which may have even smaller i - L_i, so we need to consider them. So we can compute the minimal left index reachable from N by a similar process: start with left = N, right = N. Actually we need both left and right because as we expand leftwards, the set becomes an interval [L, N]. Within that interval, we compute the minimum of (i - L_i) over i in [L, N] to possibly reduce L further. So it's like: L = N; while True: new_L = max(1, min_{i in [L, N]} (i - L_i)); if new_L < L then L = new_L else break. This is analogous to the right expansion from 1 but in the opposite direction. However, we can compute it efficiently.
Now, the key is to compute these expansions efficiently without O(N^2). Since the intervals are monotonic (L only decreases, R only increases), we can use a data structure to query min(i - L_i) over a range that expands. But maybe we can compute the final reachable intervals using a greedy sweep that is O(N) by precomputing prefix/suffix minima/maxima? Let's analyze.
For from 1 to right: We need to compute the maximum index reachable from 1. Starting with R = 1. Then we consider all i from 1 up to R, and compute the maximum of (i + L_i) among those i. If that maximum > R, then set R = that maximum, and continue. This is exactly the algorithm for the classic "jump game" where you can jump forward any distance up to a_i (here a_i = L_i) but you can also jump backward? But we only consider forward jumps because we start at leftmost and only care about reaching rightmost; backward jumps would not increase R beyond the maximum forward reach from any node in the current interval. However, note: In classic jump game (where you can only jump forward from i to any j with i < j <= i+a_i), the algorithm to compute if you can reach the end is: maintain furthest = 0, for i from 0 to n-1, if i > furthest break; else furthest = max(furthest, i + a_i). That works because you can only move forward. In our case, we can also move backward, but since we start at leftmost, backward moves only go to indices >=1, which are already within the interval. So they don't help to expand the right boundary beyond what we can get from forward jumps from some node. However, there is a nuance: In classic jump game, you can only use nodes in order of increasing index because you can only move forward; the algorithm that scans from left to right updating furthest works because you must reach nodes in order. In our scenario, we can also jump backward, so we could potentially reach a node that is to the left of the current furthest, but that node might have a larger forward reach than any node we have seen so far. But if that node is to the left of the current furthest, it is already within our interval [1, R] because R is the furthest we've reached. So we would have already considered it when computing max over i in [1,R]. Indeed, when we have interval [1,R], we consider all nodes in that interval. So any node that is left of R, even if we haven't "visited" it in a forward sequence, is still reachable because we can jump to it (maybe via backward jumps) and thus it's in the interval. So our process of updating R as max_{i in [1,R]} (i+L_i) is valid. And this process can be implemented by scanning from left to right while maintaining the current furthest reachable and updating it as we go, similar to classic jump game, but we need to consider that we can also jump to nodes left of current position, which could affect the set of nodes we have "covered". However, the classic algorithm also implicitly assumes that if you can reach index i, you have considered all nodes up to i. In our case, because we can also move backward, does that change the condition? Let's simulate: Initially, reachable set = {1}. Furthest = 1. Then we look at all nodes i from 1 to furthest (1). Among them, the max i+L_i = 1+L_1. Set furthest = max(furthest, that). Now furthest becomes maybe >1. Now, we need to consider nodes between the old furthest+1 and new furthest, because they become reachable. In classic algorithm, we would continue scanning i from 2 to new furthest, and update furthest accordingly. That works because when we reach i, we know that i is reachable (since we have a path to it) because all indices up to current furthest are reachable. This is true because in classic forward-only case, you can only reach indices in order, but still you can reach any index up to furthest because there is a path that eventually gets there? Actually in classic jump game, if you can reach index i, it's not guaranteed that all indices less than i are reachable; but the algorithm works because it processes indices in increasing order and only updates furthest based on indices that are known to be reachable (i <= furthest). In our case, with backward jumps, it is also true that if furthest is updated to a value R, then all indices <= R are reachable? We need to verify: If we have interval [1,R] reachable, then indeed every index between 1 and R is reachable (by the contiguity property). So if we maintain that all indices up to current furthest are reachable, then we can safely include them for consideration. The process of updating furthest by max(i+L_i) over i <= furthest will maintain the invariant that all indices up to furthest are reachable. Initially, only 1 is reachable. When we compute new furthest = max(i+L_i) for i in reachable set (which currently is all indices up to current furthest, since we assume contiguity), we get a new furthest. Now we need to argue that all indices between old furthest+1 and new furthest become reachable. Is that always true? Suppose we have some i0 in the reachable set such that i0+L_i0 = new furthest. Then from i0 we can jump directly to new furthest (or to some index >= new furthest, but capped at N). Since i0 is reachable, we can jump to any index j within its interval. In particular, we can jump to new furthest. Also, by contiguity, we can jump to any index between i0 and new furthest, so all indices up to new furthest become reachable. So indeed, the invariant holds. So the classic algorithm works: maintain a variable "rightmost" initially = 1. Iterate i from 1 to N, but only while i <= rightmost. For each i, compute rightmost = max(rightmost, i + L_i). If at any point rightmost >= N, we can stop and declare that N is reachable. This is O(N) if we simply loop.
But wait: In classic jump game, you are allowed to jump only forward, but here we can also jump backward. However, the algorithm as described does not need to consider backward jumps explicitly because backward jumps do not help extend the rightmost boundary; they only help to reach nodes that are already within the current rightmost (since they are leftwards). And our invariant that all indices up to rightmost are reachable might rely on the fact that from any reachable node we can also go left. But in the algorithm, we only consider nodes in increasing order; we never explicitly use backward jumps to prove that intermediate indices are reachable. But does the algorithm guarantee that all indices <= rightmost are reachable? Let's test with a potential counterexample where the classic jump game algorithm would say you can reach the end, but in our graph with backward jumps, maybe you cannot because you need to use backward jumps to activate a node that gives large forward jump, but that node might not be reachable without using backward jumps? However, the algorithm's condition i <= rightmost ensures that we consider a node i only if i is within the current reachable set? In classic jump game, the condition i <= rightmost is based on the fact that if you can reach up to rightmost, you can reach any index up to rightmost? Actually, in classic jump game (forward only), it's not true that you can reach every index less than rightmost. For example, consider array a = [3,0,0,2,0,0]. Starting at index 0 (1). rightmost = 0+3=3. Indices 1 and 2 are <= rightmost, but are they reachable? Index 1: from 0 you can jump to 1 because 0+1 <= 3? Actually, if you can jump from index 0 to any index <= 3, you can choose to jump to 1 directly. So yes, index 1 is reachable. Index 2 is also reachable directly. So all indices up to rightmost are reachable because from the start you can jump to any of them. But consider another example: a = [2,0,1,0,0]. Starting at 0, rightmost = 2. Index 2 is reachable? From 0 you can jump to 2 because 0+2 <= 2? Actually 0+2=2, yes you can jump to 2 directly. So reachable. Index 1 also reachable. So indeed, if you can jump from start to any index up to a certain distance, you can reach all indices within that distance because you can just jump directly to them. That's because the start node's interval covers a contiguous set from 1 to 1+L_1. In classic jump game, you can only jump forward, but you can still jump to any index j with 1 <= j <= 1+L_1. So initially, all indices up to 1+L_1 are reachable. Then from those indices, you can further extend. The algorithm's invariant is: all indices from 1 to rightmost are reachable. Initially rightmost = 1+L_1, and indeed all indices up to that are reachable. Then when we later update rightmost to a larger value via some i <= rightmost, we know that i is reachable, and from i we can jump to i+L_i, which may be beyond rightmost. Also, we need to ensure that all indices between old rightmost+1 and new rightmost become reachable. Since i is reachable and can jump directly to any index up to i+L_i, including those new indices, they become reachable. But are all indices in between also reachable? For any index j in (old rightmost, new rightmost], there exists i such that i+L_i >= j. However, does that guarantee that j is reachable? Possibly we need to argue that we can jump from i to j directly if j <= i+L_i. Since we have an i with i+L_i >= j, and i <= old rightmost, we can jump from i to j. So j is reachable. Thus, the invariant holds. So the algorithm works for our graph as well, because from any reachable node i, we can jump to any j within [i-L_i, i+L_i], not only forward. So the forward direction is covered.
Thus, to compute whether N is reachable from 1, we can simply run:
rightmost = 1
for i in 1..N:
if i > rightmost: break
rightmost = max(rightmost, i + L_i)
if rightmost >= N: reachable = True
But is this correct given that we can also use backward jumps to get to nodes that might allow larger forward reach but those nodes are to the left of current i? However, those nodes would already have been considered when i was <= rightmost earlier, because they are within the interval. For example, suppose we have L_1 small, but L_2 huge. Starting from 1: L_1=1 so rightmost becomes 2. Then i=2 is considered because i=2 <= rightmost=2, and we update rightmost with i+L_i = 2+huge => maybe N. So it's fine. What about a scenario where we need to go backward to access a node with large forward reach, but that node is to the left of some index we haven't visited yet? Since we start at 1, any node to the left of 1 doesn't exist. So not possible. So from 1, we never need to go backward to go right. So the forward scanning algorithm is correct.
Now, for reachability from N back to 1, we can do a symmetric backward scan:
leftmost = N
for i from N down to 1:
if i < leftmost: break
leftmost = min(leftmost, i - L_i)
if leftmost <= 1: reachable = True
But careful: In the backward direction, we need to consider that from N we can jump to any index j such that |N - j| <= L_N. That includes indices less than N. So the initial leftmost = min(N, N - L_N) = N - L_N (capped at 1). So leftmost = max(1, N - L_N). But in the scanning algorithm, we start with leftmost = N. Then iterate i from N downwards. For each i, if i >= leftmost (meaning i is within current reachable interval? Actually we need the invariant: all indices from leftmost to N are reachable. Initially, only N is reachable. But after considering i = N, we update leftmost = min(leftmost, N - L_N). That sets leftmost to N - L_N (capped? Actually we must consider that from N we can reach any index j with |N-j| <= L_N, i.e., j >= N - L_N, but also j must be >=1. So the leftmost reachable from N alone is max(1, N - L_N). However, our update leftmost = min(leftmost, i - L_i) will give N - L_N. But we haven't clamped to 1; but if N - L_N < 1, then min would be less than 1, but leftmost should not go below 1 because indices start at 1. So we need to cap at 1, but we can handle by taking max(1, i - L_i) when updating? But the algorithm as described may set leftmost to a value less than 1, and then the condition i < leftmost would be problematic. Let's adjust: We'll maintain leftmost as the smallest index that we know is reachable (so it's always between 1 and N). Initially leftmost = N. Then for i from N down to 1: if i >= leftmost (i is in the reachable interval), we update leftmost = min(leftmost, i - L_i). But note: i - L_i could be less than 1; in that case, the actual reachable leftmost becomes 1. So we should set leftmost = min(leftmost, max(1, i - L_i))? Or we can compute candidate = i - L_i, and then new leftmost = min(leftmost, candidate) but later we need to clamp to 1 because indices can't be below 1. Actually if candidate < 1, that means we can reach index 1 directly (since 1 is within range), so leftmost should become 1. So we can set leftmost = min(leftmost, candidate) and then after the loop we can clamp leftmost to at least 1? But careful: If candidate < 1, then min(leftmost, candidate) would be candidate (<1). But that would break the invariant that leftmost is a valid index. However, we can treat that as leftmost = 1 because we know we can reach 1. So we can adjust: leftmost = min(leftmost, i - L_i) and then after the update, if leftmost < 1, set leftmost = 1. But if leftmost becomes less than 1, it's effectively 1. So we can implement as: leftmost = min(leftmost, i - L_i); if leftmost < 1: leftmost = 1.
But does the scanning algorithm correctly compute the minimal reachable index? Let's test with example 3: N=4, L=[2,3,0,2]. Starting leftmost=4. i=4: i >= leftmost (4>=4) true. candidate = 4-2=2. leftmost = min(4,2)=2. leftmost stays 2. Next i=3: i=3 >= leftmost (3>=2) true. candidate = 3-0=3. leftmost = min(2,3)=2. No change. i=2: i=2 >=2 true. candidate = 2-3 = -1. leftmost = min(2, -1) = -1. Then we set leftmost = max(1, -1) =1? Actually we clamp to 1. So leftmost becomes 1. i=1: i=1 >= leftmost? leftmost is 1, so 1>=1 true. candidate = 1-2=-1. leftmost = min(1, -1) = -1 -> clamp to 1. So leftmost ends as 1. So algorithm yields leftmost=1, indicating 1 reachable from N. That matches.
But note: In the backward scanning, we need to iterate from N down to 1, but we can break early if leftmost becomes 1. Also we only need to consider i as long as i >= leftmost, because only those i are known to be reachable. This is symmetric to the forward case.
However, we must verify that the invariant holds: At any point, we maintain that all indices from leftmost to N are reachable. Initially, only N is reachable, so leftmost = N. That's consistent: reachable set = {N}. Actually, is it true that all indices between leftmost and N are reachable? Initially leftmost=N, so only N. That's fine. When we process i = N, we compute candidate = i - L_i, and if that is less than leftmost, we update leftmost. But we also need to ensure that all indices between new leftmost and old leftmost become reachable. Since from i (which is reachable), we can jump to any index j with |i-j| <= L_i, including those down to i - L_i. Thus, any index j in [i - L_i, i] becomes reachable. Since i is N, that means indices from max(1, N - L_N) up to N become reachable. So the new reachable set becomes [max(1, N - L_N), N]. So leftmost becomes max(1, N - L_N). So the invariant that reachable set is [leftmost, N] holds. Now, when we later process i = N-1, we only consider it if i >= leftmost (i.e., i is in the current reachable set). If i is reachable, then from i we can jump to indices down to i - L_i. So we can potentially lower leftmost further to min(leftmost, i - L_i). Also, we need to ensure that all indices between the new leftmost and old leftmost are reachable. Since i is reachable and can jump to any index down to i - L_i, and i - L_i might be less than current leftmost, then all indices from i - L_i up to i are reachable. Since i is within [leftmost, N], the union of current reachable [leftmost, N] and [i - L_i, i] becomes [min(leftmost, i - L_i), N]. So leftmost updates to min. So invariant holds. And we process indices in decreasing order ensures that we consider each index exactly once if it is reachable. The algorithm will correctly compute the minimal leftmost reachable from N.
Thus, both forward and backward scans are O(N).
But we need to ensure that the contiguity property holds for reachable set from N as well. Is it always contiguous? By symmetry, yes.
So the solution appears straightforward: Compute L_i = d_i // 10. Then:
rightmost = 1
for i in range(1, N+1):
if i > rightmost:
break
rightmost = max(rightmost, i + L_i)
if rightmost >= N: forward_ok = True; break
If forward_ok is False, answer "no".
If forward_ok, then compute:
leftmost = N
for i in range(N, 0, -1):
if i < leftmost:
break
leftmost = min(leftmost, i - L_i)
if leftmost <= 1: backward_ok = True; break
If backward_ok, answer "yes", else "no".
But is this always sufficient? Let's test with some tricky cases.
Consider N=5, L = [0, 4, 0, 0, 0]. Start at 1: L_1=0, so rightmost remains 1. i=1 >? rightmost=1, so loop continues. After i=1, we check i > rightmost? i=1 <= rightmost. Compute rightmost = max(1, 1+0)=1. Then i increments to 2, but i=2 > rightmost (1), so break. rightmost=1 < N, so forward fails. So no. That makes sense because from 1 you cannot jump anywhere, so cannot reach N.
Consider N=5, L = [1, 0, 0, 0, 0]. Starting from 1: rightmost becomes 2. Then i=2 (i<=rightmost?) i=2 <=2, so consider i=2: rightmost = max(2, 2+0)=2. Then i=3 > rightmost break. rightmost=2 <5, forward fails. So no.
Consider N=5, L = [2, 0, 0, 0, 3]. From 1: rightmost becomes 3 (1+2). i=2 (<=3) => rightmost remains 3, i=3 (<=3) => rightmost = max(3, 3+0)=3. Then i=4>3 break. rightmost=3<5, forward fails. So cannot reach 5. Indeed, from 1 you can reach up to 3, but from 3 cannot go further, and from 2 can't, so can't reach 5. But maybe by jumping backward? From 1 you can go to 2,3. From 3, you can only stay? Actually from 3, L_3=0, so cannot jump. So cannot. So forward fails.
Now, suppose there is a scenario where forward scan says you can reach N, but maybe you need to use backward jumps to actually get to a node that allows forward extension? But that node would be within the current interval anyway, because you can only go backward to indices that are already within the interval (since leftmost is 1). So forward scan will capture it. So it's fine.
Now, what about the return journey? Backward scan symmetric.
Edge Cases: When i+L_i can exceed N, we cap at N? Actually in the forward scan, rightmost = max(rightmost, i + L_i) might be > N. That's fine; we just compare rightmost >= N to determine success. But for the loop, we might break early if rightmost >= N. The loop variable i continues up to N but we break when i > rightmost. If rightmost becomes N or more, we can break early with success.
Similarly for backward.
Now, check example 2: N=3, L=[1,0,1]. Forward: i=1, rightmost=max(1,2)=2; i=2 <=2 => rightmost=max(2,2+0)=2; i=3? i=3 > rightmost (2) so break. rightmost=2 <3 => forward fails. So output no. correct.
Example 1: forward success, backward fails. output no. correct.
Example 3: forward success, backward success. output yes. correct.
Thus, algorithm seems correct.
But wait: Could there be a scenario where forward scan says reachable, but backward scan says not reachable, and overall answer is no? That's fine.
Now, consider something like N=5, L = [2, 0, 0, 0, 2]. Forward: from 1, rightmost=3; i=2,3 give no extension; rightmost=3<5 => fails. So no. But could we go from 1 to 5 via some path? From 1 cannot jump to 5 because distance 4*10=40 > 20? Actually L1=2 => max distance 20, so can only reach up to 3. So indeed unreachable.
What about N=5, L = [1, 0, 0, 2, 0]. Forward: from 1, rightmost=2; i=2 => rightmost stays 2; break. fails. But could we go 1->? Actually from 1 can go to 2 only. From 2 cannot jump (L2=0). So stuck. So fails.
What about a more complex: N=6, L = [1, 0, 0, 1, 0, 0]. Forward: 1->2 (rightmost=2). i=2 => no extension. break, fails. Indeed cannot reach beyond 2.
But consider a case where forward scan succeeds but maybe the path requires using a trampoline that is not simply in order? For example, start at 1, jump to 3, then back to 2, then to 5. But forward scan would capture: from 1, rightmost = 1+L1. If L1=2, rightmost=3. Then i=2 is considered (since 2 <=3) and might have L2 that extends further. So it's covered.
Thus, forward scan is essentially computing the rightmost index reachable from 1 using any sequence, and it's known to be correct for such interval reachability.
Now, for backward scan, we need to compute the leftmost index reachable from N. Similar reasoning.
Therefore, solution is O(N). We must handle up to 3e5, which is fine.
But let's double-check the initial assumption that reachable set from 1 is contiguous. Is it possible that you could reach a node far to the right without being able to reach some node in between, due to restrictions like you cannot land on certain trampolines because they might not allow a subsequent jump? But reachable means there exists a path from start to that node. If you can reach a far node j, you must have a path: 1 -> ... -> k -> j. At the step k -> j, since k can jump to j, it can also jump to any index between k and j. So those intermediate nodes are reachable directly from k. Thus they are reachable from 1 (via the same path to k and then the jump). So contiguity holds. So okay.
Edge Cases: d_i can be up to 1e6, so L_i up to 100000. That's fine; i+L_i might exceed N by a lot, but we just cap by N implicitly.
Implementation details: Input reading: N and then N lines of integers. Compute L_i = d_i // 10. Then perform two scans.
But is there any nuance about the requirement: "Starting from the left-most trampoline, the jumper jumps to another trampoline within the allowed jumping range. The jumper wants to repeat jumping until he/she reaches the right-most trampoline, and then tries to return to the left-most trampoline only through jumping. Can the jumper complete the roundtrip without a single stepping-down from a trampoline?" So the jumper must be able to go from 1 to N and then from N back to 1, but the path from N back to 1 must be possible after having arrived at N. However, the jumper's ability to jump from N depends only on d_N, not on the path taken to get there. So it's independent. So we just need to check both reachabilities.
But what about the possibility that the jumper might need to use a trampoline that gets "used up" or something? No, trampolines can be used any number of times; they are just platforms.
Thus, answer is yes if and only if 1 can reach N and N can reach 1.
Now, confirm with given examples.
Example 1: N=4, d=[20,5,10,1] => L=[2,0,1,0]. Forward: rightmost init 1. i=1: rightmost = max(1,1+2)=3; i=2 <=3: rightmost = max(3,2+0)=3; i=3: rightmost = max(3,3+1)=4; i=4? not needed because rightmost>=4 => forward success. Backward: leftmost init 4. i=4: leftmost = min(4,4-0)=4; i=3: i=3>=4? No, 3<4, break? Actually condition: i >= leftmost? At start leftmost=4, i=4 true, after update leftmost=4. Next i=3, check if i >= leftmost? 3 >= 4? false, so loop stops. So leftmost remains 4 >1, backward fails. Output no.
Example 2: forward fails, output no.
Example 3: forward success, backward: as computed, leftmost becomes 1. Output yes.
Works.
But wait, in example 1 backward, after i=4, leftmost stays 4, and i=3 is not considered because 3 < leftmost (4). But is it correct that from N=4 we cannot reach any left nodes? L_4=0, so indeed cannot jump anywhere. So leftmost stays 4. So break is correct. So condition i >= leftmost ensures we only consider nodes that are currently reachable. Initially only 4 is reachable. So we only process i=4, then leftmost doesn't change, so we break. Good.
Now, need to ensure that in backward loop, we process indices from N down to 1, but after updating leftmost to a lower value, we might need to process indices that are between the new leftmost and the old leftmost that we previously skipped because i was less than old leftmost. In the algorithm as described, we are iterating i from N downwards. When we encounter i that is less than current leftmost, we break. But if later leftmost decreases (due to processing a higher i), we might have skipped some i that are now >= the new leftmost but we already passed them (since we are iterating downwards). For example, suppose N=5. L_5 = 1, L_4 = 0, L_3 = 4. Let's compute manually. From N=5, reachable: from 5 can go to 4 (since 5-1=4). So reachable set becomes [4,5]. Then from 4, L_4=0 so cannot extend left. But from 3? 3 is not yet reachable. However, if we had considered i=3, from 3 we could go left to 1 (since 3-4=-1 => 1). But is 3 reachable from 5? Not directly, but maybe via some path? From 5 to 4, from 4 cannot go to 3. So 3 is not reachable. So leftmost remains 4. That's fine. But consider a scenario where a node with high leftward reach is reachable only after we expand leftwards to include it. For example: N=5, L_5 = 2 (so can reach 3,4,5). Then from 3, L_3 = 5 (can reach 1..5). So process: start leftmost=5. i=5: candidate = 5-2=3, leftmost becomes 3 (since min(5,3)=3). Now leftmost=3. i continues downwards: next i=4. i=4 >= leftmost (3) true, so we consider i=4. candidate = 4 - L_4 (maybe L_4 is something). If L_4=0, candidate=4, leftmost stays 3. Then i=3: i=3 >=3 true, candidate = 3-5=-2 -> leftmost becomes -2, clamp to 1. So we get leftmost=1. This works because we processed i=4 and then i=3 in order. Now, what if the order of processing caused us to skip i=3 because leftmost was initially 5, we processed i=5 and set leftmost=3. Then we continue to i=4, i=4 >= leftmost (3) true, process. Then i=3, i=3 >= leftmost (3) true, process. So fine.
But consider a scenario where the node that gives a big leftward expansion is at an index higher than some indices that we skip? Since we iterate from N downwards, and after updating leftmost to a smaller value, all indices between that new leftmost and the previous leftmost become reachable, but we have not yet processed those indices because they are lower in index. However, the loop continues to decreasing i, so we will encounter them because the next i is current i-1. For example, after processing i=5, leftmost becomes 3. The next i is 4, which is >=3, so we process it. Then next i=3, process. So all indices down to the new leftmost will be processed because the loop iterates downwards and we haven't passed them yet. That's because we update leftmost only when processing an index i that is currently in the reachable set. Since we process indices in decreasing order, when we decrease leftmost to a value L', the indices between L' and current i (which is decreasing) will be encountered later as we continue the loop. For instance, if we process i=5 and set leftmost=2, then next i=4,3,2 will all be >=2? Actually if leftmost=2, then when i=4, i>=2 true; i=3, i>=2 true; i=2, i>=2 true; so we process them. If we set leftmost to 1, then all subsequent i down to 1 are >=1, so we process them. So the algorithm works.
But what about the case where after processing i, leftmost becomes less than the next i? For example, start leftmost=5. i=5: update leftmost to 2. Next i=4, which is >2, so still i >= leftmost? 4 >=2 true, so we will process it. That's fine. So no problem.
Thus, the backward scan is correct.
Now, is there any scenario where the forward or backward scan might give a false positive? Let's test with random small N brute-force to ensure.
We'll later maybe test with python, but reasoning seems solid.
But wait: The problem says "maximum horizontal distance within which the jumper can jump safely." Does that mean the jumper can jump any distance up to that maximum? Yes.
And "Starting from the left-most trampoline, the jumper jumps to another trampoline within the allowed jumping range." So the first jump must be from trampoline 1 to some other within its range. And then repeats until reaches right-most, and then tries to return. So the path must start at 1 and end at N, and then from N back to 1. It does not require that the path from N back to 1 is possible without stepping down, but we check that separately.
Thus, our two conditions are necessary and sufficient.
Now, we should also consider that the jumper must jump to another trampoline, i.e., cannot stay on the same trampoline. But in our reachability analysis, we consider that you can jump to any trampoline within range, including possibly staying? Staying is not allowed, but staying wouldn't help anyway. So it's fine.
One more nuance: The distances are integer meters, and trampolines are at 10 m intervals. The maximum jump distance d_i is an integer. So if d_i = 9, then L_i = floor(9/10) = 0, meaning you cannot jump to any other trampoline because the minimum distance to any other trampoline is 10 > 9. So indeed cannot jump. So L_i=0 is possible.
Now, does our algorithm correctly handle L_i=0? Yes.
Now, test some edge cases:
Case: N=2. Two trampolines. d1 and d2. Need to be able to jump from 1 to 2 and back. Condition: from 1 to 2: need 10 <= d1, i.e., L1 >=1. From 2 to 1: need 10 <= d2, i.e., L2 >=1. So if both d_i >=10, output yes, else no. Our algorithm: forward: rightmost init 1. i=1: rightmost = max(1, 1+L1). If L1>=1, rightmost=2 >=2, forward success. Backward: leftmost init 2. i=2: leftmost = min(2, 2 - L2). If L2>=1, leftmost <=1? 2-1=1, leftmost becomes 1 <=1, so backward success. If L1=0, forward fails; if L2=0, forward may succeed (if L1>=1) but backward fails. So correct.
Case: N=300000, all d_i = 1e6, so L_i=100000. Then forward scan will quickly set rightmost = 1+100000, then continue scanning; essentially it will cover whole range quickly; O(N) still fine. Backward similarly.
Time complexity O(N), memory O(N) for storing L_i (or we could process on the fly? But we need L_i for both scans. We can store them in a list of length N.
Now, implement.
But we must be cautious: In forward scan, we break when i > rightmost, which means we cannot reach beyond that point. However, what if rightmost becomes >= N early, we break with success. That's fine.
In backward scan, we break when i < leftmost.
Edge case: If forward fails, we can output "no" immediately without backward scan. So we can do forward first, if not reachable, print "no". Else do backward, if reachable print "yes" else "no".
Now, is there any scenario where forward succeeds but backward fails, but by using a different path from 1 to N (maybe not the greedy expansion) we could enable a return? But backward condition depends only on d_i values, not on the path taken to reach N. So if N cannot reach 1, then no matter how you get to N, you can't get back. So it's a necessary condition.
Thus, solution seems correct.
But wait, think about the possibility that from 1 to N, you might need to use a trampoline that becomes "unavailable" for the return trip? No, they are reusable.
Thus, final answer.
Now, before finalizing, we should test with some more complex scenarios via mental simulation or quick reasoning.
Consider N=6, L = [1,0,0,2,0,0]. Forward: i=1 -> rightmost=2; i=2 -> rightmost=2; i=3? i=3 > rightmost(2) break. So rightmost=2 <6 => fail. But maybe there is a path: 1->2 (only), 2 can't jump, stuck. So fail. Good.
Consider N=6, L = [2,0,0,1,0,0]. Forward: i=1 -> rightmost=3; i=2 (<=3) -> rightmost= max(3,2)=3; i=3 -> rightmost= max(3,3)=3; i=4? i=4>3 break. So fails. Could there be a path using backward jumps? From 1 can go to 3. From 3, L_3=0 cannot jump. So stuck. So fail.
Consider N=6, L = [2,0,2,0,0,0]. Forward: i=1->rightmost=3; i=2->rightmost=3; i=3->rightmost= max(3,3+2=5)=5; now i=4? i=4 <=5 -> rightmost= max(5,4+0=4)=5; i=5 -> rightmost= max(5,5+0=5)=5; i=6? i=6>5 break. rightmost=5<6, fails. Could there be a path: 1->3 (since 1 can jump to 3, distance 20? L1=2 => max distance 20, 1 to 3 is 20, yes). From 3, L3=2 can jump to 5 (distance 20). So can reach 5. From 5, L5=0 cannot jump to 6. So cannot reach 6. So fail. Good.
Consider N=6, L = [2,0,2,0,1,0]. Forward: 1->3->5, from 5 L5=1 can jump to 6 (distance 10). So reachable. Let's test algorithm: i=1->rightmost=3; i=2->rightmost=3; i=3->rightmost= max(3,5)=5; i=4->rightmost=5; i=5->rightmost= max(5,6)=6 -> success. Good.
Now backward from 6: L6=0, so leftmost stays 6, fails. So overall no. That makes sense: from 6 cannot jump anywhere, so can't return.
Consider N=6, L = [2,0,2,0,1,2]. Forward success (as above). Backward: start leftmost=6. i=6: candidate = 6-2=4, leftmost=4. i=5: i=5>=4 true, candidate=5-1=4, leftmost stays 4. i=4: i=4>=4 true, candidate=4-0=4, leftmost stays 4. i=3: i=3>=4? 3<4 false, break. leftmost=4 >1, fails. So no. Could there be a path from 6 back to 1? 6 can jump to 4 (since L6=2, distance 20). From 4 L4=0 cannot jump. So cannot reach 1. So fails. Good.
Consider N=6, L = [2,0,2,2,1,2]. Forward: from 1->3->? 3 can jump to 5 (since L3=2, 3 to 5 is 20) or 3 to 1,3,5. Also from 3 can jump to 4? distance 10, yes if L3>=1. So 3 can jump to 4. But forward algorithm: i=1->rightmost=3; i=2->rightmost=3; i=3->rightmost = max(3,5)=5; i=4 (<=5) -> candidate 4+2=6, so rightmost becomes 6; success. So forward ok. Backward: start leftmost=6, i=6: candidate=6-2=4, leftmost=4. i=5: 5>=4 true, candidate=5-1=4, leftmost=4. i=4: 4>=4 true, candidate=4-2=2, leftmost=2. i=3: 3>=2 true, candidate=3-2=1, leftmost=1. Success. So overall yes. Let's verify: from 6 can jump to 4; from 4 can jump to 2 (L4=2, 4 to 2 distance 20); from 2 L2=0 cannot jump further. But we need to reach 1. From 4 can jump to 2, but 2 cannot jump to 1 because L2=0. However, from 4 we can also jump to 3? 4 to 3 distance 10, L4=2 so yes. Then from 3, L3=2 can jump to 1 (distance 20). So path: 6->4->3->1 works. So backward reachable. Good.
Thus, algorithm seems consistent.
Now, we should also consider if there is any trick about the jump distances being exactly d_i, not floor(d_i/10). Since trampolines are 10m apart, the distance between i and j is 10*|i-j|. The condition for a safe jump is that this distance is within the maximum allowable jumping distance d_i. So we need 10*|i-j| <= d_i. That's equivalent to |i-j| <= d_i / 10. Since d_i integer, this is equivalent to |i-j| <= floor(d_i/10). So our L_i = d_i // 10 is correct. But careful: if d_i is not a multiple of 10, floor division gives the integer part. For example, d_i = 15, then floor(15/10)=1, meaning you can jump up to 10m, but 15m is not a multiple of 10? Actually distance to next trampoline is 10m, to second next is 20m. So with d_i=15, you can jump up to 15m, which allows jumping to the immediate neighbor (10m) but not to the second neighbor (20m). So L_i=1 is correct. So integer division works.
Now, confirm with d_i values that are less than 10: L_i=0, cannot jump.
Thus, solution stands.
Now, let's think about potential pitfalls: The input size N up to 300k, d_i up to 1e6, so L_i up to 100k. Summing or using large numbers is fine.
Now, regarding the backward scan: we used condition "if i < leftmost: break". But we also need to consider that after updating leftmost to a value less than 1, we clamp. But what if leftmost becomes 0 or negative before clamping? That may cause the condition i < leftmost to be always false? For example, after processing i, we set leftmost = min(leftmost, i - L_i). If i - L_i < 1, leftmost becomes a negative number. Then we continue loop; next i is i-1, which might be greater than leftmost? Actually if leftmost is negative, i (which is positive) will be > leftmost, so condition i < leftmost is false, so we won't break. But we want to break only when i < leftmost, meaning i is less than the current leftmost (which is the smallest reachable index). If leftmost becomes negative, that would indicate we can reach index 1 (since we clamp to 1). But our leftmost variable should be kept as the actual smallest index reachable, which is at least 1. So we should clamp immediately: if leftmost < 1: leftmost = 1. Then the condition i < leftmost would be i < 1, which for i >=1 is false only when i >=1, so loop continues until i becomes 0 (but we stop at i=1). However, after leftmost becomes 1, we still need to process remaining i down to 1? Actually if leftmost becomes 1, we have already achieved the goal, but we might still need to process further i to see if leftmost can go below 1? It can't go below 1. But we can break early if leftmost <= 1. So we can add a check after update: if leftmost <= 1: success and break. That would be efficient.
In the forward scan, we similarly break if rightmost >= N.
Thus, final algorithm:
Read N.
Read d_i list.
Compute L_i = d_i // 10.
Forward
rightmost = 1
forward_ok = False
for i in range(N):
if i+1 > rightmost:
break
rightmost = max(rightmost, i+1 + L_i[i])
if rightmost >= N:
forward_ok = True
break
if not forward_ok:
print("no")
exit()
Backward
leftmost = N
backward_ok = False
for i in range(N-1, -1, -1):
if i+1 < leftmost:
break
leftmost = min(leftmost, i+1 - L_i[i])
if leftmost <= 1:
backward_ok = True
break
print("yes" if backward_ok else "no")
But careful: In the backward loop, condition "if i+1 < leftmost" means the current index is less than the smallest reachable index, so it's not reachable and we can break because we won't encounter any higher index that is reachable? Actually as we go decreasing i, if we encounter an i that is less than leftmost, that means this i is not in the reachable interval. Since we are moving leftwards, any further i will be even smaller, so they will also be less than leftmost, and thus not reachable. So we can break.
But is it possible that later we might expand leftmost to be even smaller, making some of those indices reachable? To expand leftmost, we need to process a reachable index that can jump left further. However, if we break when i < leftmost, we haven't processed those indices because they are not reachable. But could there be a reachable index that is less than the current leftmost? By definition, leftmost is the smallest reachable index. So any index less than leftmost is not reachable. So we won't miss it because it's not reachable. However, could processing a later (actually earlier in loop, i.e., larger index) index expand leftmost to be less than current i? But we process indices in decreasing order. At a given step, leftmost is the current smallest reachable index. If we are at index i, and i >= leftmost, we process it and may update leftmost to a smaller value. Then we continue to i-1. If i-1 is still >= the new leftmost, we will process it. If at some point i < leftmost, that means i is not reachable, and since leftmost is not going to increase (it only decreases or stays same), i will remain unreachable. So break is safe.
But note: The condition to break is i < leftmost. However, what if leftmost gets updated to a value that is less than the current i but we haven't yet processed the current i? Actually we process i only if i >= leftmost. So when we update leftmost to a smaller value, it's after processing i. The next iteration i-1, we compare i-1 with leftmost (which may now be smaller). So i-1 may still be >= leftmost, so we continue. So no break prematurely.
Thus, the backward loop is correct.
Now, we need to be careful about indexing: In Python, arrays are 0-indexed. So trampoline indices are 1..N. So we store L in a 0-indexed list L[0] corresponds to trampoline 1. In forward loop: for i in range(N): idx = i+1. So condition: if idx > rightmost: break. Update: rightmost = max(rightmost, idx + L[i]). Similarly backward: for i in range(N-1, -1, -1): idx = i+1. if idx < leftmost: break. leftmost = min(leftmost, idx - L[i]). If leftmost <= 1: success.
Now, one more nuance: In forward, rightmost is initialized to 1. The loop starts at i=0 (idx=1). Since idx <= rightmost (1<=1), we process. Good.
In backward, leftmost initialized to N. Loop starts at i=N-1 (idx=N). idx >= leftmost (N>=N) true, process.
Now, check example 1 with code: L=[2,0,1,0]. Forward: i=0: idx=1, rightmost = max(1,1+2)=3; i=1: idx=2 <=3, rightmost = max(3,2+0)=3; i=2: idx=3 <=3, rightmost = max(3,3+1)=4; now rightmost>=4 -> success. Backward: leftmost=4. i=3: idx=4 >=4, leftmost = min(4,4-0)=4; i=2: idx=3, check idx < leftmost? 3<4 true, break. leftmost=4, not <=1, so backward fails. Output no.
Works.
Now, is there any scenario where forward scan might incorrectly say success even though N is not reachable due to needing to go left first? Consider this: To reach a far right node, you might need to go left from the start to a node with large forward reach, but that left node would be index <1? Impossible. Or maybe you need to go right a bit, then back left to a node that you previously passed, and then forward further. But that left node is still within the interval you have already covered. So the forward scan covers it because it considers all indices up to current rightmost. For example, start at 1, L1=1 so you can go to 2. Now from 2, L2=0, so cannot go further. But suppose there is a node 3 that has L3=2, but you cannot reach 3 directly from 1 because distance 20 > L1*10=10. Could you get to 3 by going 1->2->? 2 cannot jump. So no. So unreachable.
What about: 1 can jump to 3, but 3 requires to first go to 2 and then to 3? Actually 1 can jump directly to 3 if L1>=2. So that's covered.
What about: 1 can jump to 2, 2 can jump to 0? Not possible.
Thus, seems fine.
Now, what about the possibility that forward scan might indicate success but actually you cannot reach N because you might need to avoid landing on a trampoline that is a dead end? But if you can reach N, you don't care about dead ends; you just need a path. The forward scan essentially computes the set of indices reachable from 1. If N is in that set, there exists some path. The algorithm that expands rightmost by considering max(i+L_i) for i in the interval indeed computes the maximum index reachable, but does it guarantee that N is reachable if rightmost >= N? Yes, because if rightmost >= N, then there exists some i in the interval such that i + L_i >= N, meaning from i you can jump directly to N (or beyond). Since i is reachable, N is reachable. So yes.
Thus, algorithm is correct.
Now, confirm with a corner case: N=1? But N >=2 per constraints (2 ≤ N ≤ 3e5). So no need.
Now, edge: d_i could be very large, i+L_i may overflow Python int? Python ints arbitrary, fine.
Now, consider potential pitfalls: The forward scan might break early if i > rightmost, but rightmost might later be updated by some i that is <= rightmost but we haven't reached it because we broke? No, because we break when i > rightmost, and we only increment i. So if at some point i > rightmost, that means we have processed all indices up to rightmost, and rightmost didn't increase enough to cover i. Since i increases by 1 each iteration, if rightmost < i, then for all future i, i will be > rightmost because i increases. So no chance to expand further. So break is correct.
Similarly for backward.
Now, finalize solution.
But wait: There is a nuance: In the forward scan, we update rightmost as max(rightmost, i+L_i). However, i+L_i might be less than i? No, L_i >=0, so i+L_i >= i.
Now, we must ensure that we consider that jumping is to another trampoline; you cannot stay. But does that affect reachability? For the purpose of reaching N, it's fine if the path includes staying? Not allowed. But we never need to stay; we always jump to a different trampoline. In the expansion, when we say from i we can go to i+L_i (if i+L_i > i), that's a different trampoline as long as L_i >0. If L_i=0, then i+L_i = i, which would be staying, but we cannot use that as a jump. However, in our algorithm, when we compute max(i+L_i) for i in the interval, if L_i=0, then i+L_i = i, which may not help expand rightmost (since rightmost already >= i). But if L_i=0 and i is the only node, and we are considering forward from 1 with L_1=0, then rightmost stays 1, which is correct because you cannot jump anywhere. So using i+L_i = i is fine because it doesn't increase rightmost. So no issue.
But what about the case where the only way to extend rightmost is via a node with L_i=0? That can't extend because i+L_i = i. So fine.
Thus, algorithm stands.
Now, time to write final solution.
But before finalizing, let's double-check with some random brute-force for small N to ensure no missed conditions. Since we are reasoning, we could also think about if there is any scenario where forward scan says N is reachable, but due to the requirement that you must start at left-most and only jump to another trampoline (can't stay), maybe the path might require using a trampoline that is a dead end after you leave it, but that doesn't matter. There's no constraint on using trampolines multiple times or visiting dead ends as long as you can proceed.
Thus, it's fine.
Now, consider if the jumper could jump over the right-most trampoline? No, only to existing trampolines.
Now, think about possible misinterpretation: The problem might require that the jumper must always jump to a trampoline within the allowed range, and he wants to repeat jumping until he reaches the right-most trampoline, and then tries to return to the left-most trampoline only through jumping. It does not require that the path from 1 to N is a simple path or that the jumper cannot visit other trampolines after reaching N before returning. But we only need existence.
Thus, our solution is correct.
Now, we can also note that we only need to compute L_i once. Memory O(N). Time O(N).
Now, let's produce final answer.
But wait, I recall a known problem "Trampoline" from some contest? Might be something else. But our reasoning seems solid.
However, let's consider if there is any scenario where the forward scan fails but actually there is a path that goes left first then right? Starting at leftmost 1, going left is impossible because there's no trampoline to the left. So no.
Thus, we are done.
But maybe we need to consider that the jumper could jump from 1 to some trampoline to the right, then back to a trampoline left of 1? Impossible. So forward is just about expanding to the right.
But wait: What if the forward path requires visiting a trampoline that is to the left of the current position but still within the overall interval? For example, from 1 you can go to 3, then from 3 you can go back to 2, then from 2 you can go to 5. Our forward algorithm would capture that because from 1, rightmost becomes 3. Then i=2 is considered (since 2<=3) and from 2 we can go to 5 if L_2 is large. So yes.
What if the path is 1->4 (if L1 large enough to directly go to 4), then from 4 go back to 2, then from 2 go to 6? That still captured because from 1, rightmost becomes 4 (if L1>=3). Then i=2,3,4 are considered, and i=2 might have large L_2. So fine.
Thus, no need for extra complexity.
Now, potential overflow: i+L_i can be up to 300k + 100k = 400k, fine.
Now, consider if d_i could be so large that i+L_i exceeds maximum integer? Python fine.
Thus, implement.
Let's produce final solution in Python.
Pseudo:
def solve():
import sys
input = sys.stdin.readline
N = int(input().strip())
d = [int(input().strip()) for _ in range(N)]
L = [x // 10 for x in d]
# forward
rightmost = 1
forward_ok = False
for i in range(N):
idx = i + 1
if idx > rightmost:
break
rightmost = max(rightmost, idx + L[i])
if rightmost >= N:
forward_ok = True
break
if not forward_ok:
print("no")
return
# backward
leftmost = N
backward_ok = False
for i in range(N-1, -1, -1):
idx = i + 1
if idx < leftmost:
break
leftmost = min(leftmost, idx - L[i])
if leftmost <= 1:
backward_ok = True
break
print("yes" if backward_ok else "no")
if name == "main":
solve()
Test with examples.
Example 1: N=4, L=[2,0,1,0] -> forward_ok true, backward_ok false -> no.
Example 2: N=3, L=[1,0,1] -> forward fails -> no.
Example 3: N=4, L=[2,3,0,2] -> forward success, backward success -> yes.
Seems correct.
Now, is there any scenario where forward_ok true but backward_ok false, but still the roundtrip is possible because you could choose a different path from N to 1 that relies on trampolines that are not reachable from N in the initial state but become reachable after you have traveled from 1 to N? The condition for return is: after reaching N, the jumper is on N and wants to return to 1. The ability to jump from N depends only on d_N (and then from subsequent trampolines, their d_i). It does not depend on how you got to N. The set of trampolines available is fixed. So if there is no directed path from N to 1 in the graph, you cannot return. However, is it possible that the path from N to 1 uses some trampolines that were "used up" or something? No. So backward reachability is independent.
Thus, condition is correct.
Double-check: Could there be a scenario where forward scan says N is reachable, but the jumper cannot actually land on N because the last jump must be exactly onto N, and the jumper might overshoot? But overshooting is not allowed because you can only land on existing trampolines. So you must land exactly on N. From some i, you can jump to any j within [i - L_i, i + L_i]. If i + L_i >= N, you can jump directly to N because N is within that interval, provided N >= i - L_i as well, but that's automatically true if N >= i. So you can jump to N. So reachable.
Thus, finalize.
But let's also consider potential off-by-one errors: In forward scan, we start with rightmost=1, loop i=0 (idx=1). For each i, we check idx > rightmost before processing. This ensures we only process indices that are currently reachable. However, what about the case where rightmost is updated within the loop to a value greater than current i+1, and then the next iteration i+1 may still be <= the new rightmost. That's fine because we continue loop. The condition at start of each iteration checks if the current index (which we haven't processed yet) is reachable. Since we processed previous indices, we know that all indices up to current i (since i increases) are reachable? Actually not necessarily: The invariant is that all indices from 1 to rightmost are reachable. At iteration i (0-indexed), we have processed indices 0..i-1. The current index i may be > rightmost if rightmost hasn't expanded enough to cover it. If it's > rightmost, we break. If it's <= rightmost, then we know it is reachable (by the invariant). So it's safe to use its L_i to possibly expand rightmost. After processing i, we update rightmost, and the invariant holds for indices up to new rightmost. Then i increments, and we check again.
This is classic.
Now, for backward: invariant: all indices from leftmost to N are reachable. At each iteration, we process index i (decreasing). If idx < leftmost, then it's not reachable, and we break. If idx >= leftmost, it is reachable, so we use its L_i to possibly reduce leftmost. After update, invariant holds.
Thus, correct.
Now, let's test a tricky case: N=5, L = [0, 3, 0, 0, 0]. Starting from 1, L1=0, so rightmost stays 1. Loop: i=0 idx=1 <=1, process: rightmost = max(1,1+0)=1. i=1 idx=2 > rightmost (1) break. So forward fails. Indeed, cannot move from 1.
What about L = [1, 0, 0, 0, 0]? Forward: rightmost becomes 2; i=1 idx=2 <=2, process: rightmost stays 2; i=2 idx=3 >2 break. So fails. Correct.
Now, test a case where forward passes but backward fails: L = [2, 0, 0, 0, 1]. N=5. Forward: i=0: rightmost = 3; i=1: idx=2<=3 -> rightmost stays 3; i=2: idx=3<=3 -> rightmost stays 3; i=3: idx=4>3 break? Actually after i=2, rightmost=3, i becomes 3, idx=4 >3, break. So forward fails? Wait, but from 1 we can jump to 3? L1=2 => can jump to 3. From 3, L3=0 cannot jump. So cannot reach 5. Indeed forward fails. But suppose L = [2,0,0,0,2]. Forward: i=0: rightmost=3; i=1: idx=2<=3 -> no change; i=2: idx=3<=3 -> no change; i=3: idx=4>3 break -> fails. But from 1 can jump to 3, from 3 cannot jump to 5 because L3=0. So fails. But L = [2,0,0,1,2] maybe? Let's try: L = [2,0,0,1,2]. N=5. Forward: i=0: rightmost=3; i=1: idx=2 <=3, no change; i=2: idx=3 <=3, no change; i=3: idx=4 <=3? 4>3 break, so never considers i=3 because idx=4 > rightmost=3. But is i=3 reachable? At this point, reachable set is [1,3]. Index 4 is not reachable. So algorithm breaks. But could there be a path to 5? From 1 to 3, from 3 cannot go to 4 (L3=0). So cannot. So correct.
Now, what about L = [2,0,1,0,2]? Forward: i=0: rightmost=3; i=1: idx=2 <=3; no change; i=2: idx=3 <=3, rightmost = max(3,3+1=4) => rightmost=4; now i=3: idx=4 <=4, process: rightmost = max(4,4+0=4)=4; i=4: idx=5 >4 break? Actually after i=3, rightmost=4, i becomes 4 idx=5 >4 break. So forward fails? But can we reach 5? From 1 to 3 (since L1=2), from 3 to 4 (L3=1), from 4 to ? L4=0 cannot jump to 5. So fails. So algorithm says fails. Correct.
What about L = [2,0,1,1,2]? Forward: i=0: rightmost=3; i=1: no change; i=2: rightmost = max(3,4)=4; i=3: idx=4 <=4, rightmost = max(4,4+1=5)=5; now rightmost>=5, success. So forward ok. Backward: leftmost=5; i=4: idx=5, leftmost = min(5,5-2=3)=3; i=3: idx=4 >=3, leftmost = min(3,4-1=3)=3; i=2: idx=3 >=3, leftmost = min(3,3-1=2)=2; i=1: idx=2 >=2, leftmost = min(2,2-0=2)=2; i=0: idx=1 <2? idx=1 < leftmost(2) -> break. leftmost=2 >1, backward fails. So overall no. Check manually: From 5 can jump to 3 (since L5=2, distance 20). From 3 can jump to 2 (L3=1). From 2 L2=0 cannot jump to 1. Could there be another path: 5->4 (L5=2 -> 4), from 4 L4=1 -> 3, from 3 L3=1 ->2, from 2 L2=0 -> stop. So cannot reach 1. So fails. So algorithm correct.
Thus, we are confident.
Now, consider a scenario where the backward scan might incorrectly break because leftmost becomes less than some i that we have already processed? But we process in decreasing order, so it's fine.
Now, final code.
But before finishing, double-check reading input: There are N lines, each containing an integer d_i. We'll read them using sys.stdin. We'll strip newline.
Now, edge: N could be large; we need efficient I/O. Using sys.stdin.readline for each line is fine.
Now, is there any case where we need to consider that the jumper must jump to another trampoline, meaning the path cannot have consecutive jumps that land on the same trampoline? But that would be useless anyway, and we never need to do that.
Thus, final answer.
But wait, let's read problem statement again: "A plurality of trampolines are arranged in a line at 10 m intervals. Each trampoline has its own maximum horizontal distance within which the jumper can jump safely. Starting from the left-most trampoline, the jumper jumps to another trampoline within the allowed jumping range. The jumper wants to repeat jumping until he/she reaches the right-most trampoline, and then tries to return to the left-most trampoline only through jumping. Can the jumper complete the roundtrip without a single stepping-down from a trampoline?" This description might be interpreted differently: Perhaps the jumper can only jump to trampolines that are within the maximum distance, but also must land exactly on a trampoline; that we accounted.
But one could misinterpret: The maximum horizontal distance is the distance the jumper can achieve from that trampoline, but maybe the jumper can choose any distance up to that maximum, not necessarily to another trampoline exactly at that distance? But since trampolines are discrete points, the jumper can only land on a trampoline. So the condition is that the distance to the target trampoline must be <= d_i. That's what we used.
Now, maybe there is nuance: The jumper can jump to any point within the maximum distance, not necessarily exactly to a trampoline? But "jumps to another trampoline" implies target must be a trampoline. So we're good.
Now, confirm that the distance between trampolines is exactly 10 m, so the index difference times 10 is the Euclidean distance.
Thus, solution is final.
Now, we might consider that the forward and backward scans could be combined into one pass? Not necessary.
Now, produce final answer.
But before finalizing, I want to test with a scenario where N is large and d_i values cause rightmost to expand gradually; algorithm will be linear.
Now, the answer should be "yes" or "no" in lowercase as per examples.
Now, I'll write the final solution.
One more thought: What if d_i is such that L_i = d_i // 10 but d_i can be 0? According to constraints, d_i >= 1, so L_i >= 0.
Now, final code.
But let's also consider that the problem might be from a contest that expects an O(N) solution. Our solution is O(N). Good.
Now, we can also think about an alternative approach: compute the maximum index reachable from 1 and minimum index reachable from N. If max from 1 >= N and min from N <= 1, output yes.
Now, we are done.
Now, I'll write final answer with explanation. ```python
import sys
def solve() -> None:
it = iter(sys.stdin.read().strip().split())
try:
N = int(next(it))
except StopIteration:
return
d = [int(next(it)) for _ in range(N)]
# maximal number of 10‑meter steps the jumper can cover from each trampoline
L = [x // 10 for x in d]
# ---------- forward : can we go from 1 to N ? ----------
rightmost = 1 # currently we can reach every index in [1, rightmost]
forward_ok = False
for i in range(N):
idx = i + 1
if idx > rightmost: # idx is not reachable → stop
break
rightmost = max(rightmost, idx + L[i])
if rightmost >= N:
forward_ok = True
break
if not forward_ok:
print("no")
return
# ---------- backward : can we go from N to 1 ? ----------
leftmost = N # currently we can reach every index in [leftmost, N]
backward_ok = False
for i in range(N - 1, -1, -1):
idx = i + 1
if idx < leftmost: # idx is not reachable → stop
break
leftmost = min(leftmost, idx - L[i])
if leftmost <= 1:
backward_ok = True
break
print("yes" if backward_ok else "no")
if name == "main":
solve()