Problem D
Inchworm
Ivy the inchworm is on a tree and wants to climb as high as possible. She has two legs, a front leg and a back leg.1 Ivy can only move one leg at a time (while the other leg remains stationary) while keeping her legs between $n$ and $m$ units apart, inclusive, and her front leg must always be above her back leg. She climbs the tree in a straight line starting with her back leg on the ground, and only certain points on the tree can be used as footholds. Ivy can place her legs only on those points. She may move up or down the tree, as long as she maintains proper distance between her legs.
What is the maximum height that Ivy can reach with her front leg?
Input
The first line has three integers, $k$ ($3 \leq k \leq 3\times 10^5$), $n$, and $m$ ($1 \leq n < m \leq 3\times 10^5$), which are the number of points, the minimum distance between Ivy’s legs, and the maximum distance between Ivy’s legs respectively.
The second line is a list of $k$ integers, $h_1 < h_2 < \cdots < h_k \leq 10^9$, which are the heights of the usable footholds. It is guaranteed that $h_1 = 0$.
The third line has two integers, $s_b$ and $s_f$, which are the starting positions of Ivy’s back and front legs respectively. It is guaranteed that $s_b = 0$, $n \leq s_f \leq m$, and that $s_f = h_i$ for some $i$.
Output
Print a single integer, the maximum height that Ivy can reach with her front leg.
| Sample Input 1 | Sample Output 1 |
|---|---|
5 10 20 0 6 17 26 54 0 17 |
26 |
| Sample Input 2 | Sample Output 2 |
|---|---|
4 6 7 0 3 7 9 0 7 |
7 |
| Sample Input 3 | Sample Output 3 |
|---|---|
7 4 6 0 2 4 6 8 10 12 0 6 |
12 |
Footnotes
- In reality, inchworms have two clusters of legs, but as each cluster is stuck together, we can treat them as a single leg.
