Frog Traveler
Contest: Codeforces Round 751 (Div. 1)
Difficulty rating: 1900
Tags: data structures, dfs and similar, dp, graphs, shortest paths, two pointers

Frog Gorf is traveling through Swamp kingdom. Unfortunately, after a poor jump, he fell into a well of $$$n$$$ meters depth. Now Gorf is on the bottom of the well and has a long way up.

The surface of the well's walls vary in quality: somewhere they are slippery, but somewhere have convenient ledges. In other words, if Gorf is on $$$x$$$ meters below ground level, then in one jump he can go up on any integer distance from $$$0$$$ to $$$a_x$$$ meters inclusive. (Note that Gorf can't jump down, only up).

Unfortunately, Gorf has to take a break after each jump (including jump on $$$0$$$ meters). And after jumping up to position $$$x$$$ meters below ground level, he'll slip exactly $$$b_x$$$ meters down while resting.

Calculate the minimum number of jumps Gorf needs to reach ground level.

Input format:
The first line contains a single integer $$$n$$$ ($$$1 \le n \le 300\,000$$$) — the depth of the well.

The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le i$$$), where $$$a_i$$$ is the maximum height Gorf can jump from $$$i$$$ meters below ground level.

The third line contains $$$n$$$ integers $$$b_1, b_2, \ldots, b_n$$$ ($$$0 \le b_i \le n - i$$$), where $$$b_i$$$ is the distance Gorf will slip down if he takes a break on $$$i$$$ meters below ground level.

Output format:
If Gorf can't reach ground level, print $$$-1$$$. Otherwise, firstly print integer $$$k$$$ — the minimum possible number of jumps.

Then print the sequence $$$d_1,\,d_2,\,\ldots,\,d_k$$$ where $$$d_j$$$ is the depth Gorf'll reach after the $$$j$$$-th jump, but before he'll slip down during the break. Ground level is equal to $$$0$$$.

If there are multiple answers, print any of them.

Constraints:
Time limit: 2.0 s
Memory limit: 512.0 MB

Note:
In the first example, Gorf is on the bottom of the well and jump to the height $$$1$$$ meter below ground level. After that he slip down by meter and stays on height $$$2$$$ meters below ground level. Now, from here, he can reach ground level in one jump.

In the second example, Gorf can jump to one meter below ground level, but will slip down back to the bottom of the well. That's why he can't reach ground level.

In the third example, Gorf can reach ground level only from the height $$$5$$$ meters below the ground level. And Gorf can reach this height using a series of jumps $$$10 \Rightarrow 9 \dashrightarrow 9 \Rightarrow 4 \dashrightarrow 5$$$ where $$$\Rightarrow$$$ is the jump and $$$\dashrightarrow$$$ is slipping during breaks.

Example input:
3
0 2 2
1 1 0

Example output:
2
1 0

Example input:
2
1 1
1 0

Example output:
-1

Editorial:
Let's denote sequence of moves $$$i \Rightarrow i - a_i \dashrightarrow i - a_i + b_{i-a_i}$$$ as jump.
We will use $$$dp_i$$$ — minimal number of moves needed to travel from $$$i$$$ to $$$0$$$.
It can be calculated $$$dp_i = 1 + \min (dp_j + b_j)$$$, with $$$i - a_i \le j \le i$$$. We expected calculations to use bfs-style order. So, if there is a jump to $$$0$$$, $$$dp$$$ is $$$1$$$. If there is no jump to $$$0$$$, but there is a jump to position with $$$dp=1$$$, then $$$dp$$$ is $$$2$$$, and so on.
What happens, when we know all dp's with values from $$$0$$$ to $$$d$$$? We'll take position $$$v$$$ ($$$dp_v = d$$$) and all $$$u$$$ with condition $$$u + b_u = v$$$. Then all $$$j$$$ that has $$$j - a_j \le u \le j$$$ we know for sure $$$dp_j = d + 1$$$. For every $$$i$$$ we will save in minimum segment tree value $$$i - a_i$$$. So, all $$$j$$$'s are just elements from a suffix with value not greater than $$$u$$$.
We can iterate through all $$$j$$$'s, because every of them is used only once — right after we know $$$dp_j$$$, we can use any neutral value (infinity in our case).
Time complexity is $$$O(n \log n)$$$
Bonus. Try to solve it in linear time :)