{
    \parindent=1cm
    \begin{tabular}{l@{\extracolsep{1cm}}l}
         Idea: & Mikhail Ivanov\\
         Development: & Mikhail Ivanov\\
     \end{tabular}
}

Without loss of generality, let $h > s$. Let's imagine the optimal route for Katniss; what will it look like? Katniss will alternate between walking left and walking right. It is clear that if she reaches some point at the end of a left walk, the next left walk must end strictly further away. Indeed, why would Katniss need to go left otherwise? If it was to pick up a key or exit through a hatch, she could have done that during the previous walk. And there could be no other reasons~--- Katniss could not pick locks in this segment, as they had all been opened earlier. Furthermore, it is clear that the very last walk will end at $h$, and it will be a right walk; otherwise, Katniss could have left the tunnel earlier. Formally, Katniss will choose some segment of the tunnel $[L; R]$, containing both $h$ and $s$, select several points $L_m$, $R_m$, $L_{m-1}$, $R_{m-1}$, $L_{m-2}$, $R_{m-2}$, \ldots, $L_0$, $R_0$, and visit them in this order, opening all locks and picking up all keys from the segment $[L; R]$ along the way. Here, $L = L_0 < L_1 < \ldots < L_m \le s \le R_m < R_{m-1} < R_{m-2} < \ldots < R_0 = R = h$.

Now we just need to choose these segments and these points. Let's do this gradually from the end. Choosing $R_0$ is easy: it is simply $h$. How do we choose $L_0$ now? To reach $R_0$, Katniss must have all the keys for the locks in the segment $[s; R_0]$. Let's consider all of these keys that are to the left of $s$, and choose the leftmost such key~--- its position will be the point $L_0$. Next, similarly: to reach $L_0$, all locks in $[L_0; s]$ whose keys are to the right of $s$ must be openable; thus, we should take the rightmost key that opens a lock in $[L_0; s]$ as $R_1$. We continue doing this until we find that there are no locks on the current segment that require keys from the other half of the tunnel; then we simply declare that this current segment will be the first segment we will traverse.

It may happen that such a moment never arrives. This will be the case if we find for some $j$ that $L_j \ge L_{j + 1}$ or $R_j \le R_{j + 1}$~--- this definitely means that there is a cyclic dependency. Specifically, let's say that a lock \textit{locks} a hatch/key (possibly opening another lock) if the lock is located between the given hatch/key and $s$. It is clear that Katniss will not be able to pick up the key before opening all the locks that lock this key, and she will not be able to exit through the hatch until she opens all the locks that lock the hatch. An inequality like $R_j \le R_{j + 1}$ guarantees that there is a pair $i, j$ such that the $i$-th lock locks the $j$-th key, and the $j$-th lock locks the $i$-th key, with one of the locks locking the hatch. Then Katniss will not be able to open any of the locks without first opening another one~--- this is what we previously called a cyclic dependency. And since Katniss cannot reach the hatch without opening one of these locks, the problem is unsolvable in this case. We also note that if even one necessary lock locks its own key, the problem is also unsolvable.

This solution works in $\mathcal{O}(n \log n)$: we sort all the locks, then for each key, we find the nearest lock that locks this key (or a flag indicating that the key is not locked by any lock). Next, for the locks to the right of $s$, we build an array of prefix minimums of the coordinates of the required keys for them, and for the locks to the left of $s$~--- an array of suffix maximums of the coordinates of the required keys for them. Now, as described earlier, we will construct $L_0$ and $R_0$, iterate through all the locks in $[L_0; R_0]$, and check that none of them locks its own key. Finally, we will construct $R_1, L_1, \ldots$, checking each time that the strict monotonicity of $L_i$ and $R_i$ is not violated. To find $L_i$, knowing $R_i$, we will find $\ell_j$~--- the nearest lock to $R_i$ that locks $R_i$ (at point $R_i$, there will always be either a hatch or a key). If $\ell_j$ does not exist, the process is finished; Katniss can freely walk from $s$ to $R_i$. If $\ell_j$ exists and is to the right of $s$, we find in the array of prefix minimums the leftmost key $k_q$ required to open all locks in $[s; \ell_j]$. If $k_q \ge s$, then again Katniss can simply walk to $R_i$, and all the necessary keys will be on her way~--- in other words, the process of constructing the sequences $L_i$, $R_i$ is complete. Otherwise, we say that $L_i = k_q$ and continue the process.

Finally, to find the answer, we compute $(s - L_m) + \sum_{i = 0}^{m}{(R_i - L_i)} + \sum_{i = 0}^{m - 1}{(R_i - L_{i + 1})}$, which is the sum of all segments Katniss has to walk through.

% Finally, to compute the answer, we find $C$~--- the total number of locks in the segment $[L; R]$. Then Katniss will spend $C$ seconds to open all these locks, another $C$ seconds to pick up the necessary keys for them, and $(s - L_m) + \sum_{i = 0}^{m}{(R_i - L_i)} + \sum_{i = 0}^{m - 1}{(R_i - L_{i + 1})}$ seconds on movement.